I’ve been doing installation work, where sensor and processing computers need to be tucked into a hideaway often without wifi/network connect-ability; and they only run on certain days of the week, for certain hours. This post is about making sure the applications are running when they should be (and off when they should be) for Mac OS X.

There are several ways to do this; this method uses AppleScripts and no edit to launchd. Below is code I used for an installation at the LSU Museum of Art, for Sandra Russell Clark’s Traces: Venice show that starts a standalone application which draws information from a sensors (via Arduino) and outputs audio (MaxMSP). There are other ways for installations like using RaspberryPi, creating local websites to toggle settles, etc, but I will cover that later in a different post.

At the bottom of this post, I address setting up your Mac to automatically load this script and other necessarily settings before leaving your installation computer somewhere.

1. Set the output volume.

Almost all my installations have an audio component, so I have to make sure the volume is set at a certain level and un-muted. Write a script that set the volume to a certain level at the start of the application. This may seem unnecessary, but I often want to make sure the volume is set and also not muted. The line below sets the volume to 100% and un-mutes the audio. Substitute any value you want to see the volume. For a more through resource on volume scripting, see

[ here ].

set volume output volume 100 without output muted

2. Open/Kill your application based on day and time

Script to get set the day of the week to variable today

set today to weekday of (current date)

Script to get the time, takes current  time and sets it to variable whichHour

set todayTime to time of (current date)
set whichHour to (todayTime / (60 * 60))

A possible cleaner way, sets currents current time to whichHour

set whichHour to ((time of (current date)) / (60 * 60))

We now have the current hour  to the variable ‘whichHour’ to the and the day of the week to ‘today’.

3. Create functions to start the application and stop the application.

Let’s say the application I want to control is called ‘Sandra_Sound’.

When runApp() is called, it checks if the application ‘Sandra_Sound’ is already open and if not open, then opens the application. When killApp() is called, it checks if the application ‘Sandra_Sound’ is running AND quits the application is it’s open.

4. When you want your application up and running

Essentially the day of the week and time are the most important for me. So say, the museum is open on Tuesdays-Fridays, 10am-5pm. Also, Saturdays, 11am-5pm. All the other days, I don’t want anything to happen.

Notice where I have commented out “# return 3600”, we will discuss the importance of using a return later in the Handler section.

5. Set this all in on an idle Handler

We are going to wrap the timing logic with an idle Handler. Your computer will execute the contents of an on Idle handler when idle, typically every 30 seconds, unless the code returns a different value (or the last result). So there may be reasons you would want the on Idle script to wait a longer/shorter duration before executing again. For example if the day is Sunday, and you know your application doesn’t need to start for another 24 hours — you really don’t need to check every 30 seconds which day it is, so you can set the return value higher to something like an hour (3600 seconds). For more information on Handlers, use this [ reference ].

6. Putting it all together

Now, at default, this script will run every 30 seconds, to check the day and time.  Change the last return value if you want then on idle script to execute more/less often.

7. Make sure your computer runs this script on boot. 

In OS X, make sure your mac opens the script on boot.

Users & Groups > Login Items > + > Your script name

8. Make sure your computer automatically logins under your user name and restarts after power failure.

To make sure your computer logins in under your username.

System Preferences > Users & Groups > Login Options > Automatic login > Your User Name

If you don’t see your current user name in the dropdown menu, you are probably using your AppleID & Password to login. You have to unlink your login from your AppleID.

System Preferences > Users & Groups > Current Users > Your User Name > Change Password > Use Separate Password

After you’ve change your password, you should be able see your user name in the Automatic login dropdown menu.

To make your computer restart after power failure (say your MacMini gets it’s power turned off and on quickly).

System Preferences > Energy Saver > Restart after power failure

 

That’s it, hope this helps.