HOW AUTOMATE YOUR WORKSPACE APPLICATIONS USING BASH

Suan Suan Alessandra Chen
7 min readMar 8, 2020

--

When I worked in shop floor, I learnt a lot on how is important reducing wastes. I always try to apply the lean philosophy both in work and in daily life. In particular, I tried to reducing the “DOWN TIME” wastes, and it was my biggest daily challenge!

Today, I use this philosophy to reduce the wasted time to prepare my workspace on my computer. I recently learn how prepare it using macOS terminal and I want share it with you. Everyday I took different workspaces, it means different sets of opened applications, the debugger, the editor for one type of work, the 3D cad, the slicer for another one. Switching manually, look for an app, open it, look for another one, open and repeat everyday means a big waste of time. Do you agree?

First of all you need to collect your applications and group them by workspaces (programming, 3D printing and so on). Once you have thinked of what are you workspaces, let me to show you how you can run different workspaces on you macOS using bash scripting and some little macOS tricks.

I am writing this article to whom, like me, is a beginner in macOS and bash, so I will treat some parts of this article excessively detailed and (using the words of my boyfriend, who is computer engineer) in a basic and childish way! Of course he jokes, but explaining the steps I’m learning helps me learn them even better. As the ancient Romans would say: “docendo discitur”, that is: by teaching you learn.

Are you ready? Let’s start!

0. Open the Terminal app

Open the Terminal: ⌘ + Space and enter "Terminal". Once you are in, the first line you see, should be something like it:

<user>@<name_of_computer> ~ %

This line is named command promt. Look at the simbol ~, it means you are in your default "Home". At the left side of @ there is your user name and at the right side there is your network machine name.

1. Move on to your favorite directory

This step is not mandatory, but is useful to have a quick look on what we are doing, so move on the Desktop directory. The command that give you the power to move from one dir to other is cd <path>; look at man cd to find out more. If you don't give any path after it, the command will bring you automatically back on the home directory. The syntax is ~ % cd <path>, and so move on the Desktop:

~ % cd Desktop/

Press Enter to execute our command and now the prompt should be changed in something like this:

Desktop %

If you want to ensure to be in the Desktop directory, give the following command:

% pwd

the output should be similar to /Users/<you_username>/Desktop, right? If you want to give a look in the content of the directory, give the ls command. I suggest you some variants like ls -1 or ls -al.

2. Create a file

The second step is create a new file. This file will become our beautiful script the will automate the preparation of our workspace. You could use you preferred text editor, but I prefer to use the nano command line program, because using it make me feel like a pro 😎 (but my boyfriend has arguments on this as well, he is telling me that the real pros use vim and not nano 😒).

The syntax is:

Desktop % nano <name_of_our_cool_script>

Press Enter. Let's begin to write our marvellous time-saving script!

Writing codes with nano program is extremely comfortable and I’m noticing that once you’re familiar with it, in some cases it becomes more efficient to use nano rather than a graphic text editor.

3. Write the code

Now I show you how to write the code that will launch our applications, the language used is Bash, so this script will probably be fine on other UNIX Like environments, such as Ubuntu for example. One of my workspaces consists of a text editor, such as Atom, a web page from which to follow my courses on Coursera and GitHub Desktop to record my changes, so as a workspace it is quite simple. The code will be organized in lines. Each line generally corresponds to an application that we want to launch. So in this case there are three. The syntax of each row is similar to what you see here:

open "<path_of_app_or_URL/app_name>" &
open "<path_of_app_or_URL/app_name>" &
open "<path_of_app_or_URL/app_name>" &

where the open command is a built-in command in macOS that automatically runs the default application for the given path. The most funny trick in this code is the & symbol. It means "open it in background". It is not mandatory to use it, but if we want to save time, we have to put it at the end of each line, this line will be executed in the background, which means that if more commands are executed with the & symbol, these commands will be executed in parallel! And so, saving us a lot of time. This is precisely what we want to do!

Making a practical example, I use GitHub Desktop, Atom and Coursera (a specific Web URL), so the code is:

open "Applications/GitHub Desktop.app" &
open atom /Users/you_username>/projects/project &
open "https://www.coursera.org" &

It looks like:

For Save your code, press ctrl + X then Y and finally ENTER. Congratulations! Your code is done!

5. Make it executable

Now, our script is just nothing less than a simple text file. We have to tell our mac that it is executable. To do this we have to use the chmod command to change the access permissions of file system objects (files and directories).

Let explore our file permissions by giving this command:

Desktop % ls -al prova

look for the row of your script file and give a look to this strange line:

-rw-r--r--

There are always ten characters. The first one (on the left) means if it is a file - or a directory d. r, w, and x stay for read, write and execute permission. Go to Wikipedia chmod to find out more.

In this case we have to give execute permission to the current user. Give this command:

Desktop % chmod u+x prova

where u is for user (the file owner: i.e. yourself!), + stands for "adds the specified mode" and x is the execution mode we want. Then press ENTER.

Using again the command ls -al, we should have:

-rwxr--r--

Well done! Now, our script is executable, but only if it is called thorough the Terminal. In the meantime, let’s give a try and test our script by running:

Desktop % ./prova

Using ./ is very important to run a script, otherwise it will be not executed.

Return on Desktop and look at our file: after used chmod it changed its clothes and became an exec file! Very cool, isn't it?

6. Make it clickable!

But there is a little problem, out program is not clickable. We are still interest to save time, so we want be able to open very quickly using our mouse, right? Giving an extension on macOS is not mandatory, but is comfortable in some cases. You could leave your original name file, change it or ad an extension; being a Bash script, it should be .sh. It doesn't matter, but our macOS has to know how to open our file. At the moment probably it will open it by using a default text editor. We have to change the default application used to open the script.

So do this, move on the Desktop (using the GUI!), find our file and follow these steps:

  1. right-click on the file to open the context menu
  2. move on Get Info>Open with>Other>Select on "Enable": All Applications
  3. look for "Utilities" >Terminal. app>Add

Now, you can open it by simple double click, just like and ordinary Application!

Make it a beauty! 💃🏻 (Optional)

I don’t like the default exec icon, so change it! You can use or download any image, but it must be in PNG format. When you have choose one, follow these steps:

  1. open the PNG image and select it all by CMD +A, then copy it with CMD + C
  2. right-click on the script file and click on Get Info, then click its Icon. After this, the border of the Icon must change in a blue blurred-border, finally paste the copied PNG inside it by giving CMD + V

Try to double click the script, it will open quiclky your workspace!

>> Hint: to be faster, drag the script in your dock!

Now, for open all my app, I just click on my icon and voilà… my workspace is ready in just seconds!

--

--