Zipping a mail attachment

This is a script for iCal, however, when sent to a Windows Outlook user the attachment doesn’t reach them unless it is zipped or placed into a folder. I simply want to know how to place the attachment created in this script into a folder or zip it before it is sent. Whichever is an easier addition to the applescript in this script. Thanks in advance.

-- Mail.applescript
-- iCal

on show_mail_sbrs(subjectLine, messageText, myrecipients)
	tell application "Mail"
		set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
		repeat with i from (count of myrecipients) to 1 by -1
			tell mymail to make new to recipient at beginning of to recipients with properties {name:(item i of myrecipients)}
		end repeat
		set visible of mymail to true
		activate
	end tell
end show_mail_sbrs

on show_mail_sbr(subjectLine, messageText, myrecipient)
	tell application "Mail"
		set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
		tell mymail to make new to recipient at beginning of to recipients with properties {name:myrecipient}
		set visible of mymail to true
		activate
	end tell
end show_mail_sbr

on send_mail_sb(subjectLine, messageText)
	tell application "Mail"
		set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
		set visible of mymail to true
		activate
	end tell
end send_mail_sb

on send_mail_sbr(subjectLine, messageText, myrecipient)
	tell application "Mail"
		set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
		tell mymail to make new to recipient at beginning of to recipients with properties {name:myrecipient}
		send mymail
	end tell
end send_mail_sbr

on send_mail_sbrp(subjectLine, messageText, myrecipient, invitationPath)
	set pfile to POSIX file invitationPath
	set myfile to pfile as alias
	
	try
		-- define a carriage return
		set cr to (ASCII character 13) & (ASCII character 10)
		
		-- retrieve the user's name and e-mail
		set listOfAccounts to {}
		tell application "Mail"
			repeat with oneAccount in every account
				set listOfAccounts to listOfAccounts & ¬
					{""" & (get full name in oneAccount) & "" <" & ¬
						(get email addresses in oneAccount) & ">"}
			end repeat
		end tell
		
		if ((get length of listOfAccounts) is 1) then
			set theAccountTouse to get first item of listOfAccounts
		else
			set theAccountTouse to ¬
				choose from list listOfAccounts ¬
					default items (get first item of listOfAccounts) ¬
					with prompt ¬
					¬
						"Please select which mail account to send the invitation from:" without multiple selections allowed and empty selection allowed
		end if
		
		-- open and read the ical event file to insert into an e-mail
		set myEventFileHandle to ¬
			open for access myfile without write permission
		set myEventFileContent to read myEventFileHandle
		close myEventFileHandle
		
		-- remove the timezone info from the calendar event
		-- to make compatable with outlook
		set toss to false
		set newEventFileContent to ""
		set last_line to false
		repeat with theLine in paragraphs of myEventFileContent
			if theLine contains "BEGIN:VTIMEZONE" then
				set toss to true
			else if theLine contains "END:VTIMEZONE" then
				set last_line to true
				set toss to false
			end if
			if toss is false then
				if last_line is true then
					set last_line to false
				else
					set newEventFileContent to newEventFileContent & theLine & cr
				end if
			end if
		end repeat
		set myEventFileContent to newEventFileContent
		
		-- pre-pend mail headers to the event contents
		set myNewEmailText to ¬
			"From: " & theAccountTouse & cr & ¬
			"To: " & myrecipient & cr & ¬
			"Subject: " & subjectLine & cr & ¬
			"MIME-Version: 1.0" & cr & ¬
			"Content-Type: text/calendar;  method=REQUEST;" & cr & ¬
			"       charset="utf-8"" & cr & ¬
			"Content-Transfer-Encoding: 8bit" & cr & ¬
			"Importance: Normal" & cr & cr & ¬
			myEventFileContent
		--                      "  name="meeting.ics"" & cr & ¬
		
		-- create a random event file name
		set tempMailName to (random number from 1 to 1000000) & ".ics"
		set aliasTempMail to "/tmp/" & tempMailName
		
		-- write the new e-mail to a temp file
		set myEventFileHandle to ¬
			open for access (POSIX file aliasTempMail as string) with write permission
		write myNewEmailText starting at 1 to myEventFileHandle
		close myEventFileHandle
		
		-- use SENDMAIL to send the file with proper headers
		do shell script "sendmail < " & aliasTempMail
		
		-- delete the temp file
		do shell script "rm " & aliasTempMail
	on error errMsg
		display dialog errMsg
	end try
end send_mail_sbrp

on send_mail_sbp(subjectLine, messageText, invitationPath)
	set pfile to POSIX file invitationPath
	set myfile to pfile as alias
	tell application "Mail"
		set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
		tell mymail
			tell content
				make new attachment with properties {file name:myfile} at after the last word of the the last paragraph
			end tell
		end tell
		set visible of mymail to true
		activate
	end tell
end send_mail_sbp

So can I assume that since noone replied that it can’t be done? I don’t mean that in a sarcastic way, it just sounds like that when typed. :slight_smile: I’m trying to figure it out for a friend and with a new baby, I don’t have time for much.

Hi twitch,

I thought there might be a unix way to zip and forgot about this. If there is no unix way, then you might be able to use DropZip which is a included in the Stuffit Standard download. I’m not sure yet, but there might be a nag screen to buy. If so, there might be other ways like using DropStuff instead or disk images. I’m not sure if pcs can open these. I’ll test a DropStuff script and see if there is the nag screen.

gl,

Hi,

Yes there is a unix way I think using gzip, but I don’t know how to use it. Maybe the unix people were off on vacation.

gl,

There is a way. I’m not a Unix guy (I’m a FileMaker guy), and I did not originate this code. I may have hacked it mildly. But it works:
this saves the zip without file extension (name.zip)
tell application “Finder”
set macPath to choose file
set sourcePath to quoted form of POSIX path of macPath
set theFolder to container of macPath as string
set fileName to name of macPath
set fileExt to name extension of macPath
if fileExt is not “” then
set extCount to (count of fileExt) + 1
set savePath to quoted form of POSIX path of (theFolder & text items -extCount thru 1 of fileName & “zip”)
else
set savePath to quoted form of POSIX path of (theFolder & fileName & “.zip”)
or use ((macPath as string) & “.zip”)
end if
do shell script ("ditto -c -k -rsrc " & sourcePath & " " & savePath)
delete macPath
end tell

Thanks for the replies. When I get some time (and sleep…ha ha) I’ll check them out in more detail.
I’m familiar with UNIX, and it’s really easy to zip something from the terminal, but how would I include it in that mail script.
Zippping a file in UNIX is quite easy. If I wanted to zip a text edit file named “Test.rtf” on my desktop I’d simply type.

zip Test ~/desktop/Test.rtf

This would create a zip file of the Test.rtf document. I’m just trying to find out what and where to add code into the mail script above so that the created attachment will be zipped or simply put into a folder as it is created.
Once again, thanks so much for the replies. Hopefully I’ll see more clearly when the baby is quiet…ha ha

What does the Test mean in:

zip Test ~/ …

?

The first “Test” after “zip” is what the .zip file will be named. So if you wanted the .zip file to be named “Hey” you would type “Hey” (minus the quotes) afer zip. :slight_smile:

I searched through the script and the only place where it makes attachments is in this subroutine:

on send_mail_sbp(subjectLine, messageText, invitationPath)
set pfile to POSIX file invitationPath
set myfile to pfile as alias
tell application “Mail”
set mymail to (make new outgoing message at the beginning of outgoing messages with properties {subject:subjectLine, content:messageText})
tell mymail
tell content
make new attachment with properties {file name:myfile} at after the last word of the the last paragraph
end tell
end tell
set visible of mymail to true
activate
end tell
end send_mail_sbp

The line:

set pfile to POSIX file invitationPath

converts the posix style path to AppleScript file apecification. So you can use the invitationPath in your unix command. To use the unix from AppleScript, use ‘do shell script’. The only problem is how do you make the zip command place the zipped file in a certain directory (maybe the same directory as the original file). How would you do this in your example:

do shell script “zip Test ~/Desktop/Test.txt”

How to place Test.zip on the desktop?

From there it’s easy.

Later,

Hi,

I think I figured out how to place the zip file. Try placing this:

set path1 to quoted form of invitationPath
tell application “Finder” to set c to (container of myfile) as alias
set path2 to quoted form of ((POSIX path of c) & “MyZip”)
set zip_c to (“zip " & path2 & space & path1)
do shell script zip_c
set myfile to (”" & c & “MyZip.zip”) as alias

after the statement:

set myfile to pfile as alias

in the subroutine that I posted previously. I used the quoted form because the path to the container might have spaces.

gl,