Skip to content

Instantly share code, notes, and snippets.

@tmarshall
Created March 8, 2018 00:31
Show Gist options
  • Save tmarshall/dceaee676a8bec5689d74ba2fc0ef01a to your computer and use it in GitHub Desktop.
Save tmarshall/dceaee676a8bec5689d74ba2fc0ef01a to your computer and use it in GitHub Desktop.
Example JXA script to kick off multiple terminal processes
#!/usr/bin/env osascript -l JavaScript
/*
osascript -l JavaScript <this-file.js>
see https://github.com/JXA-Cookbook/JXA-Cookbook/wiki/
*/
const System = Application('System Events')
const Terminal = Application('Terminal')
function message(msg) {
console.log('🧟 ' + msg)
}
function currentWindow() {
return Terminal.windows().find(win => win.frontmost() === true)
}
Terminal.activate()
const tabs = {
master: currentWindow(),
one: null,
two: null
}
message('Spawning tabs')
System.keystroke('t', {
using: 'command down'
})
tabs.one = currentWindow()
System.keystroke('t', {
using: 'command down'
})
tabs.two = currentWindow()
// re-focus initial tab
tabs.master.frontmost = true
message('Starting app one')
Terminal.doScript('cd ~/', {
in: tabs.one
})
Terminal.doScript('echo "whatever"', {
in: tabs.one
})
message('Starting app two')
Terminal.doScript('cd ~/', {
in: tabs.two
})
Terminal.doScript('echo "another"', {
in: tabs.two
})
message('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment