linux 从bash脚本和运行命令打开iTerm2

h7appiyu  于 2023-02-15  发布在  Linux
关注(0)|答案(3)|浏览(209)

摘要

我正在尝试编写一个脚本,以便从VS代码中自动启动iTerm2中的开发服务器。
当我打开VS Code项目时,我希望bash脚本执行以下操作:
1.打开iTerm2(
1.运行cd .. && cd其他文件夹
1.运行npm start(这样我的节点服务器将启动)

问题

我知道如何打开iTerm2,但我不知道如何创建我编写的bash脚本,然后在iTerm2中运行#2和#3中的命令,因为我需要从VS Code终端运行bash脚本,然后打开iTerm2。

iszxjhcz

iszxjhcz1#

希望对你有帮助。你可以用iTerm2命令代替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 $@
x3naxklr

x3naxklr2#

无耻的插件,但你可以尝试使用这个small program我为自己建立,这将很容易让你做你想做的事情。
它将允许您指定如下配置文件:

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

那会做你想要的:-)

smtd7mpg

smtd7mpg3#

我的版本部分基于@ubuntudroid的评论:

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
}

相关问题