-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathQSUIAccessPlugIn_Action.m
More file actions
613 lines (534 loc) · 23.8 KB
/
QSUIAccessPlugIn_Action.m
File metadata and controls
613 lines (534 loc) · 23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//
// QSUIAccessPlugIn_Action.m
// QSUIAccessPlugIn
//
// Created by Nicholas Jitkoff on 9/25/04.
// Copyright __MyCompanyName__ 2004. All rights reserved.
//
#import "QSUIAccessPlugIn_Action.h"
#import <ApplicationServices/ApplicationServices.h>
#import "QSUIAccessPlugIn_Source.h"
@implementation QSUIAccessPlugIn_Action
NSArray *MenuItemsForElement(AXUIElementRef element, NSInteger depth, NSString *elementName, NSString *parentName, NSInteger menuIgnoreDepth, NSRunningApplication *process) {
NSUInteger childrenCount = 0;
NSArray *children = nil;
if (depth > 0) {
// don't work out the children if we're not going any deeper
AXUIElementCopyAttributeValue(element, kAXChildrenAttribute, (CFTypeRef *)&children);
childrenCount = [children count];
}
if (depth < 1 || childrenCount < 1) {
QSObject *menuObject = [QSObject objectForUIElement:(id)element name:elementName parent:parentName process:process];
return (menuObject) ? [NSArray arrayWithObject:menuObject] : [NSArray array];
}
NSMutableOrderedSet *menuItems = [NSMutableOrderedSet new];
NSString *_parentName = nil;
AXUIElementCopyAttributeValue(element, kAXTitleAttribute, (CFTypeRef *)&_parentName);
@autoreleasepool {
// Track the last disabled item as a potential section header
NSString *currentSectionHeader = nil;
for (id child in children) {
NSString *name = nil;
// try not to get the name attribute and test it unless we really have to
if (AXUIElementCopyAttributeValue((AXUIElementRef)child, kAXTitleAttribute, (CFTypeRef *)&name) == kAXErrorSuccess)
{
[name autorelease];
# warning "Comparing name equality in English only will break localised apps"
if ([name isEqualToString:@"Apple"] || [name isEqualToString:@"Services"])
{
continue;
}
}
NSString *identifier = nil;
AXUIElementCopyAttributeValue((AXUIElementRef)child, kAXIdentifierAttribute, (CFTypeRef *)&identifier);
[identifier autorelease];
CFTypeRef enabled = NULL;
if (AXUIElementCopyAttributeValue((AXUIElementRef)child, kAXEnabledAttribute, &enabled) != kAXErrorSuccess) continue;
[(id)enabled autorelease];
// Handle disabled items as potential section headers
if (!CFBooleanGetValue(enabled)) {
// Only treat as section header if it has a known identifier pattern
BOOL isSectionHeader = NO;
if (identifier != nil && name != nil && [name length] > 0) {
// Check for known identifier patterns where section headers appear
if ([identifier containsString:@"mailboxesChooserMenu"] ||
[identifier containsString:@"mailboxMenuItem"]) {
isSectionHeader = YES;
}
}
if (isSectionHeader) {
// Store this as the current section header for subsequent items
currentSectionHeader = name;
}
// Skip disabled items - they won't be in the final menu
continue;
}
// Check if this enabled item has children (indicating it's a parent/submenu)
NSArray *childrenCheck = nil;
AXUIElementCopyAttributeValue((AXUIElementRef)child, kAXChildrenAttribute, (CFTypeRef *)&childrenCheck);
BOOL hasChildren = [childrenCheck count] > 0;
[childrenCheck release];
// For enabled items, build the parent path including any section header
NSString *effectiveParentName;
if (currentSectionHeader != nil && !hasChildren) {
// Include the section header in the parent path
// But only if this item doesn't have children (not a parent item itself)
NSString *baseParent = _parentName != nil ? (parentName != nil ? [NSString stringWithFormat:@"%@ ▸ %@", parentName, _parentName] : _parentName) : parentName;
effectiveParentName = baseParent != nil ? [NSString stringWithFormat:@"%@ ▸ %@", baseParent, currentSectionHeader] : currentSectionHeader;
} else {
effectiveParentName = _parentName != nil ? (parentName != nil ? [NSString stringWithFormat:@"%@ ▸ %@", parentName, _parentName] : _parentName) : parentName;
// Clear section header if this item has children (it's a new parent section)
if (hasChildren) {
currentSectionHeader = nil;
}
}
[menuItems addObjectsFromArray:MenuItemsForElement((AXUIElementRef)child, depth - 1, name, effectiveParentName, menuIgnoreDepth - 1, process)];
}
}
[_parentName release];
[children release];
return [[menuItems autorelease] array];
}
- (QSObject *)appMenus:(QSObject *)dObject pickItem:(QSObject *)iObject{
return [self pickUIElement:iObject];
}
- (BOOL)bypassValidation {
NSDictionary *appDictionary = [[NSWorkspace sharedWorkspace] activeApplication];
NSString *identifier = [appDictionary objectForKey:@"NSApplicationBundleIdentifier"];
if ([identifier isEqualToString:kQSBundleID])
return YES;
else
return NO;
}
- (NSArray *)menuItemsForObject:(QSObject *)dObject {
if ([dObject objectForType:kWindowsType]) {
// It's a window we're showing the menu items for, so activate that window first (but not the app)
// Each window may have different menu items, so this is important
[self activateWindow:[dObject objectForType:kWindowsType] andProcess:nil];
NSRunningApplication *process = [dObject objectForType:kWindowsProcessType];
dObject = [QSObject fileObjectWithFileURL:[process bundleURL]];
}
dObject = [self resolvedProxy:dObject];
id process = [dObject objectForType:QSProcessType];
if ([process isKindOfClass:[NSDictionary class]]) {
process = [NSRunningApplication runningApplicationWithProcessIdentifier:[[process objectForKey:@"NSApplicationProcessIdentifier"] intValue]];
}
AXUIElementRef app=AXUIElementCreateApplication ([process processIdentifier]);
if (!app) {
return nil;
}
AXUIElementRef menuBar;
AXUIElementCopyAttributeValue (app, kAXMenuBarAttribute, (CFTypeRef *)&menuBar);
CFRelease(app);
if (!menuBar) {
return nil;
}
NSArray *actions=MenuItemsForElement(menuBar, 6, nil, nil, 2, process);
CFRelease(menuBar);
return actions;
}
- (QSObject *)searchAppMenus:(QSObject *)dObject{
NSArray *items = [self menuItemsForObject:dObject];
[QSPreferredCommandInterface showArray:[[items mutableCopy] autorelease]];
return nil;
}
NSArray *WindowDictsForApp(NSRunningApplication *process) {
// get a list of this application's windows
CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
__block NSMutableArray *appWindowDicts = [NSMutableArray array];
[(NSArray *)windows enumerateObjectsUsingBlock:^(NSDictionary *info, NSUInteger idx, BOOL *stop) {
if ([(NSNumber *)[info objectForKey:(NSString *)kCGWindowOwnerPID] intValue] == [process processIdentifier]) {
[appWindowDicts addObject:info];
}
}];
CFRelease(windows);
return [[appWindowDicts copy] autorelease];
}
NSArray *WindowsForApp(NSRunningApplication *process, BOOL appName) {
pid_t pid = [process processIdentifier];
AXUIElementRef appElement = AXUIElementCreateApplication(pid);
if (!appElement) {
return nil;
}
NSArray *appWindows = nil;
AXUIElementCopyAttributeValue(appElement, kAXWindowsAttribute, (CFTypeRef *)&appWindows);
CFRelease(appElement);
__block NSMutableArray *windowObjects = [NSMutableArray array];
NSArray *appWindowDicts = WindowDictsForApp(process);
[appWindows enumerateObjectsUsingBlock:^(id aWindow, NSUInteger idx, BOOL *stop) {
NSString *windowTitle = nil;
AXUIElementCopyAttributeValue((AXUIElementRef)aWindow, kAXTitleAttribute, (CFTypeRef *)&windowTitle);
if (windowTitle && windowTitle.length) {
QSObject *object = [QSObject objectForWindow:aWindow name:windowTitle process:process appWindows:appWindowDicts];
if (object) {
if (appName) [object setName:[windowTitle stringByAppendingFormat:@" (%@)",[process localizedName]]];
[object setObject:process forType:kWindowsProcessType];
[windowObjects addObject:object];
}
}
[windowTitle release];
}];
[appWindows release];
return windowObjects;
}
- (QSObject *)getWindowsForApp:(QSObject *)dObject
{
dObject = [self resolvedProxy:dObject];
id process = [dObject objectForType:QSProcessType];
if ([process isKindOfClass:[NSDictionary class]]) {
process = [NSRunningApplication runningApplicationWithProcessIdentifier:[[process objectForKey:@"NSApplicationProcessIdentifier"] intValue]];
}
NSArray *windowObjects = WindowsForApp(process, NO);
if (windowObjects) {
[QSPreferredCommandInterface showArray:[[windowObjects mutableCopy] autorelease]];
}
return nil;
}
- (QSObject *)showWindowApplication:(QSObject *)dObject {
NSRunningApplication *process = [dObject objectForType:kWindowsProcessType];
return [QSObject fileObjectWithFileURL:[process bundleURL]];
}
- (QSObject *)focusedWindowForApp:(QSObject *)dObject{
dObject = [self resolvedProxy:dObject];
id process = [dObject objectForType:QSProcessType];
if ([process isKindOfClass:[NSDictionary class]]) {
process = [NSRunningApplication runningApplicationWithProcessIdentifier:[[process objectForKey:@"NSApplicationProcessIdentifier"] intValue]];
}
AXUIElementRef appElement = AXUIElementCreateApplication([process processIdentifier]);
if (!appElement) {
QSShowAppNotifWithAttributes(QSUIAccessPlugIn_Type, NSLocalizedStringFromTableInBundle(@"UI Access Error", nil, [NSBundle bundleForClass:[self class]], nil), [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"Unable to get focused window for %@", nil, [NSBundle bundleForClass:[self class]], nil), [process localizedName]]);
return nil;
}
id focusedWindow = nil;
AXUIElementCopyAttributeValue(appElement, kAXFocusedWindowAttribute, (CFTypeRef *)&focusedWindow);
CFRelease(appElement);
[focusedWindow autorelease];
QSObject *window = [QSObject objectForWindow:focusedWindow name:nil process:process appWindows:WindowDictsForApp(process)];
[focusedWindow release];
return window;
}
- (QSObject *)appWindows:(QSObject *)dObject activateWindow:(QSObject *)iObject
{
return [self activateWindow:iObject];
}
- (QSObject *)activateWindow:(QSObject *)dObject {
dObject = [self resolvedProxy:dObject];
[self activateWindow:[dObject objectForType:kWindowsType] andProcess:[dObject objectForType:kWindowsProcessType]];
return nil;
}
-(void)activateWindow:(id)aWindow andProcess:(NSRunningApplication *)process {
if (!aWindow) {
return;
}
AXUIElementPerformAction((AXUIElementRef)aWindow, kAXRaiseAction);
if (process != nil) {
[process activateWithOptions:NSApplicationActivateIgnoringOtherApps];
}
}
- (QSObject *)raiseWindow:(QSObject *)dObject {
dObject = [self resolvedProxy:dObject];
id aWindow = [dObject objectForType:kWindowsType];
if (!aWindow) return nil;
AXUIElementPerformAction((AXUIElementRef)aWindow,kAXRaiseAction);
return nil;
}
void PressButtonInWindow(id buttonName, id window) {
AXUIElementRef button;
AXUIElementCopyAttributeValue((AXUIElementRef)window,(CFStringRef)buttonName, (CFTypeRef *)&button);
[(id)button autorelease];
AXUIElementPerformAction(button,kAXPressAction);
}
- (void)pressButton:(CFStringRef)button forObject:(QSObject *)obj {
for (QSObject *object in [obj splitObjects]) {
object = [self resolvedProxy:object];
id aWindow = [object objectForType:kWindowsType];
if (!aWindow) {
continue;
}
PressButtonInWindow((id)kAXCloseButtonAttribute, aWindow);
}
}
- (QSObject *)zoomWindow:(QSObject *)dObject {
[self pressButton:kAXZoomButtonAttribute forObject:dObject];
return nil;
}
- (QSObject *)minimizeWindow:(QSObject *)dObject {
[self pressButton:kAXMinimizeButtonAttribute forObject:dObject];
return nil;
}
- (QSObject *)closeWindow:(QSObject *)dObject {
[self pressButton:kAXCloseButtonAttribute forObject:dObject];
return nil;
}
- (QSObject *)allAppWindows:(QSObject *)dObject
{
NSArray *runningApps = [[NSWorkspace sharedWorkspace] runningApplications];
__block NSMutableArray *windows = [NSMutableArray array];
[runningApps enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSRunningApplication *anApp, NSUInteger idx, BOOL *stop) {
NSArray *windowObjects = WindowsForApp(anApp, YES);
if (windowObjects) [windows addObjectsFromArray:windowObjects];
}];
[QSPreferredCommandInterface showArray:windows];
return nil;
}
- (QSObject *)allAppMenus:(QSObject *)dObject
{
NSArray *launchedApps = [[NSWorkspace sharedWorkspace] runningApplications];
NSMutableArray *menus = [NSMutableArray array];
[launchedApps enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSRunningApplication *process, NSUInteger idx, BOOL * _Nonnull stop) {
AXUIElementRef app = AXUIElementCreateApplication([process processIdentifier]);
if (!app) {
return;
}
AXUIElementRef menuBar;
AXUIElementCopyAttributeValue(app, kAXMenuBarAttribute, (CFTypeRef *)&menuBar);
CFRelease(app);
if (!menuBar) {
return;
}
QSObject *object = [QSObject objectByMergingObjects:MenuItemsForElement(menuBar, 6, nil, nil, 2, process)];
CFRelease(menuBar);
[object setName:[process localizedName]];
[object setIcon:[process icon]];
[menus addObject:object];
}];
[QSPreferredCommandInterface showArray:menus];
return nil;
}
- (QSObject *)activeAppObject
{
NSDictionary *curAppInfo = [[NSWorkspace sharedWorkspace] activeApplication];
QSObject *curAppObject = [QSObject fileObjectWithPath:[curAppInfo objectForKey:@"NSApplicationPath"]];
[curAppObject setName:[curAppInfo objectForKey:@"NSApplicationName"]];
[curAppObject setObject:curAppInfo forType:QSProcessType];
return curAppObject;
}
- (QSObject *)focusedWindowObject
{
return [self focusedWindowForApp:[self activeAppObject]];
}
- (QSObject *)currentDocumentObject
{
return [self currentDocumentForApp:[self activeAppObject]];
}
- (id)resolveProxyObject:(id)proxy{
if ([[proxy identifier] isEqualToString:kCurrentFocusedWindowProxy]) return [self focusedWindowObject];
if ([[proxy identifier] isEqualToString:kCurrentDocumentProxy]) return [self currentDocumentObject];
return nil;
}
- (NSArray *)validIndirectObjectsForAction:(NSString *)action directObject:(QSObject *)dObject{
if ([action isEqualToString:@"QSPickMenuItemsAction"]){
NSArray *actions = [self menuItemsForObject:dObject];
return [NSArray arrayWithObjects:[NSNull null],actions,nil];
} else if ([action isEqualToString:@"ListWindowsForApp"]) {
dObject = [self resolvedProxy:dObject];
id process = [dObject objectForType:QSProcessType];
if ([process isKindOfClass:[NSDictionary class]]) {
process = [NSRunningApplication runningApplicationWithProcessIdentifier:[[process objectForKey:@"NSApplicationProcessIdentifier"] intValue]];
}
NSArray *windowObjects = WindowsForApp(process, NO);
return (windowObjects) ? [NSArray arrayWithObjects:[NSNull null],windowObjects,nil] : [NSArray array];
}
return nil;
}
- (NSArray *) validActionsForDirectObject:(QSObject *)dObject indirectObject:(QSObject *)iObject{
AXUIElementRef element= (AXUIElementRef)[dObject objectForType:kQSUIElementType];
NSArray *actions=nil;
AXUIElementCopyActionNames (element, (CFArrayRef *)&actions);
[(id)actions autorelease];
if ([actions containsObject:(NSString *)kAXPickAction])
return [NSArray arrayWithObject:@"QSUIElementPickAction"];
if ([actions containsObject:(NSString *)kAXPressAction])
return [NSArray arrayWithObject:@"QSUIElementPressAction"];
return nil;
}
- (QSObject *)uiElement:(QSObject *)dObject performAction:(QSObject *)iObject{
AXUIElementRef element= (AXUIElementRef)[dObject objectForType:kQSUIElementType];
[self activateProcessOfElement:element];
AXUIElementPerformAction (element, (CFStringRef)[iObject objectForType:kQSUIActionType]);
return nil;
}
- (QSObject *)pressUIElement:(QSObject *)dObject{
AXUIElementRef element= (AXUIElementRef)[dObject objectForType:kQSUIElementType];
[self activateProcessOfElement:element];
AXUIElementPerformAction (element, kAXPressAction);
return nil;
}
- (void)activateProcessOfElement:(AXUIElementRef) element{
pid_t pid=-1;
AXUIElementGetPid (element, &pid);
ProcessSerialNumber psn;
GetProcessForPID(pid,&psn);
SetFrontProcessWithOptions (&psn,kSetFrontProcessFrontWindowOnly);
}
- (QSObject *)pickUIElement:(QSObject *)dObject{
AXUIElementRef element= (AXUIElementRef)[dObject objectForType:kQSUIElementType];
[self activateProcessOfElement:element];
AXUIElementPerformAction (element,kAXPressAction);
return nil;
}
- (QSObject *)resolvedProxy:(QSObject *)dObject {
if ([dObject respondsToSelector:@selector(resolvedObject)]) {
return [dObject resolvedObject];
}
if ([dObject respondsToSelector:@selector(object)]) {
return [self resolvedProxy:[(QSRankedObject *)dObject object]];
}
return dObject;
}
- (QSObject *)currentDocumentForApp:(QSObject *)appObject
{
appObject = [self resolvedProxy:appObject];
id process = [appObject objectForType:QSProcessType];
if ([process isKindOfClass:[NSDictionary class]]) {
process = [NSRunningApplication runningApplicationWithProcessIdentifier:[[process objectForKey:@"NSApplicationProcessIdentifier"] intValue]];
}
void (^notifBlock)(void) = ^{
QSShowAppNotifWithAttributes(QSUIAccessPlugIn_Type, NSLocalizedStringFromTableInBundle(@"No Current Document", nil, [NSBundle bundleForClass:[self class]], nil), [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(@"Unable to select current document from %@", nil, [NSBundle bundleForClass:[self class]], nil), [process localizedName]]);
};
AXUIElementRef app = AXUIElementCreateApplication([process processIdentifier]);
AXUIElementRef window = nil;
AXUIElementCopyAttributeValue(app, kAXFocusedWindowAttribute, (CFTypeRef *)&window);
[(id)app release];
if (!window) {
notifBlock();
return nil;
}
QSObject *document = [self firstDocumentObjectForElement:window depth:3 title:nil];
if (!document) {
notifBlock();
}
CFRelease(window);
return document;
}
- (QSObject *)objectFromDocumentAttribute:(NSString *)path {
if (!path || !path.length) {
return nil;
}
// check if it's a valid path (exists)
if ([[NSFileManager defaultManager] fileExistsAtPath:[path stringByExpandingTildeInPath]]) {
return [QSObject fileObjectWithPath:[[NSURL URLWithString:path] path]];
} else if ([NSURL URLWithString:path]) {
// consider it as a normal URL
return [QSObject URLObjectWithURL:path title:nil];
}
return nil;
}
- (QSObject *)firstDocumentObjectForElement:(AXUIElementRef)element depth:(NSInteger)depth title:(NSString *)title
{
QSObject *object = nil;
if (depth != 0) {
NSString *currentPath = nil;
AXUIElementCopyAttributeValue(element, kAXDocumentAttribute, (CFTypeRef *)¤tPath);
[currentPath autorelease];
object = [self objectFromDocumentAttribute:currentPath];
if (object) {
return object;
}
if (!title)
{
AXUIElementCopyAttributeValue(element, kAXTitleAttribute, (CFTypeRef *)&title);
[title autorelease];
}
NSURL *currentURL = nil;
AXUIElementCopyAttributeValue(element, kAXURLAttribute, (CFTypeRef *)¤tURL);
[currentURL autorelease];
if (currentURL)
{
object = [self objectFromDocumentAttribute:[currentURL path]];
if (object) {
return object;
}
}
NSArray *children = nil;
AXUIElementCopyAttributeValue(element, kAXChildrenAttribute, (CFTypeRef *)&children);
[children autorelease];
if ([children count] == 0) return nil;
// First pass: check immediate children for standard attributes
for (id child in children)
{
AXUIElementCopyAttributeValue((AXUIElementRef)child, kAXDocumentAttribute, (CFTypeRef *)¤tPath);
[currentPath autorelease];
object = [self objectFromDocumentAttribute:currentPath];
if (object) {
return object;
}
AXUIElementCopyAttributeValue((AXUIElementRef)child, kAXURLAttribute, (CFTypeRef *)¤tURL);
[currentURL autorelease];
if (currentURL)
{
object = [self objectFromDocumentAttribute:currentPath];
if (object) {
return object;
}
}
}
// Second pass: search deeply for file paths in titles/labels (for Electron apps like VSCode)
QSObject *deepDoc = [self searchDeepForFilePathInElement:element maxDepth:30];
if (deepDoc) return deepDoc;
for (id child in children) {
QSObject *childDoc = [self firstDocumentObjectForElement:(AXUIElementRef)child depth:depth - 1 title:title];
if (childDoc) return childDoc;
}
}
NSBeep();
return nil;
}
- (QSObject *)searchDeepForFilePathInElement:(AXUIElementRef)element maxDepth:(NSInteger)maxDepth
{
if (maxDepth <= 0) return nil;
// Check this element's title and label for file paths
NSString *titleValue = nil;
AXUIElementCopyAttributeValue(element, kAXTitleAttribute, (CFTypeRef *)&titleValue);
[titleValue autorelease];
QSObject *result = [self extractFilePathFromString:titleValue];
if (result) return result;
// Also check the label attribute (VSCode uses this for tab labels)
NSString *labelValue = nil;
AXUIElementCopyAttributeValue(element, kAXDescriptionAttribute, (CFTypeRef *)&labelValue);
[labelValue autorelease];
result = [self extractFilePathFromString:labelValue];
if (result) return result;
// Check value attribute as well
NSString *valueString = nil;
AXUIElementCopyAttributeValue(element, kAXValueAttribute, (CFTypeRef *)&valueString);
[valueString autorelease];
result = [self extractFilePathFromString:valueString];
if (result) return result;
// Recurse into children
NSArray *children = nil;
AXUIElementCopyAttributeValue(element, kAXChildrenAttribute, (CFTypeRef *)&children);
[children autorelease];
for (id child in children)
{
result = [self searchDeepForFilePathInElement:(AXUIElementRef)child maxDepth:maxDepth - 1];
if (result) return result;
}
return nil;
}
- (QSObject *)extractFilePathFromString:(NSString *)string
{
if (![string isKindOfClass:[NSString class]] || !string || string.length == 0) return nil;
// VSCode format: "~/path/to/file.ext • 23 problems in this file • Modified"
// Extract the part before the first bullet point
NSRange bulletRange = [string rangeOfString:@" •"];
if (bulletRange.location != NSNotFound) {
string = [string substringToIndex:bulletRange.location];
}
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// Check if it looks like a file path (starts with / or ~/)
if (![string hasPrefix:@"/"] && ![string hasPrefix:@"~/"]) {
return nil;
}
if ([string isEqualToString:@"/"]) {
return nil;
}
// Expand tilde
NSString *expandedPath = [string stringByExpandingTildeInPath];
// Verify the file exists
if ([[NSFileManager defaultManager] fileExistsAtPath:expandedPath]) {
return [QSObject fileObjectWithPath:expandedPath];
}
return nil;
}
@end