Copy URL

Summary: Copy URLs of the selected/dropped items to the clipboard.
Requires: PyObjC, Python 2.5
Suggested Key Binding: Script Menu, droplet, or access via LaunchBar.

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.

Download in Compiled Format | Download in Text Format

Script

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's quoted form
    
end tell
    
return my urlFromPath(thePath)
end processItem

on urlFromPath(thePath)
    
return do shell script "python2.5 -c 'from Foundation import NSURL; import sys; s = unicode(sys.argv[1], \"utf-8\"); print NSURL.fileURLWithPath_(s) ' " & thePath
end urlFromPath

Last Modified: 2007-08-17

Running the Script

You can use AppleScript Editor (AppleScript Utility prior to Mac OS X 10.6) to enable Mac OS X’s built-in Script menu. To run the script, just select it from the menu. FastScripts provides a similar menu that supports keyboard shortcuts and other features.