3

我想从最好的 Ruby 访问几个不同的 Mac OS X 应用程序,但我会选择 PHP。这些应用程序是 Elgato 的 turbo.264 和 Apple 的 iTunes。两者都定义了 Applescript 库,可以让我从 Applescript 做我想做的事情,但我不想在 Applescript 中这样做。如果我不能在 Ruby 或 PHP 中做到这一点,也许我可以在目标 C/Cocoa 中做到这一点,并创建某种可以从 Ruby 调用的包装器。

这甚至可能吗?似乎这些方法在 Applescript 中可用,它们应该在其他语言中可用,我只是找不到任何东西。

4

3 回答 3

4

试试 RubyOSA ( http://rubyosa.rubyforge.org ) 然后你可以这样做:

require 'rbosa'
itunes = OSA.app('iTunes')

track = itunes.current_track
p track                     # <OSA::Itunes::FileTrack:0x1495e20>
p track.name                # "Over The Rainbow" 
p track.artist              # "Keith Jarrett" 
p track.duration            # 362.368988037109 
p track.date_added.to_s     # "2006-06-30" 
p track.enabled?            # true

# Play the selected track.
itunes.play                    

# Fade the volume.
100.times { |i| itunes.sound_volume = i; sleep 0.1 }  

# Set iChat's status message to the current track.
OSA.app('iChat').status_message = "Playing: #{track.name}"

您可以与任何支持 AppleScript 的 Mac OS X 应用程序对话

于 2009-08-15T01:33:10.480 回答
3

尝试过 appscript ( http://appscript.sourceforge.net/rb-appscript/index.html )?

该网站的示例:

而不是AppleScript

tell application "TextEdit"
    get paragraph 1 of document "ReadMe"
end tell

你用Ruby 写:

app('TextEdit').documents['ReadMe'].paragraphs[1].get
于 2009-08-15T00:50:18.477 回答
3

Mac OS X 10.5(当前版本)包括Scripting Bridge,一个允许您从 Cocoa 应用程序访问 AppleScript 功能的框架。您还可以从 RubyCocoa 和 PyObjC 访问它。基本上,它是这样工作的:

require 'osx/cocoa'
require_framework 'ScriptingBridge'
include OSX

iTunes = SBApplication.applicationWithBundleIdentifier 'com.apple.iTunes'
iTunes.activate
puts "Play #{iTunes.currentTrack.name}?"
iTunes.playpause if gets.strip == "Yes"
于 2009-08-15T04:35:45.307 回答