AppleScript : tell running application from set of candidate names

278 Views Asked by At

We have published several different versions of an app. They have similar names – Foo Basic, Foo Deluxe, Foo Retro, etc. Similar but different bundle identifiers too. (It wasn't my idea!) Some users have more than one of these apps installed, but only one can be running.

All apps support the same AppleScript dictionary. I need an AppleScript to script the currently-running version of our app to do stuff. How can I do this?

1

There are 1 best solutions below

0
On BEST ANSWER

I got it working. It required several pieces:

  • Get the name of the running app. You can do this with either processes of System Events or else do shell script "ps …", whichever you think will be more reliable in your situation.
  • Then, using terms from one of the apps on your Mac, you can
  • *tell application appName…, provided that you have
  • saved your script as an Application, so it is already compiled.

Here is some code. The script I'm working on has not yet resorted to System Events, so to spare new users the pain of a trip to System Preferences, I chose to use /bin/ps instead…

set appName to runningAppWithBaseName("Foo")
using terms from application "Foo Deluxe" -- an app on your Mac
    tell application appName
       (* whatever code you want here *)
    end tell
end using terms from

on runningAppWithBaseName(baseName)
    set command to "/bin/ps -eo args | grep " & baseName & " | grep -v grep"
    (* The "grep -v grep" is to exclude the grep process itself from the results. *)

    try 
        set fullPathOfRunningApp to do shell script command
    end try

    (* Here, given the fullPathOfRunningApp and your list of candidate app names, *)
    (* insert  code to determine the name of the running app. *)
    return nameOfRunningApp
end runningAppWithBaseName