How to Run Shell Script upon Application Launch

automatorcommand linerubyscripttextmate

Not sure if this belongs here or on SuperUser, but it's Mac specific, so I'll go with here.

I wrote a Ruby script that edits TextMate's global properties file and changes the theme based on the time of day (light theme during the day, dark theme at night), and then launches TextMate.

I then made an Automator app that runs that script. I called it TextMate, pasted in the flower icon, and put it in my dock instead of the actual TextMate app.

The script works, but the big obvious thing I overlooked is that the real TextMate is no longer in my dock, so whenever I click my Automator version, it runs, exits, and then TextMate appends itself to the running applications section of my dock, so I have two flower icons down there when the app is running. Another problem is that if I opened the app in any other way (like opening a file from Finder), the script won't run.

So, is there any way to attach my script to the actual app, or tell the system to do something any time TextMate opens, or tell TextMate to do something any time it opens?

Best Answer

The third-party UI-scripting application Keyboard Maestro (KBM) can perform this function. (Along with many others.)

I've been using KBM for 5 or 6 years, it's very stable and reliable.

See screenshot of sample KBM script:

enter image description here