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