简体   繁体   English

从cocoa应用程序获取Finder窗口的当前目录

[英]Get current directory of a Finder window from a cocoa application

I'm trying to get a current directory of a finder window that is in focus from another cocoa application that is running in a background. 我正在尝试从后台运行的另一个cocoa应用程序获取焦点窗口的当前目录。 I know that it can be done using an applescript like: 我知道可以使用如下的AppleScript来完成:

tell application "Finder"
try
  set dir to (the target of the front window) as alias
on error
  set dir to startup disk
end try
end tell

However I was wondering whether there is some more generic way of doing it either using the accessibility API or some other UI scripting with perhaps System Event ? 但是我想知道是否有一些更通用的方法可以使用辅助功能API或其他一些UI脚本与System Event

I tried attributes like NSAccessibilityDocumentAttribute or NSAccessibilityURLAttribute but none is set. 我尝试过像NSAccessibilityDocumentAttributeNSAccessibilityURLAttribute这样的属性,但没有设置。 From other mostly document based applications this works pretty well, but not for finder nor for terminal.app. 从其他大多数基于文档的应用程序,这非常有效,但不适用于finder或terminal.app。

    // this is the finder
    FinderApplication * finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.Finder"];
    // get all the finder windows
    SBElementArray * finderWindows = finder.FinderWindows;
    // this is the front window
    FinderWindow * finderWindow = finderWindows[0];
    // this is its folder
    FinderFolder * finderFolder = finderWindow.properties[@"target"];
    // this is its URL
    NSString * finderFolderURL = finderFolder.URL;
    NSLog(@"front window URL: %@", finderFolderURL);

看一下Scripting Bridge框架 ,这可能是直接从Cocoa应用程序获取所需信息的最简单方法。

@nkuyu, I just saw your comment that you know how to run an applescript... but for others who don't (and might stumble onto this post) I'll explain. @nkuyu,我刚看到你的评论,你知道如何运行一个苹果...但对于那些没有(并可能偶然发现这篇文章)的人,我会解释。

It's easy to run an applescript from objc using NSApplescript. 使用NSApplescript从objc运行applescript很容易。 And if you return a string from your applescript it's even easier to get a result because you can get the "stringValue" from the NSAppleEventDescriptor. 如果从AppleScript返回一个字符串,则更容易获得结果,因为您可以从NSAppleEventDescriptor获取“stringValue”。 As such I return "posix paths" from the applescript. 因此,我从AppleScript返回“posix路径”。 Note that NSApplescript is not thread-safe so in multi-threaded apps you must take care to always run it on the main thread. 请注意,NSApplescript不是线程安全的,因此在多线程应用程序中,您必须注意始终在主线程上运行它。 Try this... 试试这个...

-(IBAction)runApplescript:(id)sender {
    [self performSelectorOnMainThread:@selector(getFrontFinderWindowTarget) withObject:nil waitUntilDone:NO];
}

-(void)getFrontFinderWindowTarget {
    NSString* theTarget = nil;
    NSString* cmd = @"tell application \"Finder\"\ntry\nset dir to the target of the front window\nreturn POSIX path of (dir as text)\non error\nreturn \"/\"\nend try\nend tell";
    NSAppleScript* theScript = [[NSAppleScript alloc] initWithSource:cmd];

    NSDictionary* errorDict = nil;
    NSAppleEventDescriptor* result = [theScript executeAndReturnError:&errorDict];
    [theScript release];
    if (errorDict) {
        theTarget = [NSString stringWithFormat:@"Error:%@ %@", [errorDict valueForKey:@"NSAppleScriptErrorNumber"], [errorDict valueForKey:@"NSAppleScriptErrorMessage"]];
    } else {
        theTarget = [result stringValue];
    }
    [self getFrontFinderWindowTargetResult:theTarget];
}

-(void)getFrontFinderWindowTargetResult:(NSString*)result {
    NSLog(@"result: %@", result);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM