Skip to content

Instantly share code, notes, and snippets.

@phdoerfler
Created January 17, 2019 21:26
Show Gist options
  • Save phdoerfler/3744a84633fb66f3b2dee35f0965017d to your computer and use it in GitHub Desktop.
Save phdoerfler/3744a84633fb66f3b2dee35f0965017d to your computer and use it in GitHub Desktop.
Run a command in a new iTerm tab
use std::io::{self, Write};
use std::process::{exit, Command, Stdio};
use std::env;
fn main() {
main2().unwrap_or_else(|e| {
eprintln!("{}", e);
exit(1);
})
}
fn main2() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
let current_dir = env::current_dir().unwrap();
let dir = if args.len() > 2 { &args[1] } else { current_dir.to_str().unwrap() };
let cmd = if args.len() > 2 { &args[2] } else { &args[1] };
let script = format!(r#"
tell application "iTerm2"
tell current window
create tab with default profile
tell the current session
write text "cd \"{}\" && {}"
end tell
end tell
end tell"#, dir, cmd);
let mut child = Command::new("osascript")
.arg("-i")
.stdin(Stdio::piped())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.expect("failed to execute process");
{
let child_stdin = child.stdin.as_mut().unwrap();
child_stdin.write_all(script.as_bytes())?;
}
child.wait()?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment