我已经在我的 Rails 项目中安装了 rb-scpt gem 并运行了 bundle。gem 是 rb-appscript 的一个分支。
从终端运行时,以下程序有效:
#!/usr/bin/env ruby
# List names of playlists in iTunes.
require "rb-scpt"
itu = Appscript.app("iTunes")
p itu.sources[1].user_playlists.name.get
我创建了一个控制器来做同样的事情:
class ItunesController < ApplicationController
def play
itu = ::Appscript.app('iTunes')
play_list = itu.sources[1].user_playlists.name.get
render text(play_list)
end
end
当我运行它时,我在第 3 行得到一个错误:
#<NameError: uninitialized constant ItunesController::Appscript>
我错过了什么?我确定这很简单。
我使用 RubyMine 作为我的 IDE。当我输入 ::Appscript. 时,我开始从 IDE 获取代码提示,说源是 Appscript。这是 gem 中 rb-scpt.rb 的摘录:
module Appscript
...
class GenericApplication < GenericReference
def initialize(app_class)
@_app_class = app_class
super(['app'])
end
def by_name(name, terms=true)
return @_app_class.by_name(name, terms)
end
def by_id(id, terms=true)
return @_app_class.by_id(id, terms)
end
def by_creator(creator, terms=true)
return @_app_class.by_creator(creator, terms)
end
def by_pid(pid, terms=true)
return @_app_class.by_pid(pid, terms)
end
def by_url(url, terms=true)
return @_app_class.by_url(url, terms)
end
def by_aem_app(aem_app, terms=true)
return @_app_class.by_aem_app(aem_app, terms)
end
def current(terms=true)
return @_app_class.current(terms)
end
end
#######
AS_App = Appscript::GenericApplication.new(Application)
AS_Con = Appscript::GenericReference.new(['con'])
AS_Its = Appscript::GenericReference.new(['its'])
######################################################################
# REFERENCE ROOTS
######################################################################
# public (note: Application & GenericApplication classes may also be accessed if subclassing Application class is required)
def Appscript.app(*args)
if args == []
return AS_App
else
return AS_App.by_name(*args)
end
end