Search code examples
ruby-on-railsmacossourceforge-appscript

Using Appscript with Rails


I've installed the rb-scpt gem in my Rails project and ran bundle. The gem is a fork from rb-appscript.

The following program works when run from Terminal:

#!/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

I created a controller to do the same thing:

class ItunesController < ApplicationController
  def play
    itu = ::Appscript.app('iTunes')
    play_list = itu.sources[1].user_playlists.name.get
    render text(play_list)
  end
end

When I run it, I get an error on line 3:

#<NameError: uninitialized constant ItunesController::Appscript>

What am I missing? I'm sure it's something simple.

I'm using RubyMine as my IDE. When I enter ::Appscript., I start to get code hinting from the IDE saying the source is Appscript. This is an excerpt from rb-scpt.rb in the gem:

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

Solution

  • Try using the following line itu = ::Appscript.app("iTunes")