Last active
September 29, 2015 12:41
-
-
Save ansonK/a0e45c18b1b391769306 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env osascript | |
(* | |
Script to set a HTML email signature in Mail.app | |
Before doing anything the script will: | |
* Check that the os version is 10.10 | |
* There is at least 1 Mail account | |
If either of these checks fails the script will exit | |
The script will then ask the for 3 things: | |
* Full name | |
* Email address | |
* Mobile number | |
The mobile number will have the leading "0" stripped, and the UK country code added "+44" | |
It will then use Mail to write a placeholder signature file | |
Then replace the placeholder signature with the HTML email signature | |
Thanks go to http://seesolve.com/words/2013/06/installing-html-email-signatures-for-multiple-users for | |
an explanation of how this should work. | |
Authors: | |
* Oskar Pearson <oskar.pearson@unboxedconsulting.com> | |
* Anson Kelly <anson.kelly@unboxedconsulting.com> | |
Version: 0.1 | |
*) | |
(* | |
Do not change anything below here | |
*) | |
global mailSignatureFile | |
global mailSignatureFilePosix | |
global okButton | |
set supportedOsVersion to "10.10" | |
set okButton to "OK" | |
set cancelButton to "Cancel" | |
set placeholderText to "PlaceHolder - To be replaced" | |
set emailSignatureName to "AutoSignature" | |
on replaceText(find, replace, subject) | |
set prevTIDs to text item delimiters of AppleScript | |
set text item delimiters of AppleScript to find | |
set subject to text items of subject | |
set text item delimiters of AppleScript to replace | |
set subject to "" & subject | |
set text item delimiters of AppleScript to prevTIDs | |
return subject | |
end replaceText | |
on displayError(msg) | |
display dialog msg buttons {okButton} default button okButton with icon stop with title "Error" | |
end displayError | |
-------------------------------------------------------------------------------- | |
-- Check the OS version is supported | |
-------------------------------------------------------------------------------- | |
if (system version of (system info) does not start with supportedOsVersion) then | |
displayError("This code only works on OS verson " & supportedOsVersion & ".*") | |
return | |
end if | |
-------------------------------------------------------------------------------- | |
-- Check that Mail has at least 1 Account setup | |
-------------------------------------------------------------------------------- | |
set mailboxCountComplete to false | |
tell application "Mail" | |
if (count of accounts) is greater than 0 then | |
set mailboxCountComplete to true | |
end if | |
quit | |
end tell | |
if not mailboxCountComplete then | |
displayError("There are no Mail accounts setup. Please set up a Mail account first") | |
return | |
end if | |
-------------------------------------------------------------------------------- | |
-- Ask the user for their full name | |
-------------------------------------------------------------------------------- | |
repeat | |
try | |
set fullName to text returned of (display dialog "What is your full name? (eg John Smith)" default answer "") | |
if length of fullName > 0 then | |
exit repeat | |
end if | |
on error number -128 | |
return | |
end try | |
end repeat | |
-------------------------------------------------------------------------------- | |
-- Ask the user for their email address | |
-------------------------------------------------------------------------------- | |
repeat | |
try | |
set emailAddress to text returned of (display dialog "What is your email address? (eg myemail@world.com)" default answer "") | |
if length of emailAddress > 0 then | |
exit repeat | |
end if | |
on error number -128 | |
return | |
end try | |
end repeat | |
-------------------------------------------------------------------------------- | |
-- Check that there is a Mail account already setup with that email address | |
-------------------------------------------------------------------------------- | |
set accountName to "" | |
try | |
tell application "Mail" | |
repeat with eachAccount in accounts | |
if email addresses of eachAccount contains emailAddress then | |
set accountName to name of eachAccount | |
end if | |
end repeat | |
end tell | |
on error errmsg | |
log errmsg | |
return | |
end try | |
if length of accountName = 0 then | |
displayError("There are no Mail accounts with email " & emailAddress & ". Please set up a Mail account first") | |
return | |
end if | |
-------------------------------------------------------------------------------- | |
-- Ask the user for their mobile number | |
-------------------------------------------------------------------------------- | |
try | |
set mobileNumber to text returned of (display dialog "What is your mobile number? (Leave blank if you don't know)" default answer "") | |
on error number -128 | |
return | |
end try | |
if length of mobileNumber > 0 then | |
-- Strip off the leading 0 | |
if the first character of mobileNumber is "0" then | |
set mobileNumber to text 2 thru (length of mobileNumber) of mobileNumber | |
end if | |
-- Add UK prefix of +44 | |
if the first character of mobileNumber is not "+" then | |
set mobileNumber to "+44 " & mobileNumber | |
end if | |
end if | |
-------------------------------------------------------------------------------- | |
-- Declare signature template | |
-------------------------------------------------------------------------------- | |
set websiteUrl to "https://ubxd.co/" | |
set websiteDisplayName to "ubxd.co" | |
set emailSignature to " | |
<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"width: 98%; margin-top: 10px; font-family: arial, sans;\"> | |
<tbody> | |
<tr> | |
<td style=\"border-bottom: solid 1px black; font-size: 20px; line-height: 28px; font-family: Trebuchet MS, arial, sans\" valign=\"bottom\">" & fullName & "</td> | |
<td style=\"border-bottom: solid 1px black;\" align=\"right\"> | |
<table width=\"145px\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width: 145px;\" align=\"right\"> | |
<tbody> | |
<tr> | |
<td style=\"font-family: Trebuchet MS, arial, sans; font-weight: bold; font-size: 35px; color: #000000; letter-spacing: -1px; height: 27px; line-height: 27px;\" height=\"27px\" valign=\"top\" align=\"right\"> | |
Unboxed | |
</td> | |
</tr> | |
<tr> | |
<td style=\"font-family: Trebuchet MS, arial, sans; font-weight: bold; font-size: 30px; color: #92278F; letter-spacing: -1px; height: 42px; line-height: 27px;\" height=\"42px;\" valign=\"top\" align=\"right\"> | |
Consulting | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
<tr> | |
<td align=\"left\" valign=\"top\" style=\"border-top: solid 4px white;\"> | |
<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"font-size: 12px; font-family: arial;\" align=\"left\"> | |
<tbody> | |
<tr> | |
<td style=\"width: 16px; color: #92278F;\">E:</td> | |
<td> | |
<a style=\"color: black; text-dcoration: none;\" href=\"" & emailAddress & "\">" & emailAddress & "</a> | |
</td> | |
</tr> | |
<tr> | |
<td style=\"width: 16px; color: #92278F;\">M:</td> | |
<td>" & mobileNumber & "</td> | |
</tr> | |
<tr> | |
<td style=\"width: 16px; color: #92278F;\">T:</td> | |
<td>+44 20 3137 2940</td> | |
</tr> | |
<tr> | |
<td style=\"width: 16px; color: #92278F;\">F:</td> | |
<td>+44 20 7183 4251</td> | |
</tr> | |
</tbody> | |
</table> | |
</td> | |
<td align=\"right\" style=\"font-size: 12px; font-family: arial; border-top: solid 4px white;\" valign=\"top\"> | |
<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" style=\"font-size: 12px; font-family: arial;\" align=\"right\"> | |
<tbody> | |
<tr> | |
<td align=\"right\">17 Blossom Street</td> | |
</tr> | |
<tr> | |
<td align=\"right\">London, E1 6PL</td> | |
</tr> | |
<tr> | |
<td align=\"right\">United Kingdom</td> | |
</tr> | |
<tr> | |
<td align=\"right\"><a style=\"color: #92278F; text-decoration: none;\" href=\"" & websiteUrl & "\">" & websiteDisplayName & "</a></td> | |
</tr> | |
</tbody> | |
</table> | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
" | |
-------------------------------------------------------------------------------- | |
-- Delete any existing signature, and then create a new placeholder signature | |
-------------------------------------------------------------------------------- | |
try | |
tell application "Mail" | |
activate | |
end tell | |
on error | |
display dialog "Error activating Mail" buttons {okButton} default button okButton cancel button okButton with title "Exit" | |
end try | |
delay 5 | |
try | |
log "Deleting existing signature..." | |
tell application "Mail" | |
delete (every signature whose name is emailSignatureName) | |
end tell | |
log ".. deleted" | |
delay 3 | |
on error errmsg | |
displayError("Error deleting existing signature: " & errmsg) | |
return | |
end try | |
-------------------------------------------------------------------------------- | |
-- Create placeholder signature in Mail | |
-- We let Mail create the signature file so the formatting is correct for that version of Mail | |
-------------------------------------------------------------------------------- | |
try | |
log "Creating Placeholder Signature..." | |
tell application "Mail" | |
set newSig to make new signature with properties {name:emailSignatureName, content:"PlaceHolder"} | |
set content of newSig to placeholderText | |
set selected signature to emailSignatureName | |
delay 1 | |
tell application "System Events" to keystroke "," using command down | |
delay 3 | |
tell application "System Events" to keystroke "w" using command down | |
tell application "Mail" | |
quit | |
end tell | |
end tell | |
log "... placeholder created" | |
on error errmsg | |
displayError(errmsg) | |
return | |
end try | |
log "Waiting for mail to exit" | |
delay 5 | |
-------------------------------------------------------------------------------- | |
-- Find the latest mail signature file with finder, now that things have been saved | |
-------------------------------------------------------------------------------- | |
try | |
tell application "Finder" | |
-- Different versions of Mail store the signatures in different folders. Grrr. | |
set signaturePath to ((path to library folder from user domain) as text) & "Mobile Documents:com~apple~mail:Data:MailData:Signatures:" | |
if not (exists signaturePath) then | |
set signaturePath to ((path to library folder from user domain) as text) & "Mail:V2:MailData:Signatures:" | |
end if | |
if not (exists signaturePath) then | |
set signaturePath to ((path to library folder from user domain) as text) & "Mail:MailData:Signatures:" | |
end if | |
set mailSignatureFile to item 1 of reverse of (sort (every file of alias signaturePath whose name ends with ".mailsignature") by modification date) as alias | |
set mailSignatureFilePosix to POSIX path of mailSignatureFile | |
end tell | |
on error errmsg | |
displayError(errmsg) | |
return | |
end try | |
-------------------------------------------------------------------------------- | |
-- Replace the placeholder text in the signature file with our proper unboxed template | |
-------------------------------------------------------------------------------- | |
try | |
set the mailSignatureOpenFile to open for access mailSignatureFilePosix with write permission | |
set signatureText to read mailSignatureOpenFile | |
set signatureText to get replaceText(placeholderText, emailSignature, signatureText) | |
set eof mailSignatureOpenFile to 0 | |
write signatureText to mailSignatureOpenFile starting at 0 | |
close access mailSignatureOpenFile | |
on error errmsg | |
displayError(errmsg) | |
return | |
end try | |
display dialog "It worked! | |
Emails sent from Mail should now have your new signature" buttons {okButton} default button okButton with title "Exit" with icon note giving up after 30 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment