Applescript to find application path from partial app name?

How do I get the path to a running application using part of the app name. (eg. 'Acrobat' in 'Adobe Acrobat Reader DC') This is what I tried but it's incorrect. (Note: Not sure if it changes if multiple instances are running, but I only want a single POSIX path returned)

tell application "System Events",
  if (get name of every application process) contains "Acrobat" then
    return POSIX path of (path to ((get name of every application process) contains "Acrobat"))
  end if
end tell

Solution 1:

The following example AppleScript code assumes you only have one application running that contains "Acrobat" in its name, otherwise you'll need to process the list returned from System Events in a repeat loop.

tell application "System Events" to ¬
    set appAcrobatList to the ¬
        name of every application process ¬
        whose background only is false ¬
        and name contains "Acrobat"

if appAcrobatList is not {} then ¬
    return POSIX path of ¬
        (path to application ¬
            (first item of appAcrobatList))

Solution 2:

This following AppleScript solution will return the path of the app if it is running. It will also return the paths of all running instances of the app, if there is more than one (without the need for a repeat loop).

property singleAppPath : missing value
property multipleAppPaths : missing value

tell application "System Events"
    set searchedApps to a reference to ¬
        ((every application process) whose name contains "Acrobat" or ¬
            displayed name contains "Acrobat")
    if (count of searchedApps) is 1 then
        set singleAppPath to POSIX path of application file of searchedApps as text
    else if (count of searchedApps) > 1 then
        set multipleAppPaths to POSIX path of application file of searchedApps
    end if
end tell

I do realize that the following code isn’t really a direct solution to the OP’s question. However, it occurred to me that, once the full file path of the Application is retrieved from the above code in my solution… other people, including myself, may find it valuable to have an option to reveal the application/s in Finder. This becomes especially handy if the application process which you search for does not come from an application located in your /Applications folder.

property searchedApps : missing value
property singleAppPath : missing value
property multipleAppPaths : missing value

activate
set theAppProcess to (display dialog ¬
    "Reveal App From App Process Search" default answer ¬
    "Search For ..." buttons {"Cancel", "Get Full Path", "Reveal In Finder"} ¬
    default button 3 cancel button 1 ¬
    with title "Locate Application Process" giving up after 45)

if button returned of theAppProcess is "Get Full Path" then
    getFullAppPathFromSearchedProcesses(text returned of theAppProcess)
    if singleAppPath ≠ missing value then
        activate
        display dialog singleAppPath buttons {"OK"} default button "OK"
    end if
else if button returned of theAppProcess is "Reveal In Finder" then
    getFullAppPathFromSearchedProcesses(text returned of theAppProcess)
    revealApp()
end if

on revealApp()
    if (count of searchedApps) is 1 then
        do shell script "open -R " & quoted form of singleAppPath
    else if (count of searchedApps) > 1 then
        repeat with thisItem in multipleAppPaths
            do shell script "open -R " & quoted form of thisItem
            delay 0.1
        end repeat
    end if
end revealApp

on getFullAppPathFromSearchedProcesses(appName)
    tell application "System Events"
        set searchedApps to a reference to ¬
            ((every application process) whose name contains appName or ¬
                displayed name contains appName)
        if (count of searchedApps) is 1 then
            set singleAppPath to POSIX path of application file of searchedApps as text
        else if (count of searchedApps) > 1 then
            set multipleAppPaths to POSIX path of application file of searchedApps
        end if
    end tell
    return {singleAppPath, multipleAppPaths}
end getFullAppPathFromSearchedProcesses

Solution 3:

This isn't quite what you asked, but if you happen to know the app's bundle identifier, there's a very simple way to get the path:

tell application "Finder"
    return POSIX path of (application file id "com.adobe.Acrobat" as text)
end tell

Because this uses LaunchServices, if multiple copies of the app exist, it will always return the copy that Apple considers most important—ie, the version your Mac will actually launch.