Use the target app’s bundle identifier (more flexible)
Do you plan to keep only one single copy of MyApp.app
on the entire system?
If so, you can take advantage of MyApp.app
’s bundle identifier, and have Launch Services figure out all the nitty-gritty details for you.
This solution is rather flexible; it has the benefit that you can put your Automator workflow wherever you want – it does not have to be in the same location as MyApp.app
, nor in any fixed location relative to it.
Steps
These are the steps you want to take:
Open Terminal.app and run the following command:
osascript -e 'id of app "'"$(mdfind MyApp.app)"'"'
The previous step should have printed out one single line as a result. Note what that line says (e. g. com.example.MyApp
).
Open your workflow in Automator.
Add a Run Shell Script action with the content:
open -b com.example.MyApp
Save the Automator workflow in any folder at your discretion – no need to put it in a fixed location relative to MyApp.app
.
Run the Automator workflow; it should launch MyApp.app
.
Explanation
Launch services
This solution takes advantage of a part of macOS called Launch Services, which maintain a system-wide database of apps and their properties:
Whenever a new application becomes known to the system (such as when the user drags it from an installation disk into the Applications folder), the application is registered with Launch Services, which copies the needed information about the application into its database. Launch Services can then use this information to determine the preferred application for opening a given document file or URL.
The plan
The piece of information we’re interested in is the bundle identifier, or bundle ID.
An App developer typically assigns to each of her or his apps a (hopefully) globally unique bundle ID. We can take advantage of this uniqueness if and only if there is no more then one single app with the same bundle ID on our whole system – and only one single copy of that app. No duplicates.
Figuring out the path
The command
mdfind MyApp.app
is just a fancy way to run a Spotlight search as a one-time shortcut in order to figure out the app’s path as of right now. We need the path only once in order to extract the app’s bundle identifier.
Spotlight will (hopefully) return with something like this:
/Users/watermelon/foo/MyApp.app
Extracting CFBundleIdentifier
Wrapping the app’s current path in the following AppleScript command:
id of app "/Users/watermelon/foo/MyApp.app"
is simply a convenient way to **extract the CFBundleIdentifier
** from the MyApp.app
bundle.
(You could have also looked up the bundle ID manually by right-clicking the app in Finder, choosing Show Package content, navigating to the Contents
subfolder and opening Contents/Info.plist
in TextEdit, search for the key that says CFBundleIdentifier
and find its String
value one line further down. I think this is tedious, therefore I always prefer to use the AppleScript/mdfind
snippet.)
Launching an app by bundle ID
Finally, let’s have a look at the command you’re actually going to use in your Automator workflow:
open -b com.example.MyApp
This command is just a technical way of saying “Hey macOS, you’re good friends with launchd
, right? Can you have it launch this app for me? Oh btw, I never bothered to look up the full path to it. What I do know though is its CFBundleIdentifier
, which is com.example.MyApp
; just go ask Launch Services and they will figure out the path”.
/Applications
and protected by SIP. You can verify this by runninggrep Automator /System/Library/Sandbox/rootless.conf
, orls -ldAO@ /Applications/Automator.app
. So why not simply hard-code/Applications
?~/foo
and an app~/foo/MyApp.app
. Your goal is to have an Automator workflow to act as a buddy toMyApp.app
, and lives in the same directory asMyApp.app
at all times. You want to be able to moveMyApp.app
at your discretion, into whatever folder you like, for example~/bar
– as long as the buddy workflow travels along with it. Lastly, you want to be able to launch the buddy workflow from any location, for example from the Finder, or usingopen ~/bar/MyWorkflow.workflow
while you’re inside/tmp/meow
. Correct?