2010-07-30 66 views
3

我試圖從另一個在後臺運行的可可應用程序獲得焦點的查找程序窗口的當前目錄。我知道,它可以使用像一個AppleScript來完成:從cocoa應用程序獲取Finder窗口的當前目錄

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 

但是我想知道是否有任何使用無障礙API或可能System Event一些其他的UI腳本做了一些比較通用的方法是什麼?

我試過NSAccessibilityDocumentAttributeNSAccessibilityURLAttribute這樣的屬性,但沒有設置。從其他大多數基於文檔的應用程序,這工作得很好,但不適合finder或terminal.app。

+0

@ regulus6633(我可以直接添加評論到您的文章,所以我給加上去的)感謝您的帖子,但同樣給Brian韋伯斯特,我的問題不是如何運行可可內的applescript,但是這些是否是一種通用的方式,如何使用可訪問性API或applescript從查找程序或終端應用程序中獲取當前工作目錄。現在我認爲所有這些都必須作爲特例處理。 – fikovnik 2010-08-02 00:42:56

回答

2

看看Scripting Bridge framework,這可能是直接從您的Cocoa應用程序獲取您想要的信息的最簡單方法。

+0

由於我沒有選票,我幾乎會給答案+1。 – kiamlaluno 2010-07-31 16:46:22

+0

我想我應該表達自己更好。我知道如何從可可內部執行applescript,問題是是否有辦法如何獲取finder可訪問性API的當前目錄,或者是否有更通用的方式來獲取這些信息。 – fikovnik 2010-08-02 00:38:48

1

@nkuyu,我剛剛看到你的評論,你知道如何運行一個applescript ......但對於其他人不(可能會偶然發現這篇文章)我會解釋。

使用NSApplescript很容易從objc運行applescript。如果你從你的applescript中返回一個字符串,得到結果會更容易,因爲你可以從NSAppleEventDescriptor中獲得「stringValue」。因此,我回到了蘋果的「posix路徑」。請注意,NSApplescript不是線程安全的,因此在多線程應用程序中,您必須注意始終在主線程上運行NSApplescript。試試這個...

-(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); 
} 
3
// 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); 
+0

所有這些都可以在沙箱下運行嗎?我記得有關Apple Events/Scripting的限制閱讀 – cacau 2015-01-28 07:52:45

+0

您可能需要添加適當的權利... – geowar 2015-01-28 16:39:20

+0

FinderApplication,FinderWindow和FinderFolder在哪裏定義?在Xcode文檔或Apple開發人員網站上搜索時沒有返回結果。此外,SBApplication安裝程序行將返回一個不是FinderApplication的SBApplication實例。至少它在我的Xcode 8.0上執行... – Todd 2016-12-31 13:45:56