Using Ruby to close windows on Mac OS X

I want to programmatically close the window using Ruby on Mac OS X (ie send "COMMAND + W" to the window or press the red X button in the upper left corner).

I think there are such things for Windows (like win32api), but I don’t know how to do it on Mac OS X.

0
source share
1 answer

After some searches, the answer is: appscript .

Install it using ruby-gems :

$ sudo gem install rb-appscript

Make sure that the program you are trying to control supports AppleScripting . I am trying to control Google Sketchup, so I will introduce the following at the bash prompt:

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

Preview, , :

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

script appscript:

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

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

Source: https://habr.com/ru/post/1791808/


All Articles