2014-10-29 60 views
1

我有以下处理程序;Applescript:无需打开即可获取.app的路径

on getAppPath(appName) 
    try 
    return POSIX path of (path to application appName) 
    on error 
    return "NOT INSTALLED" 
    end try 
end getAppPath 

当用例如“ImageOptim”调用时,将返回“/Applications/ImageOptim.app/”。

我遇到的问题是,这会打开我的Dock中的应用程序,有没有办法让这个路径字符串没有发生?

谢谢。

回答

2

路径是在Standard Additions中,它必须启动应用程序以获取返回值(除了Apple的某些应用程序 - 例如TextEdit)。一个想法是查询启动服务注册表,其中包含所有可执行文件的记录,并使用grep来提取与您指定的应用程序名称相匹配的路径。喜欢的东西:

getAppPath("TextEdit.app") 

on getAppPath(appName) 
    try 
     set launchServicesPath to "/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister" 

     -- get the path to all executables in the Launch Service Registry that contain that appName 
     set appPaths to paragraphs of (do shell script launchServicesPath & " -dump | grep --only-matching \"/.*\\" & appName & "\"") 
     return appPaths 

    on error 
     return "NOT INSTALLED" 
    end try 
end getAppPath 

注意,这可以返回的路径列表,因为有可能是多个可执行文件与匹配的字符串应用程序的名称。所以,你会想说明这一点。

相关问题