Email File location links in Microsoft Outlook

Hello,

This is my first time posting here. I just wanted to say what a valuable resource this site has been for me. I’m not a programmer, and have only a basic knowledge of QBASIC and Turing from when I was in school. Using this site I was able to cobble together a script that does the following:

  • Right click on any file/folder in Finder
  • Script then grabs the file path info
  • Generates an email in Microsoft Outlook that has 2 clickable links (one for MAC and one for PC) that link directly to the file/folder.

The issue that I’m running into is that sometimes the file/folder name may have an apostrophe in it. When this happens, the clickable PC link (HTML i think?) breaks once it runs into the apostrophe. My MAC link doesn’t have an issue with the apostrophe.

Here is an example of the email that it generates:

to: youremail@email.com
subject: ASSETS -

Hello,

We have received the following:

NOTES:

MAC:
smb://servername/hdd/folder/Let’s%20Eat/

If the link is broken, copy (apple+c) and paste (apple+v) whole link into Connect to Server (apple + k)

PC:
'\servername\hdd\folder\Let’s Eat'

If the link is broken, copy (ctrl+c) and paste (ctrl+v) whole link into Windows Explorer

MAC link works fine. For some reason, the PC hyperlink get’s broken at the apostrophe. I’ve tested this with other special characters like & and it works fine. I can’t do a search and replace because the folder name still has the apostrophe. I’ve read a little bit about “escaping” the possible apostrophe, but I don’t know how I would do it (or if that’s the right solution) because the apostrophe is stored in a variable. Also, as mentioned, not every folder has an apostrophe in it’s name.

Here is the full script for reference:


on run
tell application “Finder” to set sel to selection
open sel
end run

on SAR(main_text, search_text, replace_text)
– Search and Replace. Search for search-text in
– main_text and replace it by replace_text.
set old_delims to AppleScript’s text item delimiters
try
set AppleScript’s text item delimiters to search_text
considering case
set parts to every text item of main_text
end considering
set AppleScript’s text item delimiters to replace_text
set newText to (parts as string)
end try
set AppleScript’s text item delimiters to old_delims
return newText
end SAR

on open theseItems

## This Section Creates the Mac file path

set macFilePath to {}
repeat with oneFile in theseItems
	set text item delimiters to ":"
	set filePath to text items of (oneFile as text)
	set text item delimiters to "/"
	set end of macFilePath to filePath as text
end repeat
set macFilePath to macFilePath as text
set macFilePath1 to SAR(macFilePath, " ", "%20")

## This Section Creates the windows file path

set winFilePath to {}
repeat with oneFile in theseItems
	set text item delimiters to ":"
	set filePath to text items of (oneFile as text)
	set text item delimiters to "\\"
	set end of winFilePath to filePath as text
end repeat
set winFilePath to winFilePath as text
set winFilePath1 to SAR(winFilePath, " ", "%20")

## This Section Creates the email in Outlook

set the_Subject to "(ASSET) - "
set MacPath to "smb://servername/" & macFilePath1
set WindowsPath to "\\\\servername\\" & winFilePath
set WindowsLink to "<a href=" & WindowsPath & ">" & WindowsPath & "</a>"
set the_Content to "Hello, <br> <br>We have received the following: <BR><BR><BR><BR><BR><BR> NOTES:<BR><BR><BR><BR><b>MAC:</b> <BR>" & "smb://cptorpostsh002/" & macFilePath1 & "<BR><BR> If the link is broken, copy (apple+c) and paste (apple+v) whole link into Connect to Server (apple + k) <BR><BR><b>PC:</b> <BR>" & WindowsLink & "<BR><BR>If the link is broken, copy (ctrl+c) and paste (ctrl+v) whole link into Windows Explorer<BR><BR>"

tell application "Microsoft Outlook"
	activate application "Microsoft Outlook"
	set newMessage to make new outgoing message with properties {subject:the_Subject, content:the_Content}
	make new recipient at newMessage with properties {email address:{address:"yourname@email.com"}}
	open newMessage
end tell

end open

Any help is appreciated.

Hi. Welcome to MacScripter. Sorry to see you haven’t had a reply yet.

A first guess at a cure for the PC link would be to try enclosing its URL in double quotes in the “<a href=” tag, ie. change this line:

set WindowsLink to "<a href=" & WindowsPath & ">" & WindowsPath & "</a>"

… to this:

set WindowsLink to "<a href=\"" & WindowsPath & "\">" & WindowsPath & "</a>"

There are a few other things to say about the script, but I don’t have time to go into them at the moment. The first thing is to try and cure the immediate problem. :slight_smile:

MacScripter’s fora have [applescript] and [/applescript] tags which make AppleScript code appear as above when posted, with a clickable link which opens it in people’s default editors. If you could enclose any AS code in these when posting, it would be great. There’s a button for them just above the text field in the posting windows.

Hi Nigel,

That did it. Thank you very much for your help.

Here is the revised full script now:

on run
tell application “Finder” to set sel to selection
open sel
end run

on SAR(main_text, search_text, replace_text)
– Search and Replace. Search for search-text in
– main_text and replace it by replace_text.
set old_delims to AppleScript’s text item delimiters
try
set AppleScript’s text item delimiters to search_text
considering case
set parts to every text item of main_text
end considering
set AppleScript’s text item delimiters to replace_text
set newText to (parts as string)
end try
set AppleScript’s text item delimiters to old_delims
return newText
end SAR

on open theseItems

## This Section Creates the Mac file path

set macFilePath to {}
repeat with oneFile in theseItems
	set text item delimiters to ":"
	set filePath to text items of (oneFile as text)
	set text item delimiters to "/"
	set end of macFilePath to filePath as text
end repeat
set macFilePath to macFilePath as text
set macFilePath1 to SAR(macFilePath, " ", "%20")

## This Section Creates the windows file path

set winFilePath to {}
repeat with oneFile in theseItems
	set text item delimiters to ":"
	set filePath to text items of (oneFile as text)
	set text item delimiters to "\\"
	set end of winFilePath to filePath as text
end repeat
set winFilePath to winFilePath as text
set winFilePath1 to SAR(winFilePath, " ", "%20")

## This Section Creates the email in Outlook

set the_Subject to "(ASSET) - "
set MacPath to "smb://servername/" & macFilePath1
set WindowsPath to "\\\\servername\\" & winFilePath
set WindowsLink to "<a href=\"" & WindowsPath & "\">" & WindowsPath & "</a>"
set the_Content to "Hello, <br> <br>We have received the following: <BR><BR><BR><BR><BR><BR> NOTES:<BR><BR><BR><BR><b>MAC:</b> <BR>" & "smb://cptorpostsh002/" & macFilePath1 & "<BR><BR> If the link is broken, copy (apple+c) and paste (apple+v) whole link into Connect to Server (apple + k) <BR><BR><b>PC:</b> <BR>" & WindowsLink & "<BR><BR>If the link is broken, copy (ctrl+c) and paste (ctrl+v) whole link into Windows Explorer<BR><BR>"

tell application "Microsoft Outlook"
	activate application "Microsoft Outlook"
	set newMessage to make new outgoing message with properties {subject:the_Subject, content:the_Content}
	make new recipient at newMessage with properties {email address:{address:"youremail@email.com"}}
	open newMessage
end tell

end open