Pergunta

Eu encontrei uma maneira de abrir uma nova guia em iTerm:

newtabi()
{
    osascript -e 'tell application "iTerm" to activate' -e 'tell application "System Events" to tell process "iTerm" to keystroke "t" using command down'
}

E quero executar alguns comandos em uma nova aba.Que seja um comando simples pwd.Como fazer isso?

Se eu correr...

newtabi && pwd

A nova guia é aberta conforme esperado, mas pwd O comando é executado não na nova aba, mas na antiga, onde digitei newtabi && pwd

eu uso zsh.Meu sistema operacional é OS X 10.8.5

Foi útil?

Solução

Usar tell session -1 of current terminal to write text "pwd":

activate application "iTerm"
tell application "System Events" to keystroke "t" using command down
tell application "iTerm" to tell session -1 of current terminal to write text "pwd"

Outras dicas

osascript \
-e 'tell application "iTerm" to activate' \
-e 'tell application "System Events" to tell process "iTerm" to keystroke "t" using command down' \
-e 'tell application "System Events" to tell process "iTerm" to keystroke "ls"' \
-e 'tell application "System Events" to tell process "iTerm" to key code 52'

Pelo menos a partir do macOS Mojave, iTerm 3.2.8, se você executar:

$ open -a iTerm .

Ele irá adicioná-lo como uma guia à janela atual.

Não consegui fazer com que a resposta aceita funcionasse.Eu também queria passar vários comandos.Isto é o que eu inventei.

newtabi(){  
  osascript \
    -e 'tell application "iTerm2" to tell current window to set newWindow to (create tab with default profile)'\
    -e "tell application \"iTerm2\" to tell current session of newWindow to write text \"${@}\""
}

Exemplo de uso

newtabi pwd
newtabi 'cd ~/ && pwd'
newtabi 'echo \"Hello New Tab\"'

Para ações mais complexas, recomendo dividir os comandos.

code_folder="/path/to/code"

alias project="cd ${code_folder}/my-project/foo && yarn run start"

Então, newtabi project

Licenciado em: CC-BY-SA com atribuição
Não afiliado a apple.stackexchange
scroll top