Automating the start and end of your work day
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:
- the indespensible Alfred
- plain old applescript
- Slate an opensource window manager. See my post on [getting started with Slate]({% post_url 2014-05-25-getting-started-with-slate %}).
I wanted to automate the following:
- Start my development vagrant box
- Close any programs that obviously aren’t work related
- Open my work programs (text editor, chat client, chrome)
- Switch my chrome profile to the one I use for work
- 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]({{ site.url }}/images/alfred-workflow.png)
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
- Runs the vagrant script
- Closes non-essential programs
- Starts my chat client (Slack);
- Switches the chrome profile (At the moment there isn’t a better way to do this than through simulated clicks)
- Starts my text editor
- Calls my a special slate layout that positions the windows properly
- 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.