Saturday, December 16, 2017

#1 2015-10-14 10:54:59 am

hosea46
Member
Registered: 2015-10-14
Posts: 3

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:

   

Applescript:

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?


Filed under: Automator, NSURL, copy-paste

Offline

 

#2 2015-10-14 11:05:29 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Copy a file's NSURL to clipboard

NSURL has a method fileReferenceURL()

Applescript:


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


regards

Stefan

Offline

 

#3 2015-10-14 12:01:54 pm

hosea46
Member
Registered: 2015-10-14
Posts: 3

Re: Copy a file's NSURL to clipboard

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

Offline

 

#4 2015-10-14 12:05:25 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Copy a file's NSURL to clipboard

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

Applescript:

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


regards

Stefan

Offline

 

#5 2015-10-14 12:11:44 pm

hosea46
Member
Registered: 2015-10-14
Posts: 3

Re: Copy a file's NSURL to clipboard

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)