Michael Cordell's Blog

Automating the start and end of your work day

13 Jun 2014

You probably do the same thing when you arrive every morning at the office if you are a developer. Fire up your editor, open your chat program, probably a console or two. At the end of the day you probably shut all of that down, and it is a pretty stable and recurrent flow, which means that it can be automated! As an exercise, I wanted to automate my own daily bookend processes and I’d like to share how I accomplished that. As a short preface, this post focuses on automation in OSX but similar tools are probably available in most linux distros. My tools of choice for this are:

I wanted to automate the following:

  1. Start my development vagrant box
  2. Close any programs that obviously aren’t work related
  3. Open my work programs (text editor, chat client, chrome)
  4. Switch my chrome profile to the one I use for work
  5. Position all of my windows in their typical location (Chat client on my laptop screen, main screen split between editor and browser)

This all started when I noticed that the vagrant box I use for development would eat battery when not in use, I often would forget to suspend it as I was packing up for the day. Additonally, I would have to start the box every morning as I started the day. This can easily be accomplised through a bash script:

Start:

VAGRANT_PATH=/Users/michael/vagrant
cd $VAGRANT_PATH
status=$(vagrant status | grep running)

if [ "$status" != "" ];
then
    echo "Vagrant already up.";
else
    vagrant up &> /dev/null
fi

Stop:

VAGRANT_PATH=/Users/michael/vagrant
cd $VAGRANT_PATH
vagrantstatus=$(vagrant status | grep running)

if [ "$vagrantstatus" != "" ];
then
    vagrant suspend &> /dev/null
fi

I considered running the script on cron, but I don’t really leave at a percise time and I wouldn’t want my vagrant to suspend itself while I was debugging just because it was 5 pm. What I settled on was to use an Alfred shortcut to run the bash script. For those who haven’t setup a workflow before, see the alfred documentation. I set up a workflow that works like this: I type good(morning|night) which triggers a script, which runs, and then echoes “Good Morning/Night” when it completes. I pass this result to Alfred’s Large Type so that the message displays in big text and lets me know that I am ready to go.

The workflow looks like this:

Alfred Worflow

I actually run an applescript rather than just the bash script because I wanted to accomplish things like switching the chrome profile. The applescript looks like this:

do shell script "~/Dropbox/Scripts/vagrant_start.sh"

tell application "VLC"
    quit
end tell

tell application "XBMC"
    quit
end tell

tell application "Slack"
    activate
end tell

tell application "Google Chrome" to activate

tell application "System Events"
    tell process "Google Chrome"
        tell menu bar 1
            tell menu bar item "Users"
                tell menu "Users"
                    click menu item "MonkMike"
                end tell
            end tell
        end tell
    end tell
end tell

do shell script "/usr/local/bin/subl ~/mcms.sublime-project"

tell application "System Events" to keystroke "g" using {shift down, command down, control down, option down}

"Good morning!"

This script does the following

  1. Runs the vagrant script
  2. Closes non-essential programs
  3. Starts my chat client (Slack);
  4. Switches the chrome profile (At the moment there isn’t a better way to do this than through simulated clicks)
  5. Starts my text editor
  6. Calls my a special slate layout that positions the windows properly
  7. Echoes “Good morning” for Alfred’s large text

So the last piece of the puzzle is the window positioning through Slate:

alias mon-laptop      1440x900
alias mon-dell        2560x1440

alias full move screenOriginX;screenOriginY screenSizeX;screenSizeY
alias lefthalf move screenOriginX;screenOriginY screenSizeX/2;screenSizeY
alias righthalf move screenOriginX+screenSizeX/2;screenOriginY screenSizeX/2;screenSizeY

layout 2monitors 'iTunes' ${full} ${mon-laptop}
layout 2monitors 'Slack' ${full} ${mon-laptop}
layout 2monitors 'iTerm' ${full} ${mon-laptop}
layout 2monitors 'Google Chrome' ${righthalf} ${mon-dell}
layout 2monitors 'Sublime Text 2' ${lefthalf} ${mon-dell}

#aliases for keymaps
alias hyper ctrl;cmd;shift;alt

bind g:${hyper} layout 2monitors

I set the windows to the proper positions through aliases in the layout and then I trigger the layout with a hotkey Hyper+G. You can see me calling this hot key tell application "System Events" to keystroke "g" using {shift down, command down, control down, option down} .

And thats it, all I have to do is type goodmor.. in alfred and my work environment sets up for me.

comments powered by Disqus