paint-brush
Execute Multiple Commands On Parallel Terminal Tabs With A Simple Bash/Apple Script by@niranjanakella
2,920 reads
2,920 reads

Execute Multiple Commands On Parallel Terminal Tabs With A Simple Bash/Apple Script

by Niranjan AkellaMay 5th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

An AppleScript that would run the necessary commands on multiple tabs as needed to start my development in no time. The script runs the commands to run the iTerm application to create a new tab with the default tab. Further, it creates an additional tab in the same window in same window. I decided to develop a script that would automate the above process for me! The script is a simple solution to change that. It might look small but there are developers who develop lots more things that require a large basic set-up to start their development.

People Mentioned

Mention Thumbnail

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Execute Multiple Commands On Parallel Terminal Tabs With A Simple Bash/Apple Script
Niranjan Akella HackerNoon profile picture


Are you frustrated with running the same scripts again & again before resuming your work? Here is a simple solution to change that.


We often develop complex applications which require a 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 go-to application for terminal usage.


In every instance whenever I wished to resume my work on a daily basis, I had to perform a few routine tasks to set up 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 a large basic set-up to start their development.


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


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


Bash Script


#!/bin/bash
osascript -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 a 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 beginning
export 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 set up the boring stuff through automation.


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


Step — 1: Script Creation & Service Setup

  • Open the 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 that 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 the service that you have just saved 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 rightmost 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 set up your shortcut.

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

— Niranjan Akella


This article was first published here.