我正在使用JavaScript在OS上实现自动化。
我正在尝试在终端应用程序中打开一个新的选项卡。这是我到目前为止所得到的:
var Terminal = Application('Terminal);
var Tab = Terminal.Tab;
// Activate the Terminal App, creates a new window if there isn't one already
Terminal.activate();
// This contains all the windows
Terminal.windows;
// This contains the first window
Terminal.windows.at(0) // alternatively, Terminal.windows[0]
// This contains the tabs in the first window
Terminal.windows.at(0).tabsTerminal.windows.at(0).tabs本质上是一个数组。它有一个.push方法。我假设我可以使用以下语句向窗口添加一个选项卡:
Terminal.windows.at(0).tabs.push(new Tab());但是它抛出了一个非常普遍的错误:
Error -10000: AppleEvent handler failed.文档严重缺乏,我认为这个自动化的JavaScript只是一个吸引JavaScript开发人员的花招。
注意:我见过的AppleScript解决方案基本上只是告诉System Events应用程序按下Command +T打开一个新的选项卡。这感觉非常麻烦,并使Command +T硬编码在其中。
发布于 2015-01-13 22:39:15
以下代码适用于chrome和safari,但不适用于terminal,我仍在找出原因,看看这些信息是否有帮助。
chrome = Application("Google Chrome")
newTab = chrome.Tab()
chrome.windows[0].tabs.push(newTab)发布于 2015-09-19 11:31:25
看看以下方法是否适用于您的情况:
var system = Application('System Events');
var terminal = Application('Terminal');
// tell application "Terminal" to activate
terminal.activate();
// tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
system.keystroke('t', {using: 'command down'});发布于 2016-02-28 09:59:02
您可以模拟一个新选项卡的快捷方式。还需要声明目标选项卡
tell application "System Events" to keystroke "t" using {command down}查看具有两个或更多选项卡的示例
teel application "Terminal"
do script "cd ~/ && ls" in tab 1 of front window
tell application "System Events" to keystroke "t" using {command down}
do script "cd ~/Applications && ls" in tab 2 of front window
end tellhttps://stackoverflow.com/questions/27451735
复制相似问题