öffne eine neue Registerkarte in iTerm und führe dort den Befehl aus


17

Ich habe einen Weg gefunden, um einen neuen Tab zu öffnen iTerm:

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

Und ich möchte einige Befehle in einem neuen Tab ausführen. Lass es ein einfacher Befehl sein pwd. Wie es geht?

Wenn ich renne ...

newtabi && pwd

Die neue Registerkarte wird wie erwartet geöffnet, aber der pwdBefehl wird nicht in der neuen, sondern in der alten Registerkarte ausgeführt, in der ich etwas eingegeben habenewtabi && pwd

Ich benutze zsh. Mein os istOS X 10.8.5

Antworten:


16

Verwendung 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"

1
Wenn mehr als ein iTermsTab geöffnet wird , wird der neue Tab in der richtigen iTermInstanz geöffnet , aber der pwdletzte Tab der ersten Instanz von iTerm
Maxim Yefremov wird

2
@efr Du hast recht. Ich bearbeitete die Antwort zu ändern , terminal 1zu current terminal. Jetzt sollte der Befehl im aktuellen Fenster anstelle des Fensters ausgeführt werden, das zuerst geöffnet wurde.
Lri

2
Dies funktionierte nicht auf Mac 10.11.6 mit iTerm2. Im Skripteditor werden "Unerwartetes Zeilenende, aber gefundener Bezeichner" und "Terminal" hervorgehoben
Mike Blandford,

1
Dies funktioniert unter macOS execution error: System Events got an error: osascript is not allowed to send keystrokes. (1002)
10.14

12
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'

Ich mache 148:156: syntax error: Expected end of line but found identifier. (-2741)das schon.
temporary_user_name

4

Ab mindestens MacOS Mojave, iTerm 3.2.8, wenn Sie Folgendes ausführen:

$ open -a iTerm .

Es wird als Registerkarte zum aktuellen Fenster hinzugefügt.


1

Ich konnte die akzeptierte Antwort nicht zur Arbeit bringen. Ich wollte auch mehrere Befehle übergeben. Das habe ich mir ausgedacht.

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 \"${@}\""
}

Anwendungsbeispiel

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

Für komplexere Aktionen würde ich empfehlen, die Befehle aufzubrechen.

code_folder="/path/to/code"

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

Dann, newtabi project


Können Sie mir bitte sagen, wie ich meinen Strom weitergeben kann pwd, damit der Befehl wie folgt aussieht: `newtabi '$ PWD / foo && Fadenlaufstart'?
Raj

1
@ Raj, gute Frage. Ich persönlich zerlege die Befehle in konsumierbare Aliase und / oder Funktionen. Ich habe ein weiteres Beispiel hinzugefügt. Ich hoffe es hilft!
Marc Barbeau

Vielen Dank, @Marc!
Raj
Durch die Nutzung unserer Website bestätigen Sie, dass Sie unsere Cookie-Richtlinie und Datenschutzrichtlinie gelesen und verstanden haben.
Licensed under cc by-sa 3.0 with attribution required.