AppleScript to save Emails fails if ":" in Subject Line

I’ve got a script a colleague wrote some time ago as part of a project creation workflow that allows me to save emails and their attachments when setting up new projects for work.

There is one hitch, however: the script fails if there is a colon in the subject line (such as “RE:” or “WG:” as it is in German).

I have tried everything I know to correct this, but have never been able to. I even went over to using an AppleScript to change the subject line, but since upgrading to Lion, this now also fails.

Here is my original script for saving mails:

set mail_data to button returned of (display dialog "Save job data from selected mail  ?" buttons {"Yes", "No"} default button {"No"})
if mail_data = "No" then
	display alert "Job created"
else
	tell application "Mail"
		set job to item 1 of (get selection)
		set job_info to (get source of job)
		set job_name to ((get subject of job as rich text) & ".eml")
		set job_path to open for access file ((project_folders as rich text) & "mail:" & job_name) with write permission
		write job_info to job_path
		close access job_path
		repeat with original in every mail attachment in job
			set original_name to (get name of original)
			tell original
				set original_path to POSIX path of ((project_folders as rich text) & "originals:" & original_name)
				save original in (POSIX file original_path)
			end tell
		end repeat
	end tell
end if

The error I get quotes the POSIX path to where I’ve chosen to save the mail/project, which of course is a colon-delimited path. I believe this must be what’s making the script fail because it seems the colon in “RE:” as being part of the path, which it isn’t

Is there any way to correct this and get this working again?

Many thanks,

bowjest

Hi,

of course the script fails because a colon is a path separator.
I recommend to replace every colon with an underscore character


set mail_data to button returned of (display dialog "Save job data from selected mail  ?" buttons {"Yes", "No"} default button {"No"})
if mail_data = "No" then
	display alert "Job created"
else
	tell application "Mail"
		set job to item 1 of (get selection)
		set job_info to (get source of job)
		set job_name to ((get subject of job) & ".eml")
		set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
		set job_name to text items of job_name
		set AppleScript's text item delimiters to "_"
		set job_name to job_name as text
		set AppleScript's text item delimiters to ASTID
		set job_path to open for access file ((project_folders as text) & "mail:" & job_name) with write permission
		write job_info to job_path
		close access job_path
		repeat with original in every mail attachment in job
			set original_name to (get name of original)
			tell original
				set original_path to POSIX path of ((project_folders as text) & "originals:" & original_name)
				save original in (POSIX file original_path)
			end tell
		end repeat
	end tell
end if


Notes:
rich text is deprecated since Leopard
consider that the save attachment command is broken in Lion

Hi Stefan,

That worked perfectly. Outstanding. I really appreciate that. What a load of headaches you have spared me.

Thanks also for the additional notes on this.

All the best,

bowjest