如果没有打开终端应用程序,下面的代码将打开两个终端窗口。它为什么要这么做?我只想打开一扇窗。
如果只打开了一个终端窗口,则下面的代码仅打开另一个窗口。
NSAppleScript* terminal = [[NSAppleScript alloc] initWithSource:
[NSString stringWithFormat:
@"tell application \"Terminal\"\n"
@" activate\n"
@" do script \"echo %@\"\n"
@" tell the front window\n"
@" set title displays shell path to false\n"
@" set title displays custom title to true\n"
@" set custom title to \"My session! %@\"\n"
@" end tell\n"
@"end tell", name, name]];
[terminal executeAndReturnError:nil];发布于 2012-03-08 12:01:53
正如您所编写的,do script命令将始终在新窗口中运行。如果您希望它在特定的窗口中运行,请使用以下格式:do script (...) in (window...)。终端的in语法还可以处理选项卡中的运行脚本。
例如,如果您想在最前面的窗口中运行脚本,您可以编写do script "echo Hello, world!" in front window。
编辑:后续,如果您希望始终在窗口中运行脚本(如果没有打开任何窗口,则创建一个新的脚本),您可以使用以下AppleScript:
tell application "Terminal"
activate
if length of (get every window) is 0 then
tell application "System Events" to tell process "Terminal" to click menu item "New Window" of menu "File" of menu bar 1
end if
do script "echo Hello, world!" in front window
end tell当然,您需要在NSArray中正确地对其进行转义,但我相信您可以做到。
发布于 2012-03-08 12:57:27
下面是我的解决方案进行比较:
tell application "Terminal"
if it is running then
do script "echo %@"
else
activate
do script "echo %@" in front window
end if
tell the front window
set title displays shell path to false
set title displays custom title to true
set custom title to "My Terminal!"
end tell
end tellhttps://stackoverflow.com/questions/9612461
复制相似问题