Ruby/appscript problem

Discussion in 'Mac Programming' started by mac2x, Dec 9, 2010.

  1. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #1
    Trying to learn OS X application scripting with Ruby and appscript, but have run into a problem with a small example meant to demonstrate the various methods of getting OS X application objects.

    Code:
    #!/usr/bin/ruby
    
    # this ruby script demonstrates different methods of getting OS X application
    # objects using appscript (of class Appscript::Application). The method new of 
    # this class is private; thus we use another method called Appscript.app. This
    # calls Appscript::GenericApplication.new for you and generates an instance of
    # class Appscript::Application.
    
    # by_name
    # by_id
    # by_creator
    # by_pid
    # by_url
    
    require 'rubygems'
    require 'appscript'
    require 'osax'
    
    ## by_name
    
    Appscript.app.by_name("iTunes")
    
    # may be shortened thus:
    
    Appscript.app("iTunes.app")
    
    ## by_id
    
    puts OSAX.osax.info_for(
    	MacTypes::Alias.path("/Applications/iTunes.app"))[:bundle_identifier]
    #=> com.apple.iTunes
    That last line (trying to do it by bundle ID) throws this error:

    Code:
    /Library/Ruby/Gems/1.8/gems/rb-appscript-0.5.3/lib/osax.rb:153:in `initialize': OSAX::ScriptingAddition can't dynamically retrieve scripting addition terminology within a 64-bit process. (RuntimeError)
    	from /Library/Ruby/Gems/1.8/gems/rb-appscript-0.5.3/lib/osax.rb:102:in `new'
    	from /Library/Ruby/Gems/1.8/gems/rb-appscript-0.5.3/lib/osax.rb:102:in `osax'
    	from app_objects.rb:29
    FWIW, I run the x64 kernel in 10.6.5. This is NOT school, BTW. Just doing this for fun. :)

    Thanks!
     
  2. ulbador, Dec 10, 2010
    Last edited: Dec 10, 2010

    ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    Force Ruby to run in 32 bit mode. This is required because Ruby probably has a 32 bit and 64 bit component. By default, because Snow Leopard runs in 64 bit, Ruby wants to start up in 64 bit. This would work, but I assume not all the components of Ruby are compiled to be 64 bit compatible. So you have to tell Ruby to run in 32 bit.

    Replace the first line of your script(#!/usr/bin/ruby) with:

    #!/usr/bin/arch -i386 ruby
     
  3. mac2x thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #3
    Unfortunately, it did not work and it is still throwing the same error. :(
     
  4. ulbador, Dec 10, 2010
    Last edited: Dec 10, 2010

    ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #4
    How are you actually running it?

    The purpose of that first line is so you can chmod the file to a+x and then just type

    ./myRubyScript.rb

    If you are still typing:

    ruby myRubyScript.rb

    then you will have to modify what you are typing to run it.

    /usr/bin/arch -i386 ruby myRubyScript.rb

    With this ruby file:

    Code:
    hello.rb:
    
    #!/usr/bin/arch -i386 ruby
    puts 'Hello World'
    

    This will run with 32 bit ruby, which is what you want
    Code:
    daMac:Desktop adam$ chmod a+x hello.rb 
    daMac:Desktop adam$ ./hello.rb 
    Hello World
    daMac:Desktop adam$
    
    This runs it with 64 bit ruby. The first line is ignored because you aren't executing it via bash. You are executing it via ruby.
    Code:
    daMac:Desktop adam$ ruby hello.rb 
    Hello World
    daMac:Desktop adam$
    
    You would have to run it like this:

    Code:
    daMac:Desktop adam$ /usr/bin/arch -i386 ruby hello.rb
    Hello World
    daMac:Desktop adam$
    
     
  5. ulbador, Dec 10, 2010
    Last edited: Dec 10, 2010

    ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #5
    I'm putting this as a new reply as it's totally different info.

    I'm not sure exactly what you are trying to accomplish, but:

    You might look into using the ScriptingBridge with RubyCocoa. It's included with XCode and simple to use. I've used the same thing in Objective C and it works amazing.

    Code:
    require 'osx/cocoa'
    include OSX
    OSX.require_framework 'ScriptingBridge'
     
    iTunes = SBApplication.applicationWithBundleIdentifier_("com.apple.iTunes")
    iTunes.sources.each do |source|
      puts source.name
      source.playlists.each do |playlist|
        puts " -> #{playlist.name}"
        playlist.tracks.each do |track|
          puts "      -> #{track.name}" if track.enabled?
        end
      end
    end
    
    With this, you don't have to worry about 32vs64 bit and everything you can do with AppleScript, you can do via a very simple interface. It's also an Apple native interface so you don't have to worry about compatibility and all that other crap. With the above example, you can pull out all the tracknames in your iTunes in playlists. You can do the same just as easily in any other scriptable program just by pulling the right application bundle. The other huge advantage of this is that you can create OSX-native GUI applications in Ruby.

    Here is some more info and the link from where I pulled that example from:

    http://developer.apple.com/library/...ythonCocoa/Articles/UsingScriptingBridge.html

    Again, I don't know what you are looking to accomplish, but this may be a better way.
     
  6. mac2x thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #6
    Ahh, thanks ulbador! I had indeed forgotten to make it executable. Now it works. :) Thanks for all the examples too. Very helpful!

    I'll look into the info on the ScriptingBridge thing. Right now, I'm working out the basics of how all this functions.
     
  7. mac2x thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #7
    Really liking the Scripting Bridge better than appscript so far....but it's tossing a lot of Cocoa stuff at me (reading the OS X Reference Library); honestly a lot more than I know.

    Any hints?
     
  8. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #8
     
  9. mac2x thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #9
    Thanks for the info!

    One question (I haven't used svn much): is anything else needed after running that command? :eek:
     
  10. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #10
    The above command will create a directory named 'rb-appscript-trunk' in your working directory and pull all of the files from the rb-appscript trunk into it.

    You'll need to cd into the rb-appscript-trunk directory and build and install it. For a non-gem installation:

    Code:
    ruby extconf.rb
    make
    sudo make install
    For RubyGems, you need to run the following to create the .gem file and then install it:

    Code:
    gem build rb-appscript.gemspec
    sudo gem install ./rb-appscript-0.5.3.gem
     
  11. mac2x thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #11
    Thanks! It works well. :) Now I can remove the arch -i386 from my scripts.

    Off topic question, do you work on the rb-appscript project?
     
  12. hhas macrumors regular

    Joined:
    Oct 15, 2007
    #12
    Yes, I wrote it.
     
  13. mac2x thread starter macrumors 65816

    Joined:
    Sep 19, 2009

Share This Page