How to get the a file URL in OSX with AppleScript or a shell script? How to get the a file URL in OSX with AppleScript or a shell script? shell shell

How to get the a file URL in OSX with AppleScript or a shell script?


Here is a simple AppleScript that will loop through the Finder selection and put the file URLs on the clipboard, in a return-delimited string. It uses mklement's "2-line" code and is useable with Keyboard Maestro:

set theOutput to ""-- Obtain Finder selection and store it in variable "sel".tell application "Finder" to set sel to get selection as alias listrepeat with x in sel-- Convert to alias, then determine its file URL and store that in variable "myFileUrl"tell application "System Events" to set myFileUrl to URL of xif theOutput = "" then    set theOutput to myFileUrlelse    set theOutput to theOutput & return & myFileUrlend ifend repeatset the clipboard to theOutput


this is lifted almost verbatim from this answer i.e. leverage python's urllib to quote the string appropriately before adding in the file://localhost to the start of the string

on path2url(thepath)    return do shell script "python -c \"import urllib, sys; print (urllib.quote(sys.argv[1]))\" " & quoted form of thepathend path2urltell application "Finder"    set sel to the selection as text    set the clipboard to "file://localhost" & my path2url(POSIX path of sel)end tell

I've added parentheses around the print in order to make the python script compatible between python 2.x and python 3.


There is a more robust and convenient solution (tested on 10.7.4 - don't know when it became available):

-- Obtain Finder selection and store it in variable "sel".set sel to selection of application "Finder"-- Convert to alias, then determine its file URL and store that in variable "myFileUrl"tell application "System Events" to set myFileUrl to URL of (sel as alias)

Note:

  • Example only works if the Finder selection comprises exactly 1 item.
  • The tell application "System Events" part is essential, as only the System Events dictionary contains the type of alias class that has a URL property
  • For brevity, the two statements can be combined into one.

Now, let's say you want to create an OS X service that copies the file URLs of the file(s) and/or folder(s) currently selected in Finder to the clipboard:

  • Run Automator, choose File > New, and opt to create a new Service.
  • Under 'Service receives selected', choose 'files or folders', and for 'in', choose 'Finder.app'
  • Add a 'Run AppleScript' action.
  • Paste the following AppleScript code:
-- Receives the select files/folders from the Finder and copies their file URLs to the clipboard.-- If the selection comprises more than 1 item, the URLs copied are separated by LF characters.on run {input, parameters}    set allUrls to ""    tell application "System Events"        repeat with f in input            -- Convert input file/folder to a "System Events" alias...            set a to f as alias            -- and determine the value of the "URL" property, which is the file URL.            set thisUrl to URL of a            -- Add the file URL to the overall result.            if length of allUrls is 0 then                set allUrls to thisUrl            else                set allUrls to allUrls & linefeed & thisUrl            end if        end repeat    end tell    -- Finally, copy the file URL(s) to the clipboard.    set the clipboard to allUrlsend run
  • Save the new service with a descriptive name; e.g., "Copy File URLs to Clipboard"

This will make the new service show up in the context menu when you Control-click items in Finder (depending on the number of defined services, either at the top level of the context menu or in a 'Services' submenu).

If you want to assign a keyboard shortcut to the new service, open System Preferences, go to Keyboard > Keyboard Shortcuts > Services, and locate your newly created service there. Click near the right edge of the entry and press the desired key combination.