【问题标题】:ScriptingBridge Finder POSIX pathScriptingBridge Finder POSIX 路径
【发布时间】:2010-06-20 02:29:41
【问题描述】:

是否可以使用 Scripting Bridge 框架获取 POSIX 路径或目标到最前面的窗口?

我正在使用

FinderApplication *theFinder = [SBApplication aplicationWithBundleIdentifier:@"com.apple.Finder";

但我在“Finder.h”中找不到任何可以工作的东西。

【问题讨论】:

    标签: objective-c finder scripting-bridge


    【解决方案1】:

    这可能是你使用 ScriptingBridge 和 NSURL 之后的样子

    FinderApplication *finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
    
    SBElementArray *windows =  [finder windows ]; // array of finder windows
    NSArray *targetArray = [windows arrayByApplyingSelector:@selector(target)];// array of targets of the windows
    
    //gets the first object from the targetArray,gets its URL, and converts it to a posix path
    NSString * newURLString =   [[NSURL URLWithString: (id) [[targetArray   objectAtIndex:0]URL]] path];
    
    NSLog(@"newURLString   %@  ", newURLString);
    

    【讨论】:

    • 是的,好主意,正是我需要的。谢谢。
    • 很高兴它有帮助,记得将它放在类似 try / catch 块中,否则如果没有打开任何窗口,您将收到“超出范围”错误。
    【解决方案2】:

    通过appscript's ASTranslate 工具运行drawonward 的代码给了我这个:

    #import "FNGlue/FNGlue.h"
    FNApplication *finder = [FNApplication applicationWithName: @"Finder"];
    FNReference *ref = [[[finder windows] at: 1] target];
    FNGetCommand *cmd = [[ref get] requestedType: [ASConstant alias]];
    id result = [cmd send];
    

    结果将是一个 ASAlias 实例;使用 -[ASAlias path] 获取 POSIX 路径。

    您无法在 SB 中使用原始 Apple 事件代码,因为这是 Apple 工程师忘记/懒得放入 SB 的 less than stellar API 的功能之一。

    【讨论】:

    • 附录:Finder 的设计者足够优秀,可以包含一个URL 属性,该属性将为您提供一个文件 URL 字符串。稍微摆弄一下,您可能会说服 SB 检索该值,此时您可以使用 NSURL 将其转换为 POSIX 路径字符串。
    【解决方案3】:

    我没有使用过 ScriptingBridge。作为 NSAppleScript 的一部分,它将是:

    get POSIX path of (target of window 1 as alias)
    

    希望这会有所帮助。我认为 POSIX 部分来自 StandardAdditions ScriptingAddition,而不是 Finder 本身。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多