8

I'm looking for a solution that starts a timer whenever I sign in to my office wifi and stops it when I've left. It should essentially log the hours I've spent at the office. I'm aware that there's iOS software for this, but I like my battery time as it is.

Extra bonus would be if the solution could log the hours in iCal automatically.

6
  • Interesting, I don't know an app that can do this… Commented Apr 3, 2012 at 9:47
  • I'm assuming your work has a different/distinct wifi name (essid) than you use a home?
    – alesplin
    Commented Apr 4, 2012 at 14:14
  • @alesplin Yes indeed.
    – yusf
    Commented Apr 5, 2012 at 14:18
  • I may have a bit of a hack that can be modified for this. I'll take a look at it and see.
    – alesplin
    Commented Apr 5, 2012 at 18:32
  • @alesplin How did it go?
    – yusf
    Commented Apr 18, 2012 at 7:29

3 Answers 3

9

ControlPlane might be something you can use to accomplish this:

ControlPlane determines where you are or what you are doing based on a number of available evidence sources and then automatically reconfigures your Mac based on your preferences. Evidence sources can include your current location, visible WiFi networks, attached USB devices, running applications and more. You can even write your own evidence sources using shell scripts!

In a nutshell, you can give ControlPlane an "evidence source", such as

  • WiFi, either networks within range or based on the network you are currently connected to

enter image description here

Then perform a set of actions when entering (or leaving) a particular context, such as

  • Open any file, including starting Applications
  • Run a script, such as AppleScript or shell script

enter image description here

My thinking here is that if ControlPlane knows what your office wifi is called, you can create rules so that when you connect to it you start an application or shell script to start logging your hours. Then, when you disconnect, you stop the application or run another shell script to stop logging your hours.

I realise this might not be the "silver bullet" solution you were looking for, but it might be worth a try. You'll still have to find some kind of timer app/script, but thats probably the easier part of the problem :)

2
  • Thank you for the answer! ControlPlane seems to be a bit of overkill at first (was looking for something more lightweight solution) but I'll try it out once I have the time to learn AppleScript.
    – yusf
    Commented Apr 18, 2012 at 7:28
  • @yusf No problem. Hopefully it'll help. I'd also be interested in your final solution when you get there! :-)
    – binarybob
    Commented Apr 18, 2012 at 7:51
4

You can use the command networksetup -getairportnetwork <device> to get the name of your currently-connected wireless network (where is the device ID of your airport--probably en1).

From there, you can use launchd to schedule a script to run every 5 minutes or so which will get the name of the wireless network you're connected to and log when you first connect to your work wi-fi.

Depending on what you want, you could then use the same script to log another entry when it notices you're no longer connected to your work wi-fi, or to post some sort of notification (perhaps using growl) on specific intervals. Something along the lines of "I've been at work for 9 hours--time to go home".

Or you could use the start/end times in a running log and write another script to parse it and create some sort of report.

I've got bits and pieces of these types of scripts lying around, if time at work allows, I'll add something here.

man launchd is quite useful.

1
  • Thank you @alespin! Opens up for a nice minimal approach. :)
    – yusf
    Commented Apr 19, 2012 at 7:53
1

Here's another option for people trying to do this.

Rather than running a command every 5 minutes, you can run it whenever the folder /Library/Preferences/SystemConfiguration/ changes. This generally happens only when there is some sort of network change.

You could do this using Lingon or make your own launchd plist which would look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>AbandonProcessGroup</key>
        <true/>
        <key>Label</key>
        <string>localhost.on_networkchange</string>
        <key>ProgramArguments</key>
        <array>
                <string>/path/to/your/script.sh</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>WatchPaths</key>
        <array>
                <string>/Library/Preferences/SystemConfiguration/</string>
        </array>
</dict>

Obviously you'd want to replace /path/to/your/script.sh to the actual path.

Note that you may need to include a 'sleep 10' near the start of the script to give the Mac a chance to make all of its adjustments once a network change has happened.

Also, the easiest way to get the name (SSID) of the AirPort network you are connected to is:

airport -I | awk -F': ' '/ SSID/{print $NF}'

The airport command can be found at /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport in 10.8 (and I believe 10.7 and 10.6)

I generally link it to /usr/local/bin:

ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/local/bin/airport

you may need to sudo that command, depending on how your /usr/local/bin/ is setup.

Last but not least, you need to consider what to do when you put your laptop to sleep. For example, if you are in the office from 9-5 and sleep your laptop at 5, you want to record that too, right? Otherwise how will you know when you left?

For that I recommend SleepWatcher 2.2 which is available from http://www.bernhard-baehr.de (free, source code included). That will let you run a shell script whenever your Mac wakes or sleeps.

The only other consideration is what to do if you log out or shut down your computer, instead of sleeping it. I don't have a good answer for that one. Theoretically a LogoutHook would be the answer, but those seem unreliable, especially on shutdown vs logout.

A completely unrelated suggestion

You mentioned that you have an iOS device. Perhaps one way to solve this problem would be to create a Reminder for whenever you Arrive or Leave work. When the reminder goes off, trigger Siri and say "Remember left work at 5pm" or "Remember arrived at work at 9am" and Siri will make a note of it. Not elegant nor completely automatic, but if I was looking at tracking billable hours, I wouldn't mind a little redundancy :-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .