Automating My Apps With OSX Automator

17. March 2018 Uncategorized 0

Over the past few months I’ve been using the same laptop at the office as I have at home. For the most part, I’ve enjoyed it. One thing that has been annoying, though, was closing down things I was working on when I went home. For example, I don’t want Slack or Outlook open at home. Also, while I might want VS Code at home, it’s unlikely that I’ll want it with the current project loaded. And of course, when I come back in the morning, I need Outlook, Slack and a couple other apps loaded.

I assumed there had to be an app that I could install that would let me tell my computer I’m at home or the office and then have that app open or close the appropriate applications. There appeared to be a couple, but none of them were maintained, and the ones I tried out seemed way more complicated than what I wanted. But in my searching, I learned about OSX’s Automator.

 

This is a native application in OSX that let’s you create other applications to automate tasks.

As you can see on the left, there are all sorts of options and actions you can take, but I was only interested in two. Opening an application and closing applications

 

Close All Applications

I chose to create an Application and named it “LeaveWork”.  I then noticed there was an action named Quit All Applications. That seemed perfect.  I added it to my LeaveWork application

As you can see, you can specify applications that it does not close, so if you want to keep Chrome open, you can add it to the list.

Unfortunately for me, I couldn’t get this to work very well. It seemed that either Slack or Outlook would trip it up. Additionally, it seemed like having applications on multiple spaces caused some problems. I could keep re-running it and it would eventually get all my applications. But at that point, why not just cycle through and close the apps one at a time anyway.

Quit Application

The next thing I tried was to queue up a bunch of individual “Quit Application” actions

However, that suffered from roughly the same thing. Sometimes Slack would throw an error and everything else below it wouldn’t get the close message. So I moved on from that.

Shell Script

After digging through the list of actions, I saw that I could add a shell script. So that’s what I did.

As you can see, I started using killall to close the applications.  This worked for the most part. But I still ran into an issue. I tried to run killall "Code" to close my VS Code editor, but it didn’t work. I verified inside of Activity Monitor that the name of the process was in fact Code and yet VS Code stayed open.

After some digging, I found a post that said the way to kill VS Code with a bash command is to actually kill Electron.

As a result, I added kill -9 $(pgrep Electron) to my shell scripts, and ran it, and it worked!

I was now closing a list of applications that I didn’t want open anymore.

Finder

After playing around with this for a bit, I noticed I’ll often have a few Finder windows open at the office. I typically don’t need those at home, and you can’t really kill finder, as it’s always running. So I went back to the list of actions to see what else I could do.

That’s when I found out you can run applescripts from within automator as well. And, using AppleScript, I can tell finder to close all of its individual windows while still staying active.

The script above is actually:

tell application “Finder”

  activate

    set wlist to index of every Finder window

    repeat with idx in (reverse of wlist)

      tell Finder window idx to close

    end repeat

  end tell

return

After this, I saved LeaveWork in the Applications folder of OSX.

Now, at night, when I’m ready to go home, I launch spotlight by running CMD+Space and type LW and hit enter and all the work related applications close down.

Getting Back to Work

After getting that to work, I thought “Wouldn’t it be great if I could do something similar in the morning?” So I went back to Automator and created a new app that I named “GoodMorning” and told it to launch things like Outlook, Zoom, Slack etc so that no matter what I was doing the night before at home, I can check my email and slack messages when I get in to the office in the morning.

As you can see, not only do I open the applications I want, but I also set my laptop’s volume to mute. Because most nights I’m either listening to music on my laptop or watching Netflix. When I get in to the office in the morning, I don’t want random Slack and Outlook notifications bothering people in the office.

As with LeaveWork I also launch this through Spotlight by typing GM and hitting enter.

Focus

The next step, although I haven’t yet done this, would be to combine this functionality with another application, Focus (https://heyfocus.com/). I use this to block websites like Reddit, Netflix, Gmail etc while I’m in a “focus” period. It will also run scripts, so I could create an Automator application that closes things like Outlook and have Focus kick that script off when I go into a focus time and have it run “GoodMorning” when I come out of a focus time.

 


Leave a Reply

Your email address will not be published.