Writing a script that opens the programs you need to start your day!

Every time I start my workday, I have a series of applications and browser tabs that I have to open to get ready for work. It’s not the most complicated process, but it’s a little tedious to have to open all of the same tabs/programs every single day (especially when one of the urls needs to be opened in incognito mode to work properly). So, this morning I started tinkering with a script that would automate that work for me, and I thought it would be a good idea to write this post with instructions so you can automate the start of your workday (or any other task for that matter). This works on a MacBook using a zsh terminal.

Step 0: If you don’t already have one, create a folder where you’ll keep all of your scripts.

I put them in ~/scripts, but anywhere you want to store it will work.

Step 1: Add that directory to the PATH

If you stored the scripts at ~/scripts you can use the following command to export the directory to the PATH

export PATH=”$PATH:~/scripts”

Otherwise, replace “~/scripts” in that command with the path to your scripts folder.

Step 2: Create a .sh file where you’ll write your script

In your terminal, navigate to your scripts directory, and once there use the command:

touch ***your_script_name***.sh

To create your .sh file, then open that file in your text editor of choice.

Step 3: Write the script

This is what my script looks like, but there are a few things going on here, which I’ll discuss below:

`#!/bin/bash`

echo “Opening your incognito tabs…”
open -a “Google Chrome” -n — args — incognito “https://learn.co/___hidden___" “https://docs.google.com/spreadsheets/___hidden___" “https://www.google.com/gmail/about/#" “https://github.com/" “https://help.learn.co/en/articles/___hidden___"
sleep 2
echo “Opening Slack…”
open -a Slack
sleep 1
echo “Done.”
echo

First there are a couple of things that you can either ignore, or customize to your liking. The script works with or without the #!/bin/bash command on the top line, but I think it's good to be explicit there. Also, the echo commands simply dictate what gets output to the terminal as the script runs, so you can include as many of those as you want. Finally, the sleep command just tells the script to pause for a number of seconds before running the next line of the script (see this guide for more info on the sleep command).

The command that’s doing the heavy lifting is the open -a command. Let's start with the basic example that I'm using for Slack, then we'll look at the more complex Google Chrome example. Note: you can test out the open -a commands that you're trying to use in your command line before including them in your script.

Opening an application is fairly simple from the command line. You simply use the open -a command followed by the name of the program, and if the program has a name that is more than one word, wrap the name in quotes. Here, we’re using open -a Slack to open slack. Note: if you already have a program open, this command will simply bring that application window to the front of your screen.

Next, let’s talk about how to open chrome tabs using the open -a command. Chrome allows you to use open -a with a series of URLs after it to open multiple tabs. For example, the following command:

open -a “Google Chrome” “https://wikipedia.com" “https://espn.com"

Would open two new chrome tabs for Wikipedia and ESPN.

In my script, I’m doing something a little different, because I want my tabs to open in a new incognito window. For that reason, I use -n --args --incognito before my URLs. Customize those URLs to your liking, and move on to the next step.

Step 4: Set the permissions on your new script

From your scripts directory run the following command:

chmod u+x ***your_script_name***.sh

This will allow you to execute your script from the terminal.

At this point, you should be able to run your script using the following command:

~/your_script_path/your_script/name.sh

For example, mine looks like this:

~/scripts/open_expert_chat.sh

That’s a pretty long command, but we can use zsh aliasing to make this easier:

Step 5: (Optional) create a zsh alias for your new script

If you’re using zsh aliasing the command above is pretty straightforward (if you don’t have zsh, you can use this guide to install it). For example, I can simply type aaq into my terminal and it runs my script for me.

To do this, open your ~/.zshrc file in your code editor. Here you can add a line (anywhere really, but I added mine to the bottom of the file) for your alias. My alias line looks like this:

alias aaq=”~/scripts/open_expert_chat.sh”

So if you wanted to do the same with yours you would add:

alias ***your_alias_code***=”***your_command_from_step_4***”

Make sure you close and reopen your terminal before testing your alias out.

Conclusion

That should allow you to open any programs you need to start your day. I’d like to expand on this to allow me to actually sign into some of the websites that I’m opening and maybe send a slack message when I start. It would also be really cool if I could use Apple’s tile windows feature to put the Chrome windows on the left and the Slack window on the right.

Resources:

Code. Run. Eat.