He encontrado la manera de abrir una nueva pestaña en iTerm
:
newtabi()
{
osascript -e 'tell application "iTerm" to activate' -e 'tell application "System Events" to tell process "iTerm" to keystroke "t" using command down'
}
Y quiero ejecutar algunos comandos en una nueva pestaña. Que sea un comando simple pwd
. ¿Cómo hacerlo?
Si corro...
newtabi && pwd
La nueva pestaña se abre como se esperaba, pero pwd
el comando no se ejecuta en la nueva pestaña sino en la anterior, donde escribínewtabi && pwd
zsh
yo uso mi sistema operativo esOS X 10.8.5
Uso 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
, abre una nueva pestaña en la instancia correcta iTerm
, pero escribe pwd
en la última pestaña de la primera instancia de iTermterminal 1
a current terminal
. Ahora debería ejecutar el comando en la ventana actual en lugar de la ventana que se abrió primero.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)
en esto.key code 52
supone que debe hacer?A partir de al menos macos Mojave, iTerm 3.2.8, si ejecuta:
$ open -a iTerm .
Lo agregará como una pestaña a la ventana actual.
.
en la respuesta), lo ejecutará (después de un mensaje de confirmación de iTerm)No pude obtener la respuesta aceptada para trabajar. También quería pasar varios comandos. Esto es lo que se me ocurrió.
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 \"${@}\""
}
Ejemplo de uso
newtabi pwd
newtabi 'cd ~/ && pwd'
newtabi 'echo \"Hello New Tab\"'
Para acciones más complejas, recomendaría dividir los comandos.
code_folder="/path/to/code"
alias project="cd ${code_folder}/my-project/foo && yarn run start"
Después,newtabi project
pwd
para que el comando se vea así: `newtabi '$PWD/foo && yarn run start'?newtabi "$(pwd)/foo && yarn run start"
ttab
es una CLI de macOS y Linux para abrir una nueva pestaña/ventana de terminal, opcionalmente con un comando para ejecutar y/o mostrar la configuración
De sus muestras:
# 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'
En caso de que esté buscando la versión JXA:
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;
}
}
En caso de que también esté interesado en cómo usar los módulos ESM en JXA (a través de browserify), aquí hay un buen ejemplo: https://github.com/johnelm/node-jxa . Además , https://www.npmjs.com/package/esmify .
Después de la configuración, puede importar una clase reutilizable como se muestra a continuación:
import iTerm from "../lib/iTerm";
const app = new iTerm();
app.findOrCreateTab("MyTab", `cd "$HOME"`);
sorín
open -a iTerm
para ejecutar un comando.