I've been running into a rut recently with one project I've been trying to accomplish. Using Automator, but I don't know how to exactly to go about this project. There's this script editing app called Brackets, and I want to make it so whenever the app is opened, the shell command known as tty.js is executed. Any help I can get would be great
2 Answers
What you can try is going to the application's .app folder and renaming the binary, creating a script file (marked executable) named as the executable used to be which will contain a reference the now-renamed executable along with whatever you are scripting. e.g.
cd /Applications/Brackets.app/Contents/MacOS
mv Brackets BracketsReal
echo "tty.js" > Brackets
echo "BracketsReal &" >> Brackets
chmod +x Brackets
That should create a text script that launches your script and the app afterwards...
edit
scripts in Appname.app/Contents/MacOS/ folder are supported.
The first line #!/bin/sh (aka shebang) is required
The technique to move the real executable and make it execute from a script is called wrapping and is also used from applications like Gimp or XQuartz (Xorg/X11) on MacOS.
If I understand correctly what the user was trying to achieve, run Bracket and automatically open the file tty.js, apart the missing shebang, need some other consideration.
To correctly wrap BracketsReal, Brackets should be something like:
#!/bin/sh
exec "$(dirname "$0")/BracketsReal"
That's should run as if we never moved Brackets to BracketsReal.
exec is a built-in command of the shell
$0 is a special argument passed from the system to the executed shell script, when executed with a click should be the full path to the script:
/Applications/Brackets.app/Contents/MacOS/Brackets
dirname is a shell command to get only the directory part without the file name, $( ) is a command substitution, once executed the line will become:
exec /Applications/Brackets.app/Contents/MacOS/BracketsReal
So much so we can move the application from /Applications in other folders and still should run it.
At this point we should check that BracketsReal (probably a script itself) accept files to open as arguments on the command line (and eventually if require some special syntax) if it does accept arguments then the line would become something like:
exec "$(dirname "$0")/BracketsReal" "/path/to/the/file/tty.js"
Last note, to check if something is wrong with the wrapper script just open a terminal and run the wrapper, at the terminal prompt just type/paste:
/Applications/Brackets.app/Contents/MacOS/Brackets
privilegies or syntax errors are printed in the terminal window.
-
Thanks! I had one problem though. When running those commands in terminal, I kept getting the message "-bash: !/bin/sh": event not found" after trying to execute "echo "#!/bin/sh" > Brackets" – VEDA0095 May 22 '14 at 23:42
-
Aha, ok then omit that line. Edit the file in a text editor and delete that top line. Response edited. – EkriirkE May 23 '14 at 01:08
-
I have one other issue as well. Whenever I type in the "mv" command I get this as well "mv: rename Brackets to BracketsReal: No such file or directory" I have no idea what to do at this point. – VEDA0095 May 23 '14 at 02:32
-
I haven't been on OSX in a while, sorry. Try: `cd /Applications/Brackets.app/Contents/MacOS` first – EkriirkE May 23 '14 at 02:45
-
It's all good. Thanks for all your help though. Unfortunately, I just get this message everytime I try to open the app now after the changes. "You can’t open the application “Brackets” because it is not supported on this type of Mac." – VEDA0095 May 23 '14 at 02:54
-
Grr. To change it back, do this: `cd /Applications/Brackets.app/Contents/MacOS ; cp BracketsReal Brackets` – EkriirkE May 24 '14 at 02:37
-
1Thanks to @EkriirkE's answer I discovered that scripts were supported, so added some info, I'm still a MacOS newbie – Alex Sep 14 '14 at 02:52
Keyboard Maestro has an option to run a macro when an application is launched:

You might also assign a shortcut to a script like open -a Brackets;tty.js and then always use that script to open Brackets.