simen-keyboard-listener 1.1.9 → 1.1.11

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.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,520 @@
1
+ /**
2
+ * Type definitions for osascript context information module
3
+ */
4
+ interface IFrontmostApp {
5
+ /** Application name */
6
+ name: string;
7
+ /** Bundle identifier (e.g., "com.apple.finder") */
8
+ bundleId: string;
9
+ /** Whether this is Finder.app */
10
+ isFinder: boolean;
11
+ /** Application executable path */
12
+ path: string;
13
+ }
14
+ interface IFinderItem {
15
+ /** File or folder name */
16
+ name: string;
17
+ /** Absolute POSIX path */
18
+ path: string;
19
+ /** Whether this is a folder */
20
+ isFolder: boolean;
21
+ }
22
+ interface IFinderSelection {
23
+ /** Array of absolute POSIX paths */
24
+ paths: string[];
25
+ /** Number of selected items */
26
+ count: number;
27
+ /** Detailed information for each item */
28
+ items: IFinderItem[];
29
+ }
30
+ interface IFinderWindow {
31
+ /** Window title/name */
32
+ name: string;
33
+ /** Folder path displayed in this window (null for special views) */
34
+ path: string | null;
35
+ /** Window index (1-based) */
36
+ index: number;
37
+ /** true if this is a special view (Recents, Search results, etc.) */
38
+ isSpecialView: boolean;
39
+ }
40
+ interface IFileMetadata {
41
+ /** File or folder name */
42
+ name: string;
43
+ /** Absolute POSIX path */
44
+ path: string;
45
+ /** File size in bytes (0 for folders) */
46
+ size: number;
47
+ /** Whether this is a folder */
48
+ isFolder: boolean;
49
+ /** Creation date (ISO string) */
50
+ createdAt: string;
51
+ /** Modification date (ISO string) */
52
+ modifiedAt: string;
53
+ /** File extension (empty for folders) */
54
+ extension: string;
55
+ /** Kind description (e.g., "Folder", "PNG Image") */
56
+ kind: string;
57
+ }
58
+ interface IClipboardContent {
59
+ /** Text content if available */
60
+ text: string | null;
61
+ /** Whether clipboard contains file references */
62
+ hasFiles: boolean;
63
+ /** Array of file paths if clipboard contains files */
64
+ filePaths: string[];
65
+ /** Whether clipboard contains image data (screenshot, copied image) */
66
+ hasImage: boolean;
67
+ }
68
+ interface IDesktopItem {
69
+ /** File or folder name */
70
+ name: string;
71
+ /** Absolute POSIX path */
72
+ path: string;
73
+ /** Whether this is a folder */
74
+ isFolder: boolean;
75
+ }
76
+ interface IRecentFile {
77
+ /** File name */
78
+ name: string;
79
+ /** Absolute POSIX path */
80
+ path: string;
81
+ /** Last accessed date (ISO string) */
82
+ accessedAt: string;
83
+ }
84
+ interface IRunningApp {
85
+ /** Application name */
86
+ name: string;
87
+ /** Bundle identifier */
88
+ bundleId: string;
89
+ /** Whether the application is hidden */
90
+ isHidden: boolean;
91
+ /** Whether this is the frontmost application */
92
+ isFrontmost: boolean;
93
+ }
94
+ interface ISystemContext {
95
+ /** Frontmost application info */
96
+ frontmostApp: IFrontmostApp;
97
+ /** Finder selection (null if Finder is not frontmost or no selection) */
98
+ finderSelection: IFinderSelection | null;
99
+ /** Current Finder folder path (null if Finder is not open) */
100
+ finderCurrentFolder: string | null;
101
+ /** Clipboard content */
102
+ clipboard: IClipboardContent;
103
+ }
104
+ interface IExecuteOptions {
105
+ /** Timeout in milliseconds (default: 30000) */
106
+ timeout?: number;
107
+ }
108
+ interface IExecuteResult<T = string> {
109
+ /** Whether execution was successful */
110
+ success: boolean;
111
+ /** Result data if successful */
112
+ data?: T;
113
+ /** Error message if failed */
114
+ error?: string;
115
+ /** Execution time in milliseconds */
116
+ executionTime?: number;
117
+ }
118
+
119
+ /** A file or folder item in Finder selection */
120
+ interface ISelectedItem {
121
+ /** Item name (e.g., "document.pdf") */
122
+ name: string;
123
+ /** Absolute POSIX path (e.g., "/Users/xxx/document.pdf") */
124
+ path: string;
125
+ /** true if this is a folder */
126
+ isFolder: boolean;
127
+ /** File size in bytes (only when includeMetadata is true) */
128
+ size?: number;
129
+ /** Modification date ISO string (only when includeMetadata is true) */
130
+ modifiedAt?: string;
131
+ }
132
+ /** A Finder window */
133
+ interface IFinderWindowInfo {
134
+ /** Window title */
135
+ name: string;
136
+ /** Folder path displayed in window (absolute), or null for special views */
137
+ path: string | null;
138
+ /** Window index (1-based) */
139
+ index: number;
140
+ /** true if this is a special view (Recents, Search results, etc.) */
141
+ isSpecialView: boolean;
142
+ }
143
+ /**
144
+ * Complete Agent context - all information needed for Agent decision-making.
145
+ */
146
+ interface IAgentContext {
147
+ /** Frontmost (active) application */
148
+ frontmostApp: {
149
+ /** App name (e.g., "Finder", "Safari") */
150
+ name: string;
151
+ /** Bundle ID (e.g., "com.apple.finder") */
152
+ bundleId: string;
153
+ /** true if Finder is frontmost */
154
+ isFinder: boolean;
155
+ /** App path (e.g., "/Applications/Safari.app") */
156
+ path: string;
157
+ };
158
+ /** Finder selected items (up to 50, all absolute paths) */
159
+ finderSelection: {
160
+ /** Number of selected items */
161
+ count: number;
162
+ /** Detailed item info */
163
+ items: ISelectedItem[];
164
+ /** Just the paths (convenience) */
165
+ paths: string[];
166
+ };
167
+ /** Current folder in front Finder window (null if no window) */
168
+ finderCurrentFolder: string | null;
169
+ /** All open Finder windows */
170
+ finderWindows: IFinderWindowInfo[];
171
+ /** Desktop folder path (e.g., "/Users/xxx/Desktop/") */
172
+ desktopPath: string;
173
+ /** Clipboard content */
174
+ clipboard: {
175
+ /** Text content (max 2000 chars, null if empty) */
176
+ text: string | null;
177
+ /** true if clipboard contains file references */
178
+ hasFiles: boolean;
179
+ /** File paths if hasFiles is true */
180
+ filePaths: string[];
181
+ /** true if clipboard contains image data (screenshot, copied image) */
182
+ hasImage: boolean;
183
+ };
184
+ /** Unix timestamp when context was captured */
185
+ timestamp: number;
186
+ }
187
+ /** Options for getAgentContext */
188
+ interface IAgentContextOptions extends IExecuteOptions {
189
+ /** Include file metadata (size, modifiedAt) for selected items. Slower (~350ms vs ~250ms) */
190
+ includeMetadata?: boolean;
191
+ }
192
+ /**
193
+ * Get complete Agent context in ONE osascript call.
194
+ *
195
+ * @param opts - Options
196
+ * @param opts.timeout - Timeout in ms (default: 30000)
197
+ * @param opts.includeMetadata - Include file size/modifiedAt for selected items (slower)
198
+ * @returns Agent context, or null if not on macOS or failed
199
+ *
200
+ * @example
201
+ * // Basic usage (~250ms)
202
+ * const ctx = await getAgentContext();
203
+ *
204
+ * // With custom timeout
205
+ * const ctx = await getAgentContext({ timeout: 5000 });
206
+ *
207
+ * // With file metadata (~350ms)
208
+ * const ctx = await getAgentContext({ includeMetadata: true });
209
+ */
210
+ declare function getAgentContext(opts?: IAgentContextOptions): Promise<IAgentContext | null>;
211
+
212
+ /**
213
+ * Check if osascript is available (macOS only)
214
+ */
215
+ declare function isOsascriptAvailable(): boolean;
216
+ /**
217
+ * Execute a single-line AppleScript
218
+ *
219
+ * @param script - AppleScript code to execute
220
+ * @param options - Execution options
221
+ * @returns Execution result with stdout as data
222
+ */
223
+ declare function executeAppleScript(script: string, options?: IExecuteOptions): Promise<IExecuteResult<string>>;
224
+ /**
225
+ * Execute multi-line AppleScript
226
+ *
227
+ * @param lines - Array of AppleScript lines
228
+ * @param options - Execution options
229
+ * @returns Execution result with stdout as data
230
+ */
231
+ declare function executeAppleScriptLines(lines: string[], options?: IExecuteOptions): Promise<IExecuteResult<string>>;
232
+ /**
233
+ * Execute AppleScript and parse the result
234
+ *
235
+ * @param script - AppleScript code to execute
236
+ * @param parser - Function to parse the raw output
237
+ * @param options - Execution options
238
+ * @returns Parsed result
239
+ */
240
+ declare function executeAndParse<T>(script: string, parser: (output: string) => T, options?: IExecuteOptions): Promise<IExecuteResult<T>>;
241
+ /**
242
+ * Execute multi-line AppleScript and parse the result
243
+ *
244
+ * @param lines - Array of AppleScript lines
245
+ * @param parser - Function to parse the raw output
246
+ * @param options - Execution options
247
+ * @returns Parsed result
248
+ */
249
+ declare function executeMultilineAndParse<T>(lines: string[], parser: (output: string) => T, options?: IExecuteOptions): Promise<IExecuteResult<T>>;
250
+ /**
251
+ * Escape a string for use in AppleScript
252
+ * Handles quotes and backslashes
253
+ */
254
+ declare function escapeForAppleScript(str: string): string;
255
+
256
+ /**
257
+ * A Finder window
258
+ */
259
+ interface IFinderWindowItem {
260
+ /** Window title (e.g., "Downloads", "Recents") */
261
+ name: string;
262
+ /** Folder path (absolute POSIX path), null for special views */
263
+ path: string | null;
264
+ /** Window index (1-based, 1 = frontmost) */
265
+ index: number;
266
+ /** true if this is a special view (Recents, AirDrop, Search, Tags, etc.) */
267
+ isSpecialView: boolean;
268
+ }
269
+ /**
270
+ * Finder context - lightweight info about Finder state
271
+ */
272
+ interface IFinderContext {
273
+ /** All open Finder windows (ordered by z-index, front first) */
274
+ windows: IFinderWindowItem[];
275
+ /**
276
+ * Front Finder window info.
277
+ * - null if Finder is not frontmost (user is in another app)
278
+ * - null if no Finder windows are open
279
+ */
280
+ frontWindow: {
281
+ name: string;
282
+ path: string | null;
283
+ isSpecialView: boolean;
284
+ } | null;
285
+ /**
286
+ * Selected file/folder paths in the front Finder window.
287
+ * - Empty array if nothing selected
288
+ * - Empty array if Finder is not frontmost
289
+ * - Max 100 items for performance
290
+ */
291
+ selectedPaths: string[];
292
+ /** true if Finder.app is the frontmost application */
293
+ isFinderFrontmost: boolean;
294
+ /** Number of open Finder windows */
295
+ windowCount: number;
296
+ }
297
+ /**
298
+ * Get lightweight Finder context.
299
+ *
300
+ * This is optimized for scenarios where you only need Finder-related info,
301
+ * not the full system context (clipboard, desktop, etc.).
302
+ *
303
+ * @param options - Execution options (timeout, etc.)
304
+ * @returns Finder context, or null if not on macOS or failed
305
+ *
306
+ * @example
307
+ * const ctx = await getFinderContext();
308
+ * if (ctx?.isFinderFrontmost) {
309
+ * console.log('Current folder:', ctx.frontWindow?.path);
310
+ * console.log('Selected files:', ctx.selectedPaths);
311
+ * }
312
+ *
313
+ * @example
314
+ * // List all Finder windows
315
+ * const ctx = await getFinderContext();
316
+ * for (const win of ctx?.windows ?? []) {
317
+ * if (win.isSpecialView) {
318
+ * console.log(`Special view: ${win.name}`);
319
+ * } else {
320
+ * console.log(`Folder: ${win.path}`);
321
+ * }
322
+ * }
323
+ */
324
+ declare function getFinderContext(options?: IExecuteOptions): Promise<IFinderContext | null>;
325
+
326
+ /**
327
+ * Get information about the frontmost (active) application
328
+ *
329
+ * @param options - Execution options
330
+ * @returns Frontmost application info, or null if failed
331
+ */
332
+ declare function getFrontmostApp(options?: IExecuteOptions): Promise<IFrontmostApp | null>;
333
+
334
+ /**
335
+ * Get the current Finder selection (selected files/folders)
336
+ *
337
+ * @param options - Execution options
338
+ * @returns Finder selection info with absolute paths, or null if failed
339
+ */
340
+ declare function getFinderSelection(options?: IExecuteOptions): Promise<IFinderSelection | null>;
341
+ /**
342
+ * Get the current folder displayed in Finder's front window
343
+ *
344
+ * @param options - Execution options
345
+ * @returns Current folder path, or null if no Finder window or failed
346
+ */
347
+ declare function getFinderCurrentFolder(options?: IExecuteOptions): Promise<string | null>;
348
+
349
+ /**
350
+ * Get information about all open Finder windows
351
+ *
352
+ * @param options - Execution options
353
+ * @returns Array of Finder window info, or null if failed
354
+ */
355
+ declare function getFinderWindows(options?: IExecuteOptions): Promise<IFinderWindow[] | null>;
356
+
357
+ /**
358
+ * Get metadata for a file or folder
359
+ *
360
+ * @param filePath - Absolute POSIX path to the file/folder
361
+ * @param options - Execution options
362
+ * @returns File metadata, or null if failed
363
+ */
364
+ declare function getFileMetadata(filePath: string, options?: IExecuteOptions): Promise<IFileMetadata | null>;
365
+ /**
366
+ * Get metadata for multiple files/folders
367
+ *
368
+ * @param filePaths - Array of absolute POSIX paths
369
+ * @param options - Execution options
370
+ * @returns Array of file metadata (null entries for failed lookups)
371
+ */
372
+ declare function getFilesMetadata(filePaths: string[], options?: IExecuteOptions): Promise<(IFileMetadata | null)[]>;
373
+
374
+ /**
375
+ * Get clipboard content (text and/or file references)
376
+ *
377
+ * @param options - Execution options
378
+ * @returns Clipboard content info, or null if failed
379
+ */
380
+ declare function getClipboardContent(options?: IExecuteOptions): Promise<IClipboardContent | null>;
381
+ /**
382
+ * Get clipboard text only (faster if you only need text)
383
+ *
384
+ * @param options - Execution options
385
+ * @returns Clipboard text, or null if no text or failed
386
+ */
387
+ declare function getClipboardText(options?: IExecuteOptions): Promise<string | null>;
388
+
389
+ /**
390
+ * Get all items on the desktop
391
+ *
392
+ * @param options - Execution options
393
+ * @returns Array of desktop items, or null if failed
394
+ */
395
+ declare function getDesktopItems(options?: IExecuteOptions): Promise<IDesktopItem[] | null>;
396
+ /**
397
+ * Get the desktop folder path
398
+ *
399
+ * @param options - Execution options
400
+ * @returns Desktop folder path, or null if failed
401
+ */
402
+ declare function getDesktopPath(options?: IExecuteOptions): Promise<string | null>;
403
+
404
+ /**
405
+ * Get recent files from Finder's Recents folder
406
+ *
407
+ * @param limit - Maximum number of files to return (default: 20)
408
+ * @param options - Execution options
409
+ * @returns Array of recent files, or null if failed
410
+ */
411
+ declare function getRecentFiles(limit?: number, options?: IExecuteOptions): Promise<IRecentFile[] | null>;
412
+ /**
413
+ * Get recently modified documents (using mdfind)
414
+ * This searches for files modified in the last 7 days
415
+ *
416
+ * @param options - Execution options
417
+ * @returns Array of recent files, or null if failed
418
+ */
419
+ declare function getRecentDocuments(options?: IExecuteOptions): Promise<IRecentFile[] | null>;
420
+
421
+ /**
422
+ * Get all running applications
423
+ *
424
+ * @param options - Execution options
425
+ * @returns Array of running apps, or null if failed
426
+ */
427
+ declare function getRunningApps(options?: IExecuteOptions): Promise<IRunningApp[] | null>;
428
+ /**
429
+ * Check if a specific application is running
430
+ *
431
+ * @param appName - Application name or bundle ID
432
+ * @param options - Execution options
433
+ * @returns true if running, false otherwise
434
+ */
435
+ declare function isAppRunning(appName: string, options?: IExecuteOptions): Promise<boolean>;
436
+ /**
437
+ * Get the frontmost application from the running apps list
438
+ *
439
+ * @param options - Execution options
440
+ * @returns Frontmost app info, or null if failed
441
+ */
442
+ declare function getFrontmostFromRunning(options?: IExecuteOptions): Promise<IRunningApp | null>;
443
+
444
+ /** Result of reading file content */
445
+ interface IFileContent {
446
+ /** File path that was read */
447
+ path: string;
448
+ /** File content as text */
449
+ content: string;
450
+ /** File size in bytes */
451
+ size: number;
452
+ /** true if read was successful */
453
+ success: boolean;
454
+ /** Error message if failed */
455
+ error?: string;
456
+ }
457
+ /** Options for readFileContent */
458
+ interface IReadFileOptions extends IExecuteOptions {
459
+ /** Maximum bytes to read (default: 1MB = 1048576) */
460
+ maxBytes?: number;
461
+ /** Encoding (default: utf8) */
462
+ encoding?: 'utf8' | 'utf16';
463
+ }
464
+ /**
465
+ * Read file content using AppleScript.
466
+ *
467
+ * @param filePath - Absolute POSIX path to the file
468
+ * @param opts - Options
469
+ * @returns File content, or null if not on macOS
470
+ *
471
+ * @example
472
+ * const result = await readFileContent('/Users/xxx/document.txt');
473
+ * if (result?.success) {
474
+ * console.log(result.content);
475
+ * }
476
+ *
477
+ * @example
478
+ * // Read with size limit
479
+ * const result = await readFileContent('/path/to/file.txt', { maxBytes: 10000 });
480
+ */
481
+ declare function readFileContent(filePath: string, opts?: IReadFileOptions): Promise<IFileContent | null>;
482
+ /**
483
+ * Read multiple files in parallel.
484
+ *
485
+ * @param filePaths - Array of absolute POSIX paths
486
+ * @param opts - Options
487
+ * @returns Array of file contents
488
+ *
489
+ * @example
490
+ * const results = await readMultipleFiles(['/path/a.txt', '/path/b.txt']);
491
+ */
492
+ declare function readMultipleFiles(filePaths: string[], opts?: IReadFileOptions): Promise<IFileContent[]>;
493
+
494
+ /**
495
+ * osascript module - macOS system context for Agent decision-making
496
+ *
497
+ * RECOMMENDED: Use getAgentContext() for best performance (~250ms).
498
+ * It retrieves all commonly needed info in a single osascript call.
499
+ *
500
+ * Available information:
501
+ * - Frontmost application (name, bundleId, path)
502
+ * - Finder selection (files/folders with absolute paths, supports multi-select)
503
+ * - Finder windows (all open windows with paths)
504
+ * - Desktop path
505
+ * - Clipboard content (text and/or file paths)
506
+ *
507
+ * Additional APIs for specific use cases:
508
+ * - File metadata, recent files, running apps, etc.
509
+ *
510
+ * Note: macOS only. Returns null on other platforms.
511
+ */
512
+
513
+ /**
514
+ * @deprecated Use getAgentContext() instead for better performance.
515
+ */
516
+ declare function getSystemContext(options?: IExecuteOptions): Promise<ISystemContext | null>;
517
+
1
518
  type IGlobalKeyState = 'DOWN' | 'UP' | 'PERMISSION_LOST';
2
519
  interface IGlobalKeyEvent {
3
520
  readonly name: string;
@@ -65,4 +582,4 @@ declare function getSelectedTextSmart(): string | null;
65
582
  */
66
583
  declare function setBlockSystemHotkeys(block: boolean): void;
67
584
 
68
- export { type IGlobalKeyDownMap, type IGlobalKeyEvent, type IGlobalKeyListener, type IGlobalKeyState, type IGlobalKeyboardListener, type IPermissionLostListener, checkKeyboardPermission, createGlobalKeyboardListener, getContextJSON, getFocusedInputSelectedText, getFocusedInputValue, getGlobalKeyboardListener, getSelectedTextSmart, setBlockSystemHotkeys };
585
+ export { type IAgentContext, type IAgentContextOptions, type IClipboardContent, type IDesktopItem, type IExecuteOptions, type IExecuteResult, type IFileContent, type IFileMetadata, type IFinderContext, type IFinderItem, type IFinderSelection, type IFinderWindow, type IFinderWindowInfo, type IFinderWindowItem, type IFrontmostApp, type IGlobalKeyDownMap, type IGlobalKeyEvent, type IGlobalKeyListener, type IGlobalKeyState, type IGlobalKeyboardListener, type IPermissionLostListener, type IReadFileOptions, type IRecentFile, type IRunningApp, type ISelectedItem, type ISystemContext, checkKeyboardPermission, createGlobalKeyboardListener, escapeForAppleScript, executeAndParse, executeAppleScript, executeAppleScriptLines, executeMultilineAndParse, getAgentContext, getClipboardContent, getClipboardText, getContextJSON, getDesktopItems, getDesktopPath, getFileMetadata, getFilesMetadata, getFinderContext, getFinderCurrentFolder, getFinderSelection, getFinderWindows, getFocusedInputSelectedText, getFocusedInputValue, getFrontmostApp, getFrontmostFromRunning, getGlobalKeyboardListener, getRecentDocuments, getRecentFiles, getRunningApps, getSelectedTextSmart, getSystemContext, isAppRunning, isOsascriptAvailable, readFileContent, readMultipleFiles, setBlockSystemHotkeys };