Applescript: Get path to .app without opening it

China☆狼群 提交于 2019-12-24 04:33:16

问题


I have the following handler;

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

Which when called with eg "ImageOptim" will return "/Applications/ImageOptim.app/".

The problem I have is that this opens that application in my Dock, is there a way to get this path string without that happening?

Thanks.


回答1:


path to is in Standard Additions and it must launch the app to get the return value (with the exception of some of Apple's apps-- see for example TextEdit). One idea is to query the Launch Services Registry, which has records of all executables, and the use grep to pull the path that matches the app name you're specifying. Something like:

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

Note that this can return a list of paths, since there could be more than one executable with a matching string app name. So, you'll want to account for that.



来源:https://stackoverflow.com/questions/26636860/applescript-get-path-to-app-without-opening-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!