views:

154

answers:

4

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.
+2  A: 

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
sakra
Great, this works. I suppose there's no way to get a list of furls?
kch
I didn't find a way. Retrieving the clipboard contents as a list will just give you a list with one text element that contains all the file names.
sakra
A: 

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
kch
+1  A: 

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
regulus6633
Doesn't seem to work for me. posixPaths always ends up empty.
kch
Anyway, if I understand what's going on, this seems flaky. What happens if I copy multiple entries from a search result window?
kch
It works when you copy files or folders from a normal Finder window. Search windows aren't normal windows and don't work the same... so this won't work on them.
regulus6633
A: 

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"))
kch