applescript-node 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ export interface OsaLanguageInfo {
2
+ name: string;
3
+ subtype: string;
4
+ manufacturer: string;
5
+ capabilities: {
6
+ compiling: boolean;
7
+ sourceData: boolean;
8
+ coercion: boolean;
9
+ eventHandling: boolean;
10
+ recording: boolean;
11
+ convenience: boolean;
12
+ dialects: boolean;
13
+ appleEvents: boolean;
14
+ };
15
+ description?: string;
16
+ }
17
+ /**
18
+ * Get all installed OSA (Open Scripting Architecture) languages
19
+ *
20
+ * **IMPORTANT: Empty Output Handling**
21
+ *
22
+ * This function includes critical handling for edge cases:
23
+ *
24
+ * 1. **Empty Output**: When `osalang -L` returns empty output (e.g., on systems
25
+ * with no languages installed, or in test environments), the function:
26
+ * - Filters out empty lines to prevent parsing errors
27
+ * - Returns an empty array instead of crashing
28
+ *
29
+ * 2. **Missing Flags**: When a language line doesn't have capability flags:
30
+ * - Defaults to empty string for flags parameter
31
+ * - parseCapabilityFlags() safely handles empty strings (all capabilities false)
32
+ *
33
+ * 3. **Why This Matters**: Without proper empty output handling:
34
+ * - Empty stdout would cause split() to return [''], leading to parsing errors
35
+ * - Missing flags would cause undefined access in parseCapabilityFlags()
36
+ * - Tests and edge cases would fail unpredictably
37
+ *
38
+ * **Usage Example:**
39
+ * ```typescript
40
+ * const languages = await getInstalledLanguages();
41
+ * if (languages.length === 0) {
42
+ * console.warn('No OSA languages found');
43
+ * } else {
44
+ * languages.forEach(lang => console.log(`${lang.name}: ${lang.capabilities}`));
45
+ * }
46
+ * ```
47
+ *
48
+ * **Testing Considerations:**
49
+ * - Always test with empty output (simulates edge cases)
50
+ * - Test with malformed lines (missing flags, etc.)
51
+ * - Verify empty arrays are returned gracefully
52
+ * - Ensure parseCapabilityFlags handles empty strings safely
53
+ *
54
+ * @returns Promise resolving to array of language information objects
55
+ * @throws Never throws - always returns array (empty if no languages found)
56
+ *
57
+ * @see {@link OsaLanguageInfo} for the language information structure
58
+ * @see {@link parseCapabilityFlags} for capability flag parsing
59
+ */
60
+ export declare function getInstalledLanguages(): Promise<OsaLanguageInfo[]>;
61
+ export declare function getDefaultLanguage(): Promise<OsaLanguageInfo>;
@@ -0,0 +1,24 @@
1
+ import { AppleScriptBuilder } from './builder.js';
2
+ /**
3
+ * Load an AppleScript file and return a builder instance that can be edited further.
4
+ * The script is parsed to maintain proper block structure (tell, if, repeat, etc.)
5
+ * so you can continue adding commands using the fluent builder API.
6
+ *
7
+ * @param filePath Absolute or relative path to the AppleScript (.applescript or .scpt) file
8
+ * @returns Promise that resolves to an AppleScriptBuilder instance with the loaded script
9
+ * @throws Error if the file cannot be read
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * // Load an existing script and append new commands
14
+ * const script = await loadScript('./my-script.applescript');
15
+ * script
16
+ * .tell('Finder')
17
+ * .displayDialog('Script loaded and extended!')
18
+ * .endtell();
19
+ *
20
+ * const result = await runScript(script);
21
+ * console.log(result.output);
22
+ * ```
23
+ */
24
+ export declare function loadScript(filePath: string): Promise<AppleScriptBuilder>;
package/dist/sdef.d.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { ApplicationDictionary, Class, Command } from './types.js';
2
+ /**
3
+ * Extract the scripting definition (sdef) XML for a macOS application
4
+ * @param appPath - Path to the application (e.g., '/System/Applications/Messages.app')
5
+ * @returns Promise resolving to the sdef XML string
6
+ */
7
+ export declare function getSdef(appPath: string): Promise<string>;
8
+ /**
9
+ * Parse sdef XML into a structured ApplicationDictionary
10
+ * @param xml - The sdef XML string to parse
11
+ * @returns Parsed ApplicationDictionary
12
+ */
13
+ export declare function parseSdef(xml: string): ApplicationDictionary;
14
+ /**
15
+ * Get and parse the application dictionary for a macOS application
16
+ * @param appPath - Path to the application (e.g., '/System/Applications/Messages.app')
17
+ * @param useCache - Whether to use cached results (default: true)
18
+ * @returns Promise resolving to the parsed ApplicationDictionary
19
+ */
20
+ export declare function getApplicationDictionary(appPath: string, useCache?: boolean): Promise<ApplicationDictionary>;
21
+ /**
22
+ * Clear the sdef cache
23
+ */
24
+ export declare function clearSdefCache(): void;
25
+ /**
26
+ * Get a list of all commands in an application dictionary
27
+ * @param dictionary - The application dictionary
28
+ * @returns Array of all commands from all suites
29
+ */
30
+ export declare function getAllCommands(dictionary: ApplicationDictionary): Command[];
31
+ /**
32
+ * Get a list of all classes in an application dictionary
33
+ * @param dictionary - The application dictionary
34
+ * @returns Array of all classes from all suites
35
+ */
36
+ export declare function getAllClasses(dictionary: ApplicationDictionary): Class[];
37
+ /**
38
+ * Find a command by name in an application dictionary
39
+ * @param dictionary - The application dictionary
40
+ * @param commandName - The name of the command to find
41
+ * @returns The command if found, undefined otherwise
42
+ */
43
+ export declare function findCommand(dictionary: ApplicationDictionary, commandName: string): Command | undefined;
44
+ /**
45
+ * Find a class by name in an application dictionary
46
+ * @param dictionary - The application dictionary
47
+ * @param className - The name of the class to find
48
+ * @returns The class if found, undefined otherwise
49
+ */
50
+ export declare function findClass(dictionary: ApplicationDictionary, className: string): Class | undefined;
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Application information returned from macOS
3
+ */
4
+ export interface ApplicationInfo {
5
+ name: string;
6
+ bundleId: string;
7
+ pid: number;
8
+ visible: boolean;
9
+ frontmost: boolean;
10
+ windowCount: number;
11
+ }
12
+ /**
13
+ * Get all running applications
14
+ * @param includeBackgroundApps - Include background-only applications (default: false)
15
+ * @returns Array of running application information
16
+ *
17
+ * @example
18
+ * import { applications } from 'applescript-node/sources';
19
+ * const apps = await applications.getAll();
20
+ * console.log(`Found ${apps.length} running applications`);
21
+ */
22
+ export declare function getAll(includeBackgroundApps?: boolean): Promise<ApplicationInfo[]>;
23
+ /**
24
+ * Get the frontmost (focused) application
25
+ * @returns The currently focused application
26
+ *
27
+ * @example
28
+ * import { applications } from 'applescript-node/sources';
29
+ * const frontApp = await applications.getFrontmost();
30
+ * console.log(`Active application: ${frontApp.name}`);
31
+ */
32
+ export declare function getFrontmost(): Promise<ApplicationInfo>;
33
+ /**
34
+ * Check if an application is running
35
+ * @param appName - Name of the application to check
36
+ * @returns True if the application is running, false otherwise
37
+ *
38
+ * @example
39
+ * import { applications } from 'applescript-node/sources';
40
+ * if (await applications.isRunning('Safari')) {
41
+ * console.log('Safari is running');
42
+ * }
43
+ */
44
+ export declare function isRunning(appName: string): Promise<boolean>;
45
+ /**
46
+ * Get application by name
47
+ * @param appName - Name of the application
48
+ * @returns Application information, or null if not running
49
+ *
50
+ * @example
51
+ * import { applications } from 'applescript-node/sources';
52
+ * const safari = await applications.getByName('Safari');
53
+ * if (safari) {
54
+ * console.log(`Safari PID: ${safari.pid}`);
55
+ * }
56
+ */
57
+ export declare function getByName(appName: string): Promise<ApplicationInfo | null>;
58
+ /**
59
+ * Activate (bring to front) an application
60
+ * @param appName - Name of the application to activate
61
+ *
62
+ * @example
63
+ * import { applications } from 'applescript-node/sources';
64
+ * await applications.activate('Safari');
65
+ */
66
+ export declare function activate(appName: string): Promise<void>;
67
+ /**
68
+ * Launch an application without activating it
69
+ * @param appName - Name of the application to launch
70
+ *
71
+ * @example
72
+ * import { applications } from 'applescript-node/sources';
73
+ * await applications.launch('Calendar');
74
+ */
75
+ export declare function launch(appName: string): Promise<void>;
76
+ /**
77
+ * Quit an application
78
+ * @param appName - Name of the application to quit
79
+ *
80
+ * @example
81
+ * import { applications } from 'applescript-node/sources';
82
+ * await applications.quit('TextEdit');
83
+ */
84
+ export declare function quit(appName: string): Promise<void>;
85
+ /**
86
+ * Hide an application
87
+ * @param appName - Name of the application to hide
88
+ *
89
+ * @example
90
+ * import { applications } from 'applescript-node/sources';
91
+ * await applications.hide('Safari');
92
+ */
93
+ export declare function hide(appName: string): Promise<void>;
94
+ /**
95
+ * Show (unhide) an application
96
+ * @param appName - Name of the application to show
97
+ *
98
+ * @example
99
+ * import { applications } from 'applescript-node/sources';
100
+ * await applications.show('Safari');
101
+ */
102
+ export declare function show(appName: string): Promise<void>;
@@ -0,0 +1,29 @@
1
+ /**
2
+ * High-level data source APIs for common macOS operations.
3
+ *
4
+ * These modules provide clean, succinct entry points for typical macOS data sourcing tasks.
5
+ * Each module wraps the low-level builder API to provide simple, async functions.
6
+ *
7
+ * @example
8
+ * // Get all open windows across all applications
9
+ * import { windows } from 'applescript-node/sources';
10
+ * const allWindows = await windows.getAll();
11
+ *
12
+ * @example
13
+ * // Get all running applications
14
+ * import { applications } from 'applescript-node/sources';
15
+ * const apps = await applications.getAll();
16
+ *
17
+ * @example
18
+ * // Get system information
19
+ * import { system } from 'applescript-node/sources';
20
+ * const info = await system.getInfo();
21
+ *
22
+ * @module sources
23
+ */
24
+ export type { ApplicationInfo } from './applications.js';
25
+ export * as applications from './applications.js';
26
+ export type { DisplayInfo, SystemInfo, VolumeInfo } from './system.js';
27
+ export * as system from './system.js';
28
+ export type { WindowInfo } from './windows.js';
29
+ export * as windows from './windows.js';
@@ -0,0 +1,178 @@
1
+ /**
2
+ * System information returned from macOS
3
+ */
4
+ export interface SystemInfo {
5
+ computerName: string;
6
+ hostname: string;
7
+ osVersion: string;
8
+ systemVersion: string;
9
+ userName: string;
10
+ homeDirectory: string;
11
+ architecture: string;
12
+ }
13
+ /**
14
+ * Volume information for mounted drives
15
+ */
16
+ export interface VolumeInfo {
17
+ name: string;
18
+ capacity: number;
19
+ freeSpace: number;
20
+ usedSpace: number;
21
+ format: string;
22
+ ejectable: boolean;
23
+ }
24
+ /**
25
+ * Display information
26
+ */
27
+ export interface DisplayInfo {
28
+ id: number;
29
+ bounds: {
30
+ x: number;
31
+ y: number;
32
+ width: number;
33
+ height: number;
34
+ };
35
+ }
36
+ /**
37
+ * Get system information
38
+ * @returns System information including computer name, OS version, etc.
39
+ *
40
+ * @example
41
+ * import { system } from 'applescript-node/sources';
42
+ * const info = await system.getInfo();
43
+ * console.log(`Computer: ${info.computerName}`);
44
+ * console.log(`OS: ${info.osVersion}`);
45
+ */
46
+ export declare function getInfo(): Promise<SystemInfo>;
47
+ /**
48
+ * Get information about mounted volumes
49
+ * @returns Array of volume information for all mounted drives
50
+ *
51
+ * @example
52
+ * import { system } from 'applescript-node/sources';
53
+ * const volumes = await system.getVolumes();
54
+ * volumes.forEach(vol => {
55
+ * console.log(`${vol.name}: ${vol.freeSpace}GB free of ${vol.capacity}GB`);
56
+ * });
57
+ */
58
+ export declare function getVolumes(): Promise<VolumeInfo[]>;
59
+ /**
60
+ * Get display information for all connected displays
61
+ * @returns Array of display information
62
+ *
63
+ * @example
64
+ * import { system } from 'applescript-node/sources';
65
+ * const displays = await system.getDisplays();
66
+ * console.log(`Found ${displays.length} display(s)`);
67
+ */
68
+ export declare function getDisplays(): Promise<DisplayInfo[]>;
69
+ /**
70
+ * Get the current clipboard contents
71
+ * @returns Clipboard text content
72
+ *
73
+ * @example
74
+ * import { system } from 'applescript-node/sources';
75
+ * const clipboardText = await system.getClipboard();
76
+ * console.log('Clipboard:', clipboardText);
77
+ */
78
+ export declare function getClipboard(): Promise<string>;
79
+ /**
80
+ * Set the clipboard contents
81
+ * @param text - Text to set in clipboard
82
+ *
83
+ * @example
84
+ * import { system } from 'applescript-node/sources';
85
+ * await system.setClipboard('Hello, World!');
86
+ */
87
+ export declare function setClipboard(text: string): Promise<void>;
88
+ /**
89
+ * Get path to common system folders
90
+ * @param folder - Folder identifier (e.g., 'home', 'desktop', 'documents', 'downloads', 'applications', 'library', 'temporary items')
91
+ * @returns POSIX path to the folder
92
+ *
93
+ * @example
94
+ * import { system } from 'applescript-node/sources';
95
+ * const desktop = await system.getPath('desktop');
96
+ * console.log('Desktop path:', desktop);
97
+ */
98
+ export declare function getPath(folder: 'home' | 'desktop' | 'documents' | 'downloads' | 'applications' | 'library' | 'temporary items' | 'music' | 'pictures' | 'movies' | 'public'): Promise<string>;
99
+ /**
100
+ * Check if the system is in dark mode
101
+ * @returns True if dark mode is enabled
102
+ *
103
+ * @example
104
+ * import { system } from 'applescript-node/sources';
105
+ * if (await system.isDarkMode()) {
106
+ * console.log('Dark mode is enabled');
107
+ * }
108
+ */
109
+ export declare function isDarkMode(): Promise<boolean>;
110
+ /**
111
+ * Get system uptime in seconds
112
+ * @returns Number of seconds since last boot
113
+ *
114
+ * @example
115
+ * import { system } from 'applescript-node/sources';
116
+ * const uptime = await system.getUptime();
117
+ * console.log(`System uptime: ${Math.floor(uptime / 3600)} hours`);
118
+ */
119
+ /**
120
+ * Get system uptime in seconds since last boot
121
+ *
122
+ * **IMPORTANT: Invalid Input Handling**
123
+ *
124
+ * This function includes critical error handling for edge cases:
125
+ *
126
+ * 1. **Invalid Boot Time**: When the shell script returns invalid output
127
+ * (non-numeric, malformed, etc.):
128
+ * - Number.parseInt() returns NaN
129
+ * - NaN is detected and defaults to 0 (treats as current time)
130
+ * - Result: uptime = current_time - 0 = large positive number
131
+ *
132
+ * 2. **Why Default to 0**: Defaulting invalid boot time to 0 ensures:
133
+ * - Function always returns a valid number (never NaN)
134
+ * - Result is always positive (current time is always > 0)
135
+ * - Downstream code can safely use the result without NaN checks
136
+ *
137
+ * 3. **Why This Matters**: Without proper NaN handling:
138
+ * - `now - NaN` = NaN, causing downstream errors
139
+ * - Tests expecting positive numbers would fail
140
+ * - Production code could crash on unexpected input
141
+ *
142
+ * **Usage Example:**
143
+ * ```typescript
144
+ * const uptime = await system.getUptime();
145
+ * console.log(`System has been up for ${uptime} seconds`);
146
+ * console.log(`That's ${Math.floor(uptime / 3600)} hours`);
147
+ * ```
148
+ *
149
+ * **Edge Cases Handled:**
150
+ * - Invalid/malformed boot time output → defaults to 0
151
+ * - Empty output → defaults to 0
152
+ * - Non-numeric output → defaults to 0
153
+ * - Script execution failure → throws error (expected behavior)
154
+ *
155
+ * **Testing Considerations:**
156
+ * - Test with valid boot time output
157
+ * - Test with invalid output (non-numeric, empty, etc.)
158
+ * - Verify NaN is properly handled and defaults to 0
159
+ * - Ensure result is always a positive number
160
+ *
161
+ * @returns Promise resolving to uptime in seconds (always positive, never NaN)
162
+ * @throws Error if script execution fails (different from invalid output)
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * try {
167
+ * const uptime = await getUptime();
168
+ * // uptime is always a valid positive number
169
+ * if (uptime > 86400) {
170
+ * console.log('System has been up for more than a day');
171
+ * }
172
+ * } catch (error) {
173
+ * // Only thrown if osascript execution fails
174
+ * console.error('Failed to get uptime:', error);
175
+ * }
176
+ * ```
177
+ */
178
+ export declare function getUptime(): Promise<number>;
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Window information returned from macOS
3
+ */
4
+ export interface WindowInfo {
5
+ name: string;
6
+ app: string;
7
+ id: number;
8
+ bounds: {
9
+ x: number;
10
+ y: number;
11
+ width: number;
12
+ height: number;
13
+ };
14
+ minimized: boolean;
15
+ zoomed: boolean;
16
+ visible: boolean;
17
+ }
18
+ /**
19
+ * Get all open windows across all applications
20
+ * @returns Array of window information for all visible windows
21
+ *
22
+ * @example
23
+ * import { windows } from 'applescript-node/sources';
24
+ * const allWindows = await windows.getAll();
25
+ * console.log(`Found ${allWindows.length} open windows`);
26
+ */
27
+ export declare function getAll(): Promise<WindowInfo[]>;
28
+ /**
29
+ * Get all windows for a specific application
30
+ * @param appName - Name of the application (e.g., 'Safari', 'Chrome', 'Finder')
31
+ * @returns Array of window information for the specified application
32
+ *
33
+ * @example
34
+ * import { windows } from 'applescript-node/sources';
35
+ * const safariWindows = await windows.getByApp('Safari');
36
+ * console.log(`Safari has ${safariWindows.length} windows open`);
37
+ */
38
+ export declare function getByApp(appName: string): Promise<WindowInfo[]>;
39
+ /**
40
+ * Get the frontmost (focused) window across all applications
41
+ * @returns The currently focused window, or null if no window is focused
42
+ *
43
+ * @example
44
+ * import { windows } from 'applescript-node/sources';
45
+ * const activeWindow = await windows.getFrontmost();
46
+ * if (activeWindow) {
47
+ * console.log(`Active window: ${activeWindow.name} (${activeWindow.app})`);
48
+ * }
49
+ */
50
+ export declare function getFrontmost(): Promise<WindowInfo | null>;
51
+ /**
52
+ * Get window count by application
53
+ * @returns Map of application names to window counts
54
+ *
55
+ * @example
56
+ * import { windows } from 'applescript-node/sources';
57
+ * const counts = await windows.getCountByApp();
58
+ * console.log('Window counts:', counts);
59
+ * // { 'Safari': 3, 'Chrome': 2, 'Terminal': 1 }
60
+ */
61
+ export declare function getCountByApp(): Promise<Record<string, number>>;