Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Another take on exporting and importing Mail signatures' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Another take on exporting and importing Mail signatures
Authored by: mark hunte on Feb 12, '09 02:03:25AM
Dave Hughes Pointed out that the random file name generator in the Import
script did not have an if exists check.
The New script below now includes this.
Thanks Dave.

MH

(* // Import_Mail_Signatures  Script//
Created by mark hunte on 19/04/2008.
//  Copyright 2008 . All rights reserved. ( well you know what I mean)
In response to Macosxhints  question in http://forums.macosxhints.com/showthread.php?p=464704-

*****  WARNINGS  >
Although I have not had any issues with this script,  It goes without saying...

** YOU use this script at you own risk, and should back up any files you feel is necessary in case of some unforseen problem **

If you Pass this script on Modified or whole, Please include all comments and warnings.

****<

This script will allow you to choose a folder of rtf files, and them into New webarchive files , Place them into 
the mail.app signature folder ("/Users/Username/library/Mail/Signatures/").

And write the details of the new files to the SignaturesByAccount.plist
also in the Signatures folder. 

 The script use the command line tool Textutil to do the conversion from RTF to webarchive. 
Textutil can convert to and from other formats  you may want.
-- formats --( txt, html, rtf, rtfd, doc, docx, wordml, odt, or webarchive )
--
I would suggest you quit Mail.app  first before running.
But if not, Once run, you can quit Mail.app and reopen it, for mail.app to read the plist file again.


I have only set the Script to  put the new Signatures into the ALL Signatures preference (in Mail.app's Signature preferences.)
All you have to then do is drag any of the signatures on to an Account name as normal within the Signature preferences.

And remove them if you wish, using the options to delete in the same Signature preferences
--


*)
global uniqPath, uniq
tell application "Finder"
	(* set format of source files *)
	set format_ to "rtf"
	(* choose Source for rtf files*)
	set sourcePath to (choose folder with prompt "Please select the source directory")
	(* only allow rtf files*)
	set sig_webAs to (items of folder sourcePath whose name extension is format_) as alias list
	(* get users Mail signature folder*)
	tell application "System Events" to set sigPath to POSIX path of (path to home folder of user domain) & "/library/Mail/Signatures/" as Unicode text
end tell

(* repeat for each rtf file found*)
repeat with i from 1 to number of items in sig_webAs
	(* get name of rtf file minus  extension*)
	tell application "Finder" to set SignatureName to text from word 1 to word -2 of (displayed name of (item i of sig_webAs) as string)
	(* get the posix path (unix) of the file *)
	set this_item to POSIX path of item i of sig_webAs
	(* create a unique name for the  web archive *)
	
	my Rand(sigPath)
	--set uniq to (randomString(8) & "-" & randomString(4) & "-" & randomString(4) & "-" & randomString(4) & "-" & randomString(12))
	--set uniqPath to (sigPath & uniq & ".webarchive")
	
	(* convert the rtf file to a  web archive, saving it to the Mail signature folder and naming it with the unique name  *)
	set sig_xml to do shell script "Textutil   -convert webarchive " & quoted form of this_item & " -output " & quoted form of uniqPath
	log sig_xml
	(* go to handler,  writePlist(uniq, SignatureName, sigPath) *)
	my writePlist(uniq, SignatureName, sigPath)
end repeat
on Rand(sigPath)
	set checkResult to true
	repeat until checkResult is false
		set uniq to (my randomString(8) & "-" & my randomString(4) & "-" & my randomString(4) & "-" & my randomString(4) & "-" & my randomString(12))
		set uniqPath to (sigPath & uniq & ".webarchive")
		tell application "Finder"
			if not (exists file (uniq & ".webarchive" as string) of (POSIX file sigPath as alias)) then
				set checkResult to false
			else
				set checkResult to true
				
				log uniq
			end if
		end tell
	end repeat
end Rand
(* handler to write to the  SignaturesByAccount.plist, adding the new details of the new file to the "AllSignaturesKey" array *)
on writePlist(uniq, SignatureName, sigPath)
	tell application "System Events"
		
		set SignatureIsRichSTATE to "true"
		
		
		set this_plistfile to (sigPath & "SignaturesByAccount")
		(* escape any spaces in name of file *)
		set SignatureName to (do shell script "echo " & quoted form of SignatureName & "|sed 's/\\ /\\\\ /g'")
		(* write to the  SignaturesByAccount.plist *)
		do shell script "defaults  write " & this_plistfile & " \"AllSignaturesKey\" -array-add '<dict><key>SignatureIsRich</key><" & SignatureIsRichSTATE & "/><key>SignatureName</key><string> '" & SignatureName & "'</string><key>SignatureUniqueId</key><string>" & uniq & "</string></dict>'"
		
	end tell
end writePlist

(* handler to  create a unique name for the new signature web archive file *)
on randomString(aLength)
	set aLength to aLength / 2 as integer
	log aLength
	set randNumber to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
	set randomChars to {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
	set aString to ""
	
	repeat aLength times
		set aString to aString & some item of randNumber & some item of randomChars
	end repeat
	
	return aString
end randomString

---
mh

[ Reply to This | # ]