I love automation and often use automation tools to improve my productivity. I’m using a MacBook Pro as my main workstation, which includes the Unix Like Mac OSX. I wanted to find a way to ease my everyday tasks, for example, programming and writing on my blog. Open many apps and setting them up, slows down my cognitive system, and doing that can take a couple of minutes. What if I could reduce that to a few seconds by writing a script to perform those repetitive tasks for me.
Here’s an example of the set of tasks I need to do in order to start writing a new blog post (I use the Hugo static site generator).
- Open the terminal.
- Navigate to the path in which my blog is located
- Run the server command
hugo serve -D.
- In the current (iTerm) terminal open a new horizontal panel/session.
- Navigate again to the path (yes my default iTerm session doesn’t remember my the path I was).
- Run visual studio code
- Start writing.
Ok, so if I am focused enough these steps take 20 seconds (maybe). If I am a little distracted 1 minute?.
So I found OSX includes its own interpreter for automating those type of tasks. It supports their own automation scripting language called Applescript. I started using and didn’t enjoy it since it lacked features you would expect. I accept the idea they had for a declarative language was good. I imagine they were looking to reach a wider audience, not only programmers.
Here’s a simple example. It opens a new iTerm window and writes a command.
The above script checks if an iTerm window is already open, if not it opens a new one, it gets the current session an writes a simple command.
Running the script
osascript, the interpreter, is already installed on OSX
To run your script use in your terminal:
And magic! that will perform those tasks.
In my use case, I wanted this script to be available as a binary
Did you notice the
Make it executable
chmod + x /path/to/script/myscript.js
Run it with the shell
Automation is not only fun but can increase your productivity a lot. I showed you a simple example, but with imagination, you can write more sophisticated automation scripts. That can help you to focus on what you care when working with anything.