Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applescript: Get path to .app without opening it

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.

like image 763
Jamie Mason Avatar asked Jun 30 '26 14:06

Jamie Mason


1 Answers

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.

like image 129
jweaks Avatar answered Jul 02 '26 04:07

jweaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!