AppleScript -- Getting References to Files and/or Folders onto the Clipboard for Pasting

Hey Folks,

Here’s how to put a reference to one or more files and/or folders onto the clipboard.

These files and/or folders may then be pasted anywhere the macOS will allow, such as in the Finder, Apple Mail, etc.

-Chris

----------------------------------------------------------------
# Auth: Christopher Stone { Heavy Lifting by Shane Stanley }
# dCre: 2018/04/10 16:57
# dMod: 2018/04/19 14:42
# Appl: AppleScriptObjC
# Task: Place a reference to one or more files and/or folders on the clipboard for pasting into an app like Mail 04.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Place, @Reference, @Multiple, @Items, @Clipboard, @Pasting
# Vers: 1.02
----------------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
----------------------------------------------------------------

--» Provide 1 or more path strings here.
--  Takes Full POSIX Path(s) or Tilde-Form POSIX Path(s) or HFS path(s).

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

set filePathList to "
~/test_directory/KM_TEST/test_file_001.txt
/Users/myHomeDir/test_directory/KM_TEST/test_file_002.txt
Mercury:Users:myHomeDir:Documents:BBEdit Documents:
"

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

# Extract the path strings and exclude any whitespace.
set filePathList to its reMatch:"(?-s)^\\S.+" inText:filePathList

repeat with thePath in filePathList
   if thePath starts with "~" then
      set contents of thePath to (current application's |NSURL|'s fileURLWithPath:(its expandTildeInPath:thePath))
      set fileExists to (thePath's checkResourceIsReachableAndReturnError:(missing value)) as boolean
      if fileExists = false then error linefeed & linefeed & "Error -- One or more paths in “filePathList” are invalid!"
   else if thePath starts with "/" then
      set contents of thePath to (current application's |NSURL|'s fileURLWithPath:thePath)
      set fileExists to (thePath's checkResourceIsReachableAndReturnError:(missing value)) as boolean
      if fileExists = false then error linefeed & linefeed & "Error -- One or more paths in “filePathList” are invalid!"
   else
      set contents of thePath to (current application's |NSURL|'s fileURLWithPath:(POSIX path of thePath))
      set fileExists to (thePath's checkResourceIsReachableAndReturnError:(missing value)) as boolean
      if fileExists = false then error linefeed & linefeed & "Error -- One or more paths in “filePathList” are invalid!"
   end if
end repeat

set thePasteboard to current application's NSPasteboard's generalPasteboard()
thePasteboard's clearContents()
thePasteboard's writeObjects:filePathList

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on expandTildeInPath:tildeBasedPath
   set expandedPath to (current application's NSString's stringWithString:tildeBasedPath)'s stringByExpandingTildeInPath
   set expandedPath to expandedPath as text
   return expandedPath
end expandTildeInPath:
----------------------------------------------------------------
on reMatch:findPattern inText:theText
   set theNSString to current application's NSString's stringWithString:theText
   set theOptions to ((current application's NSRegularExpressionDotMatchesLineSeparators) as integer) + ((current application's NSRegularExpressionAnchorsMatchLines) as integer)
   set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:findPattern options:theOptions |error|:(missing value)
   set theFinds to theRegEx's matchesInString:theNSString options:0 range:{location:0, |length|:theNSString's |length|()}
   set theFinds to theFinds as list -- so we can loop through
   set theResult to {} -- we will add to this
   
   repeat with i from 1 to count of items of theFinds
      set theRange to (item i of theFinds)'s range()
      set end of theResult to (theNSString's substringWithRange:theRange) as string
   end repeat
   
   return theResult
   
end reMatch:inText:
----------------------------------------------------------------

EDIT: 2018/04/19 14:45 CDT – script updated to v1.02

1 Like

Script above updated to v1.01.

Hey Folks,

I've put the above script into a Keyboard Maestro macro, so non-scripters can make easier use of it.

In the filePathList variable are examples of the three acceptable path formats -- tilde-based POSIX Path, normal POSIX Path, and HFS path.

These items will be placed on the clipboard exactly as if you copied them in the Finder.

-Chris


Put File References on the Clipboard for Pasting v1.00.kmmacros (12 KB)

Script above updated to v1.02.