desktop-entry-parser
v1.0.0
Published
A TypeScript library for parsing .desktop files according to the Desktop Entry Specification
Maintainers
Readme
Desktop Entry Parser
A TypeScript library for parsing .desktop files according to the Desktop Entry Specification.
Installation
npm install desktop-entry-parserUsage
Basic Usage
import { parseDesktopFile, parseDesktopString } from 'desktop-entry-parser';
// Parse from file
const entry = parseDesktopFile('/usr/share/applications/firefox.desktop');
// Parse from string
const content = `[Desktop Entry]
Name=Firefox
Exec=firefox %u
Type=Application`;
const entry = parseDesktopString(content);
// Get basic properties
console.log(entry.getName()); // "Firefox"
console.log(entry.getExec()); // "firefox %u"
console.log(entry.isApplication()); // trueLocalization Support
// Get localized values
const entry = parseDesktopFile('/path/to/app.desktop');
console.log(entry.getName('zh_CN')); // Returns Chinese name if available
console.log(entry.getComment('de')); // Returns German comment if availableWorking with Actions
const entry = parseDesktopFile('/path/to/app.desktop');
// Get available actions
const actions = entry.getActions(); // ['new-window', 'new-private-window']
// Get action details
for (const action of actions) {
console.log(entry.getActionName(action));
console.log(entry.getActionExec(action));
console.log(entry.getActionIcon(action));
}Desktop Environment Visibility
const entry = parseDesktopFile('/path/to/app.desktop');
// Check if should show in specific desktop environment
console.log(entry.shouldShowIn('GNOME')); // true/false
console.log(entry.shouldShowIn('KDE')); // true/falseAdvanced API
import { DesktopEntryParser, DesktopEntryWrapper } from 'desktop-entry-parser';
// Create parser with options
const parser = new DesktopEntryParser({
encoding: 'utf8',
strictMode: true, // Throw on invalid format
preserveComments: false
});
// Parse and create wrapper
const entry = parser.parseFile('/path/to/app.desktop');
const wrapper = new DesktopEntryWrapper(entry, parser);
// Get complete metadata
const metadata = wrapper.getMetadata();
console.log(metadata.type);
console.log(metadata.categories);
console.log(metadata.mimeType);
// Get raw entry data
const rawEntry = wrapper.getEntry();
const desktopGroup = rawEntry.groups.get('Desktop Entry');
// Serialize back to string
const serialized = wrapper.serialize();
// Save to file
wrapper.saveToFile('/path/to/new.desktop');Async Operations
import { parseDesktopFileAsync } from 'desktop-entry-parser';
// Async file parsing
const entry = await parseDesktopFileAsync('/path/to/app.desktop');
// Async save
await entry.saveToFileAsync('/path/to/new.desktop');API Reference
Main Functions
parseDesktopFile(filePath: string)- Parse a desktop file synchronouslyparseDesktopFileAsync(filePath: string)- Parse a desktop file asynchronouslyparseDesktopString(content: string)- Parse desktop content from string
DesktopEntryWrapper Methods
Basic Properties
getName(locale?: string)- Get application namegetComment(locale?: string)- Get comment/descriptiongetExec()- Get executable commandgetIcon()- Get icon name/pathgetType()- Get entry type (Application/Link/Directory)getUrl()- Get URL (for Link type)
Lists
getCategories()- Get category listgetMimeTypes()- Get supported MIME typesgetActions()- Get available actions
Boolean Properties
isHidden()- Check if entry is hiddenisTerminal()- Check if runs in terminalisApplication()- Check if type is ApplicationisLink()- Check if type is LinkisDirectory()- Check if type is Directory
Actions
getActionName(action: string, locale?: string)- Get action namegetActionExec(action: string)- Get action commandgetActionIcon(action: string)- Get action icon
Desktop Environment
shouldShowIn(desktop: string)- Check if should display in desktop
Metadata
getMetadata(locale?: string)- Get all metadata as object
Serialization
serialize()- Convert to desktop file stringsaveToFile(filePath: string)- Save to filesaveToFileAsync(filePath: string)- Save to file async
License
ISC
