Tuesday, July 3, 2012

Launching and Quitting Applications on Mac OS X at Specific Times

So for about a year now I've been trying to find out ways to quit and launch apps on Mac OS X at specific times of the day. This is especially useful when I run BOINC because my computer never sleeps at night anymore. Initially it was just iTunes that I wanted to quit after a set number of minutes, and I would use this nifty little application called iTunesShut. Here's a quick screen shot of it.

Basically, as you can probably tell by the screenshot after a set amount of time or at the end of the playlist (or songs) you can tell iTunes to quit, pause, and even tell the whole system to shut down or sleep. However, this application is only limited to iTunes.

After a while I realized I wanted something that could launch or quit ANY application on a timely basis. I would try to google applications that could do this but I couldn't find a solution. However, yesterday I saw this application called Cronnix. The website describes this application as "a GUI frontend to the powerful Unix tool "cron". Cron is a Unix system service that allows scheduled execution of scripts, programs, applications - in short anything that can be started from the command line. This includes OSX applications and AppleScripts." In short, this can be used to launch applescripts, meaning that I can launch and quit applications at will. Now, while I dabble a bit into the tech world I am by no means sa programmer. So I continued to google for how to set up an applescript to launch an application.

Before I talk about how to setup an applescript I'm just going to quickly talk about Cronnix how to use it to launch scripts in the first place. So, when you launch the application it should look something like this, however, without the tasks.

To start a new task you click the "new" icon at the top left (fairly straightforward so far, right?). YOu will then be greeted with this screen. At first I was unsure what the screen was supposed to do (I had thought that cronnix was a timer based app, where I could execute a task after a set amount of time, but I was wrong).

This information is also in the cronnix help file as well (if you go to the help menu), but I'm going to go through it anyway. Minute and day are for the time of day you need to run the script. And it's 24 hour time, so just a quick note there. So 23:30 would be 11:30 pm and so on. As for the month and day, if you need to run the script every day make sure you check the box and you see the asterisk in the box. That means it'll run every day.

I have used applescripts in the past (at one point in time I wanted to launch all of the applications I used at once using an applescript. However, I quickly realized that the application would have to fully launch before the next one would run. That disappointed me at the time (where I wanted all of the applications to launch at the same time), so I abandoned applescript.

Back on topic, so I'm just going to quickly paste the commands for running and quitting a script. It's actually quite simple. First of all, launch the applescript editor.

For launching an application
tell application "[insert application of choice here]"
end tell

For quitting an application
tell application "[insert application of choice here]"
end tell

Here's a quick screenshot of how it looks like.

One thing I noticed was to run the script before hand to make sure it works. Sometimes the applescript editor is unsure of which application you are referring to and an extra dialog box comes up. Select the application you want to launch or quit and click "done." Now to save the script. Go to "file" and click "save as," and make sure it's saved as an application and not as a script.

Back to Cronnix

So now at the dialog screen at cronnix check the "prepend usr/bin/open" box and click browse to go to where the applescript was saved. Once you've selected the script click "new" in cronnix and the task is set. While in cronnix you can choose to run the task to give it a final check, where you go to "task" and then "run now." While in cronnix make sure you click the "save" icon to save the task. The nice thing about cronnix is that since it's a system feature you don't need to open cronnix to run or quit the app! It's done all in the background, so automatically at the set time the script will run and there you have it, an automated mac.

One last screen shot to make sure all is well.

So in this script I'm telling skype to run at 1 pm every day. And that's it! I hope you enjoyed this tutorial. Leave a comment if you have any questions, and I will try my best to answer them (I did just discover this all yesterday, after all).