2

Summary

I'm trying to write a script to automatically launch my development server in iTerm2 from VS Code.

When I open my VS Code project, I want my bash script to:

  1. open iTerm2 (
  2. run cd .. && cd OtherFolder
  3. run npm start (so my node server will start)

Problem

I know how to open iTerm2 but I can't figure out how to make the bash script I wrote then run the commands from #2 and #3 in iTerm2 because I need to run the bash script from the VS Code terminal and then open iTerm2.

5
  • You put the commands in a file and execute the file. Commented Jul 3, 2019 at 4:02
  • Can you elaborate a little more? I already created my bash script and am running it in from the VS Code terminal. Once the bash script opens iTerm, how would I tell the original bash script to execute commands in iTerm? Commented Jul 3, 2019 at 4:09
  • Why do you want to execute the commands in another terminal? Just leave out this open iTerm2 stuff and just execute your cd's commands an npm start. Commented Jul 3, 2019 at 4:16
  • I’m developing a React Native app and a node.js server. I want the server to launch in a separate window from VS Code. My goal is to open my React Native project folder in VS code and run a bash script that starts the iOS & Android simulators—very easy—and then launch a separate terminal for my server Commented Jul 3, 2019 at 4:18
  • You'd be better off using screen or tmux to accomplish what you are trying to do. Commented Jul 3, 2019 at 4:26

4 Answers 4

6

My version, based in part on @ubuntudroid's comment:

function dovt
{
    osascript <<EOF
    tell application "iTerm2"
         create window with default profile
         tell current session of current window
              delay 1
              write text "cd $PWD"
              write text "$@"
          end tell
    end tell
EOF
}
Sign up to request clarification or add additional context in comments.

1 Comment

this worked inside a bash script. thankyou
2

The following bash script uses Applescript to launch iTerm. You can modify it to use iTerm2 instead of iTerm.

#!/bin/bash
#
# Open new iTerm window from the command line
#
# Usage:
#     iterm                   Opens the current directory in a new iTerm window
#     iterm [PATH]            Open PATH in a new iTerm window
#     iterm [CMD]             Open a new iTerm window and execute CMD
#     iterm [PATH] [CMD] ...  You can prob'ly guess
#
# Example:
#     iterm ~/Code/HelloWorld ./setup.sh
#
# References:
#     iTerm AppleScript Examples:
#     https://gitlab.com/gnachman/iterm2/wikis/Applescript
# 
# Credit:
#     Inspired by tab.bash by @bobthecow
#     link: https://gist.github.com/bobthecow/757788
#

# OSX only
[ `uname -s` != "Darwin" ] && return

function iterm () {
    local cmd=""
    local wd="$PWD"
    local args="$@"

    if [ -d "$1" ]; then
        wd="$1"
        args="${@:2}"
    fi

    if [ -n "$args" ]; then
        # echo $args
        cmd="; $args"
    fi

    osascript &>/dev/null <<EOF
        tell application "iTerm"
            activate
            set term to (make new terminal)
            tell term
                launch session "Default Session"
                tell the last session
                    delay 1
                    write text "cd $wd$cmd"
                end
            end
        end tell
EOF
}
iterm $@

5 Comments

I found this and tried it but isn't this the code for an AppleScript as opposed to a bash script?
Yes, the above code uses AppleScript. In their official site, they recommend using AppleScript.
For iTerm2 it would look like this: tell application "iTerm2" create window with default profile tell current session of current window delay 1 write text "cd $wd$cmd" end tell end tell - sorry for the missing linebreaks - SO commenting doesn't allow them.
iTerm's Applescript support is now deprecated. The supported alternative is to use the Python API. See iterm2.com/python-api/examples/launch_and_run.html.
Applescript won't go away—it's in maintenance mode. Use it if it solves your problem. When you run in to a wall, switch to Python.
1

This small program I built for myself do what you're looking for. It takes a configuration file like this:

{
  "tabs": [
    {
      "commands": ["cd /to/other/folder", "cd /to/project && npm start"]
    }
  ]
}

Comments

1

This option will also open the new window/tab with the current conda environment


iterm() {
local env_name="${CONDA_DEFAULT_ENV:-base}"
osascript <<EOF
    tell application "iTerm"
    activate

    if (count of windows) = 0 then
        set newWindow to create window with default profile
        set theSession to current session of newWindow
    else
        tell current window
        set newTab to create tab with default profile
        set theSession to current session of newTab
        end tell
    end if

    tell theSession
        write text "cd \"${PWD}\""
        write text "conda activate ${env_name}"
        write text "clear"
    end tell
    end tell
EOF
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.