Thursday, June 4, 2009

 

Mail a link to a file on OS X


I had an odd query from a client a couple of days ago - he couldn't find a way to email a link to a file on his server in Leopard client, so I shouldered him aside (as is my wont), confident that there was some easy, simple solution in Mail to enable this quite basic solution.

Well, there was and then again, there wasn't. Mail will allow you to send a web link with no problem, and will also allow you to send links to files on a mounted share, but only if you're happy to actually type in the link by hand, which can be... cumbersome.

The answer was to throw together this quick n'dirty little script, turn on the Script Menu in the AppleScript Utility application, then pop the finished product into ~/Library/Scripts. Thus:

-- Activate the Finder and choose a file to link to.

tell application "Finder"
get selection
set PathToFile to (choose file with prompt "Choose file to link") as string

-- Use text item delimiters to strip out ":" and the word "Contents" from the beginning of the string reported above.

set AppleScript's text item delimiters to "Contents"
set AppleScript's text item delimiters to ":"

-- Set the newly-pruned text to a new variable "first_path"

set first_path to every text item in PathToFile

-- Use text item delimiters to put "/" in as the default separator between text items.

set AppleScript's text item delimiters to "/"

-- Take the resulting string and set it to a new variable "second_path" that can be passed down the line.

set second_path to every item in first_path as string

-- Put the address of your server as a prefix to "second_path", then set the result to a new variable "third_path"

set third_path to "afp://domainnamegoeshere.com/" & second_path as string

Use text item delimiters to knock out spaces and replace them with "%20", then save the result as "final_path"

set AppleScript's text item delimiters to " "
set fourth_path to every text item in third_path
set AppleScript's text item delimiters to "%20"
set final_path to every item in fourth_path as string


end tell

-- tell Mail to create a new message, inserting "final_path" as a link.

tell application "Mail"
activate
make new outgoing message with properties {visible:true, content:final_path}
end tell



There are a few things wrong with that script - it's a trifle too cumbersome, you have to hard-code your server's name (although it wouldn't be too hard to add a line or two to allow you to enter or choose that), and it strips out the word "Contents" - necessary to make the script play nice with the result it returns from getting your share, but kind of lousy if you have a folder called "Contents" that you want to link from.

All that aside, its a solution to an annoying problem, and shows how flexible and powerful AppleScript can be...

Comments: Post a Comment




<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]