Ich habe eine Möglichkeit gefunden, einen neuen Tab zu öffnen in 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 pwd
der Befehl wird nicht in der neuen Registerkarte ausgeführt, sondern in der alten, in die ich eingegeben habenewtabi && pwd
Ich benutze zsh
. Mein Betriebssystem istOS X 10.8.5
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"
iTerms
neue Registerkarte in der richtigen iTerm
Instanz geöffnet, aber pwd
in die letzte Registerkarte der ersten Instanz von iTerm geschriebenterminal 1
Ich habe die Antwort bearbeitet, um sie zu ändern current terminal
. Jetzt sollte der Befehl im aktuellen Fenster ausgeführt werden und nicht im Fenster, das zuerst geöffnet wurde.execution error: System Events got an error: osascript is not allowed to send keystrokes. (1002)
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'
148:156: syntax error: Expected end of line but found identifier. (-2741)
darauf ein.key code 52
soll man machen?Ab mindestens macos Mojave, iTerm 3.2.8, wenn Sie Folgendes ausführen:
$ open -a iTerm .
Es wird es als Registerkarte zum aktuellen Fenster hinzufügen.
.
in der Antwort) einen Pfad zu einer Binärdatei übergeben, wird diese ausgeführt (nach einer Bestätigungsaufforderung von iTerm)Ich konnte die akzeptierte Antwort nicht zum Laufen 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 \"${@}\""
}
Beispielnutzung
newtabi pwd
newtabi 'cd ~/ && pwd'
newtabi 'echo \"Hello New Tab\"'
Für komplexere Aktionen würde ich empfehlen, die Befehle aufzuteilen.
code_folder="/path/to/code"
alias project="cd ${code_folder}/my-project/foo && yarn run start"
Dann,newtabi project
pwd
, damit der Befehl so aussieht: `newtabi '$PWD/foo && Garnlaufstart'?newtabi "$(pwd)/foo && yarn run start"
ttab
ist eine MacOS- und Linux-CLI zum Öffnen eines neuen Terminal-Tabs/Fensters, optional mit einem Befehl zum Ausführen und/oder Anzeigen von Einstellungen
Aus seinen Proben:
# Open a new tab, execute a command, wait for a keypress, and exit.
ttab 'ls "$HOME/Library/Application Support"; echo Press a key to exit.; read -rsn 1; exit'
Falls Sie nach der JXA-Version suchen:
class iTerm {
constructor() {
this.app = Application("iTerm");
this.window = this.app.currentWindow();
}
findOrCreateTab(tabName, command) {
this.app.activate();
let existingTab = this.findTab(tabName);
if (existingTab == undefined) {
existingTab = this.createTab(tabName);
console.log(`Created new tab: ${tabName}`);
existingTab.currentSession().write({ text: command });
} else {
existingTab.select();
console.log(`Found existing tab: ${tabName}`);
}
return existingTab;
}
createTab(name) {
const newTab = this.window.createTabWithDefaultProfile();
const session = newTab.currentSession();
session.name = name;
// console.log(newTab.index());
// console.log(newSession.name());
// console.log(newSession.profileName());
return newTab;
}
findTab(name) {
const tabs = this.window.tabs();
for (const tab of tabs) {
let session = tab.currentSession();
// Strange, but iTerm uses `profileName` instead of `name` for keeping manually assigned session name.
const tabName = session.profileName();
if (tabName === name) {
return tab;
}
}
return undefined;
}
}
Falls Sie auch daran interessiert sind, wie man ESM-Module in JXA (über Browserify) verwendet, dann ist hier ein gutes Beispiel: https://github.com/johnelm/node-jxa . Plus https://www.npmjs.com/package/esmify .
Nach dem Einrichten können Sie wiederverwendbare Klassen wie unten gezeigt importieren:
import iTerm from "../lib/iTerm";
const app = new iTerm();
app.findOrCreateTab("MyTab", `cd "$HOME"`);
Sorin
open -a iTerm
, um einen Befehl auszuführen.