What is Best Method to Detect (Check for) a File on the Clipboard?

asobjc

(Jim Underwood) #1

I have a script that needs to branch on whether or not a file (or folder) is on the Clipboard.
###What is Best Method to Detect (Check for) a File on the Clipboard?

Here’s the simple script I have. Seems to work fine, and runs fast.
But I’d like to know if there is a better or faster method.

Any ideas?

set clipRec to the clipboard as record

try
  set fileObj to «class furl» of clipRec
  set isFileOnCB to true
  
on error
  set isFileOnCB to false
end try

return isFileOnCB

Thanks for your help and suggestions.


(Christopher Stone) #2

Hey Jim,

That will only discover 1 file/folder on the clipboard even if there are more than one.

It’s a nasty little limitation, although for some tasks it may be enough information.

To get more you have to use AppleScriptObjC.

------------------------------------------------------------------------------
use framework "Foundation"
use framework "AppKit"
use scripting additions
------------------------------------------------------------------------------

set theClipboard to current application's NSPasteboard's generalPasteboard()
set theURLs to theClipboard's readObjectsForClasses:{current application's class "NSURL"} options:(missing value)
set theCount to theURLs's |count|()

------------------------------------------------------------------------------

-Chris


(Jim Underwood) #3

Thanks, Chris. Perfiect! :+1:
That’s simple enough. :wink:
And it is so fast that SD6 doesn’t even register a time:

I can see other uses for this snippet as well.