Copy URL

Summary: Copy URLs of the selected/dropped items to the clipboard.
Requires: macOS 10.10 or later
Suggested Key Binding: Script Menu, droplet, or access via LaunchBar.
Install Location: ~/Library/Scripts/Applications/Finder/
Last Modified: 2022-06-28

Description

I like to execute this script via LaunchBar. I select a folder in the Finder, press Command-Space, start typing “Copy URL,” and then press Command-D to drop the Finder selection onto the script. For example, if I have my Documents folder selected, the script will put file://localhost/Users/mjt/Documents/ on the clipboard. It will also handle escaping, e.g. file://localhost/Users/mjt/Documents/ATPM/ATPM%20Web%20Site/. One use for this script is to obtain the URL of a local Subversion repository.

Installation Instructions · Download in Compiled Format · Download in Text Format

Script

use AppleScript version "2.4"
use framework "Foundation"
use scripting additions

on run
    
tell application "Finder"
        
set theList to the selection
        
my processItems(theList)
    
end tell
end run

on open theList
    
my processItems(theList)
end open

on processItems(theList)
    
set theURLs to {}
    
repeat with theItem in theList
        
copy my processItem(theItem) to end of theURLs
    
end repeat
    
set AppleScript's text item delimiters to return
    
set theResult to theURLs as string
    
set the clipboard to theResult
    
return theResult
end processItems

on processItem(theItem)
    
tell application "Finder"
        
set theAlias to theItem as alias
        
set thePath to theAlias's POSIX path
    
end tell
    
return my urlFromPath(thePath)
end processItem

on urlFromPath(_path)
    
set _url to my (NSURL's fileURLWithPath:_path)
    
return _url's absoluteString as Unicode text
end urlFromPath