Created
March 5, 2017 16:21
-
-
Save yantze/cdbc9f2ac4c3433790086049718162b2 to your computer and use it in GitHub Desktop.
JavaScript for Automator 在当前命令行运行指定命令
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
run command in current Terminal.app | |
how to run: | |
osascript ./runTerminal.js | |
osascript -l JavaScript -e 'Application("iTunes").currentTrack.name()' | |
*/ | |
function commandExistWindow(command) { | |
var Terminal = Application('Terminal') | |
Terminal.activate() | |
try { | |
currentWindow = Terminal.windows.at(0) | |
currentTab = currentWindow.selectedTab() | |
Terminal.doScript(command, { | |
in: currentTab | |
}) | |
} catch(err) { | |
// console.log(err) // no window | |
Terminal.doScript(command); | |
} | |
} | |
var cmd = `echo "hello world"` | |
commandExistWindow(cmd) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment