Copy a file's NSURL to clipboard

am trying to do a rather simple thing: in the context menu I want to have an action (e.g. with Automator) that gets and copies the file’s NSURL to clipboard.

The solution seems simple, but it does not work (some unspecific error). Here is what I have so far:

 on run {input, parameters}

       set p to POSIX path of (input)
       set nsu to current application's |NSURL|'s fileURLWithPath:p

       return nsu

    end run

I am looking to get the file reference URL, like so: file:///.file/id=6571367.2773272/

I am on El Capitan.

Any ideas?

NSURL has a method fileReferenceURL()


use framework "Foundation"

set p to POSIX path of (input)
set nsu to current application's |NSURL|'s fileURLWithPath:p

return nsu's fileReferenceURL()

consider that input might contain a list of items.
In this case either use a repeat loop to collect all items or an index e.g. item 1 of to get a specific item

Hi StefanK.

Thanks for the fix. Now it does not return anything yet. If i return p it copies the full path to clipboard, but using nsu’s fileReferenceURL() does not produce anything. Ideas?

Greetings

What do you expect to be on the clipboard?

NSURL is a Cocoa object, which is not readable by AppleScript.
if you want to have a (AppleScript) readable string representation use

return nsu's fileReferenceURL()'s absoluteString() as text

Thanks, that did the trick. I would want “file:///.file/id=6571367.3777118” on the clipboard. And with your tweak it does just that. :cool: