ruby - 使用 MacRuby 和脚本桥关闭窗口

标签 ruby cocoa macruby scripting-bridge

我正在尝试编写一个快速脚本,该脚本将关闭终端应用程序除当前窗口之外的所有窗口。该脚本使用 MacRuby 和脚本桥。这是我到目前为止所拥有的:

#!/usr/local/bin/macruby

framework "Foundation"
framework "ScriptingBridge"

terminal = SBApplication.applicationWithBundleIdentifier("com.apple.Terminal")
terminal.windows[2].close while terminal.windows.count > 1

当我尝试运行此程序时,出现以下错误:

undefined method `close' for #<TerminalWindow:0x40033b0e0> (NoMethodError)

这种方法似乎与 AppleScript 配合得很好。有人知道为什么这里失败吗?

最佳答案

我找到了 TerminalWindow API 的副本 here 。使用它,我需要做的就是:

TerminalSaveOptionsNo = 'no  '.unpack('N').first
terminal.windows[1].closeSaving(TerminalSaveOptionsNo, savingIn: nil)

关于ruby - 使用 MacRuby 和脚本桥关闭窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12774308/

相关文章:

ruby - Xcode 4.4 + MacRuby = "macruby.h not found"(OS X 10.8 Mountain Lion)

cocoa - 如何使用 MacRuby 读取全局选择?

javascript - "TypeError: undefined is not a constructor"与 capybara (PhantomJS)

iphone - 如何按 NSString 长度对 NSMutableArray 进行排序?

Ruby:如何处理失败或无效的初始化

macos - 为什么我的 Mac 应用程序没有出现在 'LaunchPad' 中?

iphone - NSMutableArray 在索引处从其他数组添加对象

ruby - 安装 ruby​​ 1.9.3 时遇到问题

Ruby - 系统命令 - ps aux

ruby-on-rails - 基于参数的动态 ActiveRecord 查找器方法链接