Create local finder links for mail.app
July 7, 2008 6:35 PM   Subscribe

A few of us work off a local OS X Fileserver. We often need to point each other to files deep with it by email. I am looking for an automated way to create clickable local links in the mail.app friendly format of "file:///Volumes/Server%20Portal/". Any solutions?

I have found applescripts that nearly do it. But they only make POSIX links. For Mail to recognise and launch links they need both file:/// appended and spaces replaced with %20. I am guessing it could be achieved by a few additions to current applescripts, but unfortunately it is above me.
posted by Raff to Computers & Internet (8 answers total)
 
When I was creating links in safari to my file server (Win2k3), I used this format link-

smb://fileserver/blah/blah

Not sure if will help in your case.
posted by wongcorgi at 2:09 AM on July 8, 2008


I wrote something to do precisely this for exactly the same reason a while back. I'll dig it up when I get home.
posted by bonaldi at 9:51 AM on July 8, 2008


You could use the URI Escape scripting addition to fix the paths. You have to install it by dragging it into your Library/Scripting Additions folder.

Here's an example of how to use it for a simple conversion once it's installed:


set myFile to "hello dolly"
set myURI to "file://" & URI Escape myFile


Here's more on scripting additions, if you've never messed with them. The nutshell is that scripting additions add functionality to AppleScript, with the drawback being that if you use a scripting addition to write a script, others who want to use the script elsewhere will also need to install the same scripting addition on their own systems/in their own accounts.
posted by mph at 2:54 PM on July 8, 2008


Best answer: Looking at the link to script snippet you want to work from, here's how you could use the URI Escape scripting addition in that script:
on open {dropped_item}
	tell application "Finder"
		set the clipboard to "file://" & (URI Escape (POSIX path of the dropped_item))
	end tell
end open
Saved as an application bundle, that gives you a way to drag-n-drop a file to get a Mail-friendly URI. You can drag the saved script into your Dock to keep it handy.
posted by mph at 4:30 PM on July 8, 2008


Response by poster: Thanks mph! Worked a treat!

I found a snippet on the page i linked that replaces text strings. In an overenthusiastic laziness I had an attempt to make an integrated script.

It doesn't work and why is beyond me. If its a quick fix i would love if someone could point me in the right direction. I made bold where i think i went wrong.

on open {dropped_item}



set myString to (POSIX path of (the selection as alias))
set search_string to " "
set replacement_string to "%20"

my findReplace(myString, search_string, replacement_string)

tell application "Finder" to set the clipboard to "file:///" & theString

end open

(*============ SUBROUTINE ==============*)
on findReplace(theString, search_string, replacement_string)

if theString contains search_string then
set AppleScript's text item delimiters to search_string
set the text_item_list to text items of theString
set AppleScript's text item delimiters to replacement_string
set the theString to every item in text_item_list as Unicode text
set AppleScript's text item delimiters to ""
end if

return theString
end findReplace

posted by Raff at 1:19 AM on July 9, 2008


Best answer: It's going wrong because you're mixing terms. The script starts as a droplet:

on open {dropped_item}

but then it refers to what is supposed to be the Finder selection:

set myString to (POSIX path of (the selection as alias))

I cannibalized another one of the scripts on that page. This will work as a droplet, and it takes multiple selections into account:
on open dropped_items
  set myList to ""
  repeat with anItem in dropped_items
	set myString to (POSIX path of (anItem as alias))
	set search_string to " "
	set replacement_string to "%20"
	
	if myString contains search_string then
		set AppleScript's text item delimiters to search_string
		set the text_item_list to text items of myString
		set AppleScript's text item delimiters to replacement_string
		set the myString to every item in text_item_list as Unicode text
		set AppleScript's text item delimiters to ""
	end if
		
	set myList to myList & "file:///" & myString & return
end repeat
	
set the clipboard to myList
	
end open
If that works, cool. If you want it as an app you run when you have files selected, it needs to look like this:
set theSel to (selection of application "Finder")
set pathList to ""
set search_string to " "

repeat with anItem in theSel
	
       set myItem to (POSIX path of (anItem as alias))
	  if myItem contains search_string then
		set myItem to (do shell script "echo " & quoted form of myItem & "|sed \"s/\\ /%20/g\"")
	  end if
	
	
	set pathList to pathList & "file://" & myItem & return
end repeat

set the clipboard to pathList
That demonstrates an alternate approach to cleaning out the spaces by using sed with "do shell script."

Which way you go is a matter of personal preference, I guess. I tend to fall back to "do shell script" a lot because I'm more familiar with traditional Unix tools than I am AppleScript.

I also took a shortcut and made pathList a string instead of a list. Since we're not doing anything more with the files than figuring out their paths, a list isn't really needed and it involves some extra contortions to get newline characters into the string that ends up on the clipboard.
posted by mph at 9:05 AM on July 9, 2008


Response by poster: Ha! Both works great! ... Until i realised that there was other ascii illegal characters that stopped making it a friendly URI. Stupid me! So i have fallen back to your first more full proof solution and found a way that you can embed the scripting addition within the app.

Thanks for all the help mph!

I now have a lot of mefi karma to catchup on..
posted by Raff at 6:08 PM on July 10, 2008


Hey! I didn't know that bit about embedding OSAXen. That's really good to know.

One other bit of AppleScript that's useful in a similar way:

(path to me)

You can use it to include shell scripts or other things you'd like to call on to run the AppleScript. I used it to embed Plucker in a little AppleScript app I wrote to convert VoodooPads to Palm-browseable documents.

You can call it like:
set my_arg to "foo"
set helper_script to  posix path of (path to me as text) & "Contents/Resources/Scripts/helper.rb"
set script_output to do shell script helper_script & " --argument " & my_arg 

posted by mph at 11:09 AM on July 11, 2008


« Older Can I call an API on an ssl-encrypted page?   |   Way too shaggy Newer »
This thread is closed to new comments.