Automate with AppleScript or how to be less distracted

karolinkas

Karolinkas

Posted on January 17, 2021

Automate with AppleScript or how to be less distracted

Write some simple AppleScript commands to prevent yourself from procrastinating


A while ago I went on a flight from Spain to the UK for a work meeting and since I was officially on a work trip I decided to try to get some work done throughout the flight and made sure I had the materials I need beforehand. Getting on the flight I ordered a cup of coffee and got started. A couple of times I was about to type something unrelated in the browser, when I remembered that I was on a flight and offline, so I went back to work. The flight passed extraordinarily quickly and I suddenly had reached already in the UK. Surprised I realised at arrival that I had finished the task and even learned a lot because I had time to experiment more. I assumed that this had happened because I was less distracted from being able to search things online that then lead me to other topics that are exciting and, in short, distracted me completely. I think it's absolutely amazing to find ways to work more focussed and have more time for real life. 👪 🏕️ 🍺

What do you need for this tutorial?

Especially if you are a new starter it is nice to work with languages that are already installed on your computer, so you can get started without any setup.

Being on a Macintosh, Bash and AppleScript will automatically be installed. If you don't know what these are here or get stuck on the way, that's fine. You can read up here:

What is Bash Script?
Basics of AppleScript

What do we want to achieve?

Sometimes there is repetitive tasks on your machine that we can simplify by using a script. In this case it would be: turning off distractions temporarily, open your favourite app and set it to full screen mode.
We are using AppleScript for this example, because it's nice and human-readable and great to get started with and lets you easily execute bash scripts, too. It's great to macgyver a script together fast and get excited about how easy you can interact with your machine, automate things and fine tune it over time.


The fun of writing your own automation scripts

Starting with your script

Create a file in any code editor called "focus.applescript" and add the below content. do shell script is the AppleScript command to run a built in bash script. In your terminal then run osascript focus.applescript, being in the folder where the script is located, to execute the command. The first line informs the application that runs the script, here the Terminal, to use AppleScript.


!/usr/bin/osascript

do shell script "networksetup -setairportpower en0 off"
Enter fullscreen mode Exit fullscreen mode



You will see it does something simple, which is disabling your Wifi. You can confirm in the menu bar easily, because of the greyed out icon.

Alt Text

For sure you can just turn it back on manually, but for the sake of this tutorial it is great because the functionality of the script can be confirmed easily. Now let's get more playful and add a delay until we turn the wifi back on after 30 seconds.


Quick Tip:
You can add inline comments with -- in AppleScript and with (* do some magic 💥 *) for block comments. This is very useful to add explanations to your code for when you later look through it to understand what each part is for.



-- Turn off wifi
do shell script "networksetup -setairportpower en0 off"

-- Wait a bit ⏱️
delay 30

-- Turn wifi back on
do shell script "networksetup -setairportpower en1 on"
Enter fullscreen mode Exit fullscreen mode

Adding an argument

That works as expected, now let's use an argument. We want
to specify how many minutes the WIFI should be turned off. Change your script to the code example below. Then we add the argument to the terminal command to execute the script like this: osacript focus.applescript 1 in combination with the code example below. The argument from the user (here you) gets taken from your command by accessing argv, it stands for argument values, with which by default gets the first argument. As soon as you introduce a parameter, you have to wrap the previous commands in a "parametrised handler". It's this part on run argv and end run, that belong together and in between is the function body, the things you want to happen once the parameter is available. The "parametrised handler" gets called by default when running the script. We can use a variable called secondsOfSilence to calculate the seconds for the delay using the parameter. That is necessary because AppleScript calculates things related to time in seconds, but we want to to say: "Turn off my internet for 10 mins" and not: "Turn off my internet for 600 seconds". Here is the next example code:



on run argv
    -- Now store time required to focus as seconds
    set secondsOfSilence to 60 * argv
    do shell script "networksetup -setairportpower en1 off"
    delay secondsOfSilence
    do shell script "networksetup -setairportpower en1 on"
end run

Enter fullscreen mode Exit fullscreen mode


Quick Tip:
When you are unsure about the value of something and you need to double check while developing it can be really useful to add a line like say "The value of" & secondsOfSilence. You machine will read out aloud what the value is.


Adding more arguments to the script

Next let's add a section to our command to open a specific app that we usually work with and would like to focus on. This one is interesting because as soon as we send a second argument the argv is a list of values that we need to access differently. With item n of argv we get each element in the list of arguments and in our case we store it in a variable again. tell is a built in command from AppleScript to execute things which is wrapped around whatever you would like to do, in this case activate and launch an app and bring it to front and ends with a closing tag like end tell.

    set yourFavouriteApp to item 2 of argv

    -- Open an app you would like to work with and focus on
    tell application yourFavouriteApp

            activate

    end tell
Enter fullscreen mode Exit fullscreen mode



Now when trying out the script in the Terminal you will be able to specify another argument, that you can provide like this: osascript focus.applescript 1 "TextEdit". Here "TextEdit" is just an example it can be any app you like to work with. The exact spelling matters, it should be the same like the name of the app in the dock.

Change settings of a currently running app

Lastly we add a final step, which is setting your favourite app to fullscreen mode. You will see that you can easily nest the tell commands. After activating the app we add a delay of 3 seconds to give it time to start up and then use the internal "System Events" application that manages processes in your machine to find the app of interest and the its attribute "AXFullScreen" to true.


Quick Tip: In AppleScript you can list most available apps with: tell application "System Events" to get name of (processes where background only is false).


    -- Open an app you would like to work with and focus on
    tell application yourFavouriteApp

            activate

            delay 3

            tell application "System Events" to tell process yourFavouriteApp

                    -- Set app of interest to fullscreen
                    set value of attribute "AXFullScreen" of window 1 to true

            end tell

    end tell
Enter fullscreen mode Exit fullscreen mode



Let's summarise again the steps we took:


Alt Text


This is the final script below. Feel free to comment or ask any questions.

#!/usr/bin/osascript

on run argv

    set minutes to item 1 of argv
    set yourFavouriteApp to item 2 of argv

    -- Turn off wifi
    set secondsOfSilence to 60 * minutes
    do shell script "networksetup -setairportpower en0 off"

    -- Open App
    tell application yourFavouriteApp

            activate

            delay 3

            tell application "System Events" to tell process yourFavouriteApp

                    -- Set App of interest to fullscreen
                    set value of attribute "AXFullScreen" of window 1 to true

            end tell

    end tell

    -- After waiting turn wifi back on
    delay secondsOfSilence
    do shell script "networksetup -setairportpower en1 on"

end run
Enter fullscreen mode Exit fullscreen mode



Now take a well-deserved rest and be proud of your work! Congratulations for having written your own custom command.

💖 💪 🙅 🚩
karolinkas
Karolinkas

Posted on January 17, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related