Search code examples
osx-lionfindersourceforge-appscriptrb-appscript

How to get path of currently selected file in Finder with rb-appscript in OSX Lion


In OSX 10.6 I used the following code to do something to each currently selected file in Finder:

app('Finder').selection.get.each do |item|
  url =CGI::unescape(item.URL.get)
  puts url
  do_pdf(url[16..-1])
end

(this is launched by Keyboard Maestro). This worked perfectly fine, however in OSX Lion, the behaviour seems to have changed.

selection.get still returns the right reference app('Finder').selection.get[0] => app"/System/Library/CoreServices/Finder.app"disks["Home"]folders["stian"]folders["Downloads"]document_files["Comprehensive Examination (1).doc"]

however, when I try to extract it with URL.get, I get a weird number: app('Finder').selection.get[0].URL.get => "file:///.file/id=6637683.2924283"

How do I take the reference above and get a POSIX path as a text string (/Home/stian/Downloads/Comprehensive Examination (1).doc)?

I can get a text version of the entire reference like this: app('Finder').selection.get[0].get.to_s => "app("/System/Library/CoreServices/Finder.app").disks["Home"].folders["stian"].folders["Downloads"].document_files["Comprehensive Examination (1).doc"]"

so I guess I could parse this manually and construct the POSIX path, but that seems very cumbersome and brittle.

Thanks for any help!


Solution

  • In Python, it's

    app('Finder').selection.get(resulttype=k.alias)[0].path
    

    I guess in Ruby it would be

    app('Finder').selection.get(:result_type=>:alias)[0].path