Simple Bash/Apple Script to execute multiple commands on parallel terminal tabs

Niranjan Akella
5 min readApr 24, 2022

--

Source: iStock

We often develop complex applications which require certain amount of basic setup to begin development on every occasion.

For instance, I recently started working on a react-native project where I had to develop a mobile application with a backend on Express.js and MongoDB. I generally use iTerm 2 on my M1 MacBook as my goto application for terminal usage.

At every instance when ever I wished to resume my work on a daily basis I had to perform few routine tasks to setup my development environment as follows:

#Terminal1
cd <BackendProjectFolder>
npm run start
#Then open the MongoDB Compass Application#Terminal2
cd <ApplicationProjectFolder>
npm run android
#Then open the VSCode and navigate to project folder

After doing all these routine tasks every time I will be good to start my development. It might look small but there are developers who develop lots more things that require large basic setup to start their development.

Hence I decided to develop a script that would automate this above process for me.!!

I have developed an AppleScript that would run the necessary commands on multiple tabs as needed to setup my environment as needed to start my development in no time.

Bash Script

#!/bin/bashosascript -e 'tell application "iTerm2"
set newWindow to (create window with default profile)
tell current session of newWindow
write text "cd /Users/akellaniranjan/Desktop/Hypr-API-1
npm run start"
end tell
tell current window
create tab with default profile
tell current session
write text "open $MONGODB_COMPASS
open $VSCODE"
end tell
end tell
tell current window
create tab with default profile
tell current session
write text "cd /Users/akellaniranjan/Desktop/Hypr-Mobile_App
npm run android"
end tell
end tell
end tell'

Apple Script

tell application "iTerm"
set newWindow to (create window with default profile)
tell current session of newWindow
write text "cd /Users/akellaniranjan/Desktop/<BackendProjectFolder>
npm run start"
end tell
tell current window
create tab with default profile
tell current session
write text "open $MONGODB_COMPASS
open $VSCODE"
end tell
end tell
tell current window
create tab with default profile
tell current session
write text "cd /Users/akellaniranjan/Desktop/<FrontendProjectFolder>
npm run android"
end tell
end tell
end tell

Script Explanation

  • Initially the script commands the system to open the iTerm application and then to create new tab with the default profile.
  • Then it runs the commands to navigate to the backend project folder to run the project.
  • Further it creates an additional tab in the same window to run the $MONGODB_COMPASS & $VSCODE for which I have preset the variable as environment variables in the ~/.zshrc file as
#Add these two lines below the PATH variable at the beginningexport MONGODB_COMPASS=/Applications/MongoDB\ Compass.app
export VSCODE=/Applications/Visual\ Studio\ Code\ -\ Insiders.app
  • By running these commands the script automatically opens both the applications and since I have previous quite/closed my VSCode session with my project folders open it would open them in the same state.
  • Then finally, the script would create another tab in the same window and execute the commands to navigate into my frontend folder and build my android app which would in turn trigger to open up the android emulator device.

Now to spice things up a bit!! 🌟🌟🌟

I added further functionality to run the above developed script just using a keyboard shortcut from any application. This simplifies my workflow tremendously by reducing the time needed for me to setup the boring stuff through automation.

Note: Below instructions are given as on 25th April 2022, MacOSX. It may/may not vary, please kindly do refer other sources for slight modifications.

Step — 1: Script Creation & Service Setup

  • Open Automator application from Spotlight and choose ‘Quick Action’ from the menu.
  • Search for ‘AppleScript’ on the top search bar. You will find a ‘Run AppleScript’ option will show up in the menu below. Double click on it.
  • This will open up a short window on the main menu where you will need to set ‘Workflow Receives’ input as ‘no input’ to make sure that the script runs without any inputs.
  • Then in as ‘Any Application’, to allow any application to run the script.
  • Further in the ‘Run AppleScript’ menu, past the above mentioned Apple Script (Note: Modify the script as you desire it to perform) and click on the build icon. Which will open up another menu to choose the application to run. Please select ‘iTerm’ from the given list of applications.
  • Then click on the ⏯ button to run the script and check if it runs as desired if not modify the script.
  • Save the file with a proper name without spaces in the name from File->Save.

Step — 2: Setting up Apple Keyboard Shortcut

  • Go to System Preferences -> Keyboard -> Shortcuts.
  • Select Services from the left sidebar.
  • Find your service that you have just save in the previous step from the list on the right-hand side .
  • Then hover on the short cut and you will be displayed with an option to add a shortcut on the right most end.
  • Now navigate to System Preferences -> Security & Privacy -> Privacy and select ‘Accessibility’ from the left sidebar.
  • Unlock the padlock and click on the ‘+’ sign to add applications.
  • Add Automator. and Finder (you can find the finder application at ‘/System/Library/CoreServices/Finder.app’, Please navigate to this location) to be accessible by the system.
  • Now you can close the System Preferences.

THAT’s IT!!! 👍 you have successfully setup your shortcut.

If you found this article helpful please do share it with your peer developers to ease their workflows and to embrace knowledge.

— Niranjan Akella

--

--

Niranjan Akella

A scientist by heart and a passionate researcher in my field of expertise. I love philosophy.