Automator use current directory location

automatorbash

my goal is to use Automator to open an application with arguments

open ./MyApp.app -arguments

via running a shareable Buddy.app with Myapp.app (the Automator/workflow). both apps should be shareable together with anyone so they run straight off.

the simplest way to do so, seems to be to run the Buddy.app from within the same folder as MyApp.app.
then i can move the Buddy.app with MyApp.app anywhere, and they will still work together

to set up the relative file locations in bash script,

open "$(pwd)"/myapp.app -arguments

only problem is that Automator uses the home directory instead of the application directory.
ie pwd is the home directory, and not the actual file location.

in Automator the output from

echo $(pwd)

is my home directory, /Users/User

so the question is, how to get Automator (Buddy.app) to detect the current folder location and run MyApp.app ?

Best Answer

Have your workflow figure out its own path (more robust)

This works as long as your Automator workflow lives in the same directory as MyApp.app; you can create copies of either the workflow or the app at your discretion and reuse those copies wherever you want; nothing needs to be unique.

Steps

These are the steps to automate launching MyApp.app:

  1. Open the workflow in Automator.

  2. Add a Run AppleScript action. Remove all the boilerplate code inside and replace it with the following lines:

    set AppleScript's text item delimiters to ":"
    set appPath to (text items 1 through -3 of (path to me as text) & {"MyApp.app"} as text)
    tell application "Finder" to open file appPath
    
  3. Save the Automator workflow in the same folder as MyApp.app.

  4. Run the Automator workflow; it should launch MyApp.app.

Explanation

set AppleScript's text item delimiters to ":"

This means “Watch out, I’m going to split a string soon; and I want you to use the : character as the split boundaries.”

path to me as text

This instructs the workflow to figure out the path to itself; path components are delimited by a colon (:), which is a remnant of Classic Mac OS.

text items 1 through -3 of […] & {"MyApp.app"}

This cuts off the last part of the path, and appends MyApp.app to the path.

[…] as text

This pieces the path back together (again with : as a delimiter).

tell application "Finder" to open file appPath

Lastly, this final line causes MyApp.app to launch.