[英]Open iTerm2 from bash script & Run commands
Summary概括
I'm trying to write a script to automatically launch my development server in iTerm2 from VS Code.我正在尝试编写一个脚本来从 VS Code 在 iTerm2 中自动启动我的开发服务器。
When I open my VS Code project, I want my bash script to:当我打开我的 VS Code 项目时,我希望我的 bash 脚本:
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.我知道如何打开 iTerm2,但我不知道如何制作我编写的 bash 脚本,然后在 iTerm2 中运行 #2 和 #3 中的命令,因为我需要从 VS Code 终端运行 bash 脚本,然后打开 iTerm2 .
Hope It helps you.希望对你有帮助。 You may use the command iTerm2 instead of iTerm.
您可以使用命令 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 $@
Shameless plug, but you can try using this small program I built for myself that'll easily let you do what you're looking to do.无耻的插件,但是您可以尝试使用我为自己构建的这个小程序,它可以轻松地让您做您想做的事。
It will let you specify a configuration file like this:它会让你指定一个这样的配置文件:
{
"tabs": [
{
"commands": ["cd /to/other/folder", "cd /to/project && npm start"]
}
]
}
That will do what you want :-)那会做你想做的:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.