首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么用NSAppleScript打开两个终端窗口?

为什么用NSAppleScript打开两个终端窗口?
EN

Stack Overflow用户
提问于 2012-03-08 11:22:43
回答 2查看 549关注 0票数 0

如果没有打开终端应用程序,下面的代码将打开两个终端窗口。它为什么要这么做?我只想打开一扇窗。

如果只打开了一个终端窗口,则下面的代码仅打开另一个窗口。

代码语言:javascript
复制
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];
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-08 12:01:53

正如您所编写的,do script命令将始终在新窗口中运行。如果您希望它在特定的窗口中运行,请使用以下格式:do script (...) in (window...)。终端的in语法还可以处理选项卡中的运行脚本。

例如,如果您想在最前面的窗口中运行脚本,您可以编写do script "echo Hello, world!" in front window

编辑:后续,如果您希望始终在窗口中运行脚本(如果没有打开任何窗口,则创建一个新的脚本),您可以使用以下AppleScript:

代码语言:javascript
复制
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中正确地对其进行转义,但我相信您可以做到。

票数 2
EN

Stack Overflow用户

发布于 2012-03-08 12:57:27

下面是我的解决方案进行比较:

代码语言:javascript
复制
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 tell
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9612461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档