开发者

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

开发者 https://www.devze.com 2023-03-26 18:58 出处:网络
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|

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/Core开发者_如何学GoServices/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!


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号