Applescript:无需打开即可获取到.app的路径 [英] Applescript: Get path to .app without opening it

查看:353
本文介绍了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/".

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

我的问题是,这会在我的Dock中打开该应用程序,是否有办法在没有发生这种情况的情况下获取此路径字符串?

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?

谢谢.

推荐答案

标准添加中的路径,它必须启动应用程序以获取返回值(某些Apple应用程序除外-例如,请参见TextEdit) .一种想法是查询启动服务注册表,其中包含所有可执行文件的记录,并使用grep提取与您指定的应用程序名称匹配的路径.像这样:

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.

这篇关于Applescript:无需打开即可获取到.app的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆