Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mac2x

macrumors 65816
Original poster
Sep 19, 2009
1,146
0
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!
 
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!

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
 
Last edited:
Unfortunately, it did not work and it is still throwing the same error. :(
 
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$
 
Last edited:
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.
 
Last edited:
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.
 
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?
 
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:
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)[/QUOTE]

As others have noted, the OSAX module, which is used to access scripting additions, works in 32-bit ruby only. This is because the OSAX module historically retrieves scripting addition dictionaries via Carbon's now-deprecated OSAGetAppTerminology function, which isn't present in 64-bit OS X. 

Updating the OSAX module has been very low priority, since users can get equivalent functionality from standard and third-party Python/Ruby libraries.

(The main Appscript module retrieves application dictionaries via Apple events, so works just fine in both 32- and 64-bit OS X.)

However, the new app store's ban on deprecated APIs has finally provided motivation for a fix, and the Python and Ruby osax modules in the latest svn trunk (rev 720) no longer use the deprecated API. This also means that you can now use the OSAX module in 64-bit ruby (assuming, of course, that the osaxen you're using are 64-bit-capable as well).

The fix isn't perfect as it relies on OS X's OSACopyScriptingDefinition function, which doesn't always provide the correct information (bits of dictionaries sometimes go missing), and a somewhat crude parser that ignores the more awkward and less used parts of the sdef format (e.g. synonyms, XIncludes). But it should be good enough.

A file release should follow once the documentation is updated and some more testing is done. In the meantime, you can grab the latest rb-appscript trunk as follows:

[CODE]svn checkout \
    https://appscript.svn.sourceforge.net/svnroot/appscript/rb-appscript/trunk \
    rb-appscript-trunk

HTH
 
Thanks for the info!

One question (I haven't used svn much): is anything else needed after running that command? :eek:
 
Thanks for the info!

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

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
 
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.