用Ruby,收在Mac OS X的窗口 [英] Using Ruby to close windows on Mac OS X

查看:203
本文介绍了用Ruby,收在Mac OS X的窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编程方式关闭使用Ruby在Mac OS X的窗口(即发送命令+ W的窗口,或者在左上角点击红色的X按钮)。

I want to programmatically close a window using Ruby on Mac OS X (i.e. send "COMMAND+W" to the window, or click on the red X button in the top left corner).

我觉得有这样的事情的Windows(例如WIN3​​2API),但我不知道怎么做了Mac OS X上。

I think there are things like this for Windows (e.g. win32api), but I do not know how to do it on Mac OS X.

推荐答案

一些google搜索后,答案是: appscript

After some googling, the answer is: appscript.

使用安装它红宝石宝石的:

$ sudo gem install rb-appscript

请确保您试图控制程序支持AppleScripting 。我试图控制谷歌Sketchup的,所以我键入以下到bash提示符:

Make sure the program you are trying to control supports AppleScripting. I'm trying to control Google Sketchup, so I type the following into the bash prompt:

$ defaults write /Applications/Google\ SketchUp\ 8/SketchUp.app/Contents/Info NSAppleScriptEnabled -bool YES

如果你正在控制preVIEW,例如,你将改为键入以下内容:

If you were controlling Preview, for instance, you would instead type the following:

$ defaults write /Applications/Preview.app/Contents/Info NSAppleScriptEnabled -bool YES

然后用appscript相应的头使你的脚本:

Then make your script with the appropriate headers for appscript:

#!/usr/bin/ruby
require 'rubygems'
require 'appscript'
include Appscript   # note the lack of quotes

app('SketchUp').windows[0].close   # closes the window

这篇关于用Ruby,收在Mac OS X的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆