AppleScript: I copied the Finder selection to the clipboard. Can I get the full path to the copied items?
I have one or many files and/or folders selected in the Finder. I manually copy them to the clipboard/pasteboard (⌘C).
To keep things simple, let's say I just copied one single normal file. The ideal solution, however, would deal with many files and a mixed selection with folders, aliases.
Now that this file is on the clipboard, I want to get its full path (preferably the POSIX path).
To save you time:
- I'm looking for an AppleScript (or rb-appscript) solution.
- I don't want to get the path directly from the selection. It must be from the item on the clipboard.
- Really, I know I can route around this by copying the path(s) to the selection first, then doing whatever I'm up to with it.
What I know so far (noted in rb-appscript):
OSAX.osax.the_clipboard
has a string of file names without path.开发者_如何学运维Appscript.app('Finder').clipboard.get
is apparently not implemented (dictionary says "NOT AVAILABLE YET"; calling it returns:missing_value
.
The following AppleScript seems to do the trick:
POSIX path of (the clipboard as «class furl»)
If there are multiple items on the clipboard, it will return the POSIX path of the first item only.
Also see the AppleScript command reference for the command the clipboard
.
rb-appscript version:
OSAX.osax.the_clipboard(:result_type => :file_url).path
Here's an applescript that will get all of the posix paths from the clipboard, not just the first one...
set theFiles to paragraphs of (get the clipboard)
set posixPaths to {}
repeat with aFile in theFiles
try
tell application "Finder" to set thePath to item aFile as text
set end of posixPaths to (POSIX path of thePath)
end try
end repeat
return posixPaths
The Finder being what it is, and AppleScript being what it is, it's all too much fail to go around. So, what the hell, I jumped into Cocoa.
Either of these scripts will return the list of absolute paths each on a new line.
MacRuby:
#!/usr/bin/env macruby
# encoding: UTF-8
framework 'Cocoa'
puts NSPasteboard.generalPasteboard.pasteboardItems
.map { |pbi| pbi.stringForType('public.file-url') }.compact
.map { |url| NSURL.URLWithString(url).path }
Nu:
#!/usr/bin/env nush
(puts ((((((NSPasteboard generalPasteboard) pasteboardItems)
map: (do (pbi) (pbi stringForType: "public.file-url")))
select: (do (url) (url)))
map: (do (url) ((NSURL URLWithString: url) path))) componentsJoinedByString: "\n"))
just thought I'd share the rb-appscript code that I wrote after sakra's answer:
#!/usr/bin/arch -i386 /usr/bin/ruby
require "rubygems"
require "osax"
include OSAX
def path_from_clipboard
osax.clipboard_info.flatten.include? :file_url or raise "clipboard does not contain path data"
osax.the_clipboard.count("\r") == 0 or raise "clipboard contains more than one path"
osax.the_clipboard(:result_type => :file_url).path
end
puts path_from_clipboard
I was looking for a solution that would copy the path of the files selected in the Finder. Here is what I came up with :
set ASTID to AppleScript's text item delimiters --——>>
set AppleScript's text item delimiters to return
tell application "Finder" to set sel to the selection as text
set listPaths to {}
repeat with pth in paragraphs of sel
set end of listPaths to POSIX path of pth
end repeat
set listPathsClipboard to listPaths as text
set AppleScript's text item delimiters to ASTID --——<<
set the clipboard to listPathsClipboard
精彩评论