Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Applescript > Works in Script...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 1510 of 1674
Post > Topic >>

Works in Script Editor, but not as Mail Rule ... why?

by misterfriendly <Luke.Jaeger@[EMAIL PROTECTED] > Oct 16, 2007 at 07:33 AM

Here's a Mail Rule I wrote to do the following:

1=2E Check all incoming messages for attachments
2=2E For every new message with attachments, create a new folder named
for the message's subject and date. Save the attachments in the
folder.
3=2E Save the message's body text in the folder as a .txt file.
4=2E Send an outgoing email re****ting the name(s) and file size(s) of
the saved attachments.
5=2E Open the Finder window(s) of  the newly made folder(s).

It works perfectly when run from the Script Editor. But when run as a
Mail Rule, it only does steps 1 and 2. If anyone can tell me how to
fix it, I will mail you a can of soup!

It helps to set Mail's prefs to always display messages as plain text,
not HTML. This requires a change to the plist file which is in the
intro comments.

And here's the script:

(*Mail attachment save script by Luke Jaeger www.trickfilm.org

This script will mine a list of Mail messages for attachments and save
them to a destination you choose.
The script creates a subfolder for each message with attachments,
named with the subject of the message and its date sent. All
attachments are saved with their original names. Messages without
attachments are ignored.

After running, the script will send a mail message detailing what was
saved.

The script is ready to use as a rule action in Mail - just fill in the
specifics of your destination folder and recipient.

I wrote this script to deal with large art files sent to me via Mail.
Hope you find it useful!

NOTE: Saving message text as .txt file works best if you set Mail to
always display plain text.
Here's how: in Terminal, paste this text -->     defaults write
com.apple.mail PreferPlainText -bool TRUE

*)


--comment the first two lines ("using terms=E2=80=A6" and "on
perform=E2=80=
=A6") and
their accompanying "end" statements when testing. Uncomment them to
use this script as a Mail rule function.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule UploadEm
		tell application "Mail"
			--comment the following line when running this script as a Mail.app
rule function - uncomment it for testing

			--	set theMessages to selection

			set theGoodName to ""
			set theFileList to ""
			set theFolderList to {}
			set theTextFileName to ""
			set theTextFileContents to ""

			tell application "Finder"
				mount volume "afp://myservername" as user name "username" with
password "password"
				set theParentFolder to folder "Uploads" of folder "Shared" of
folder "Desktop" of disk "Home Directory"
			end tell




			repeat with thisMessage in theMessages

				--does the message have attachments? if not, then skip it
				if (every mail attachment of thisMessage) =E2=89=A0 {} then

					--make a subfolder for each message, named by subject & date sent
					set theBadName to (subject of thisMessage) & " | " & ((date sent
of thisMessage) as string)


					--delete colons from subfolder name so Finder doesn't choke on
them!
					set myChars to every character of theBadName

					repeat with i from 1 to (count of myChars)
						if item i of myChars =E2=89=A0 ":" then
							set theGoodName to theGoodName & item i of myChars
						else
							set theGoodName to theGoodName & "."
						end if
					end repeat

					--does subfolder already exist? If not, create it

					tell application "Finder"
						if (exists folder theGoodName of theParentFolder) then

							set theSubFolder to ((theParentFolder as string) & (theGoodName
as string)) as alias
						else
							set theSubFolder to (make new folder at theParentFolder with
properties {name:theGoodName})
						end if

					end tell

					--save the attachment(s) to the subfolder
					set theAttachments to (every mail attachment of thisMessage)
					repeat with thisAttachment in theAttachments
						set thePath to (theSubFolder as string) & (name of
thisAttachment)


						--if same named file already exists in subfolder, skip it and
display error message
						--otherwise, save it and get its size

						--	try
						save thisAttachment in thePath

						if file size of thisAttachment > 1000000 then
							set mySize to (((round ((file size of thisAttachment) /
10240)) / 100) as string) & " MB"
						else
							set mySize to (((round (file size of thisAttachment) / 1024) +
4) as string) & " KB"
						end if
						set theFileList to theFileList & return & (name of
thisAttachment) & tab & tab & (mySize)
						--		on error
						--			display dialog "Could not save " & (name of thisAttachment)
& " to " & thePath & " because of an error (probably because a file
with that name already exists."
						--	end try



					end repeat

					--build a list of newly created folders for Finder to open at the
end
					set theFolderList to theFolderList & (theSubFolder as alias)

					--save the message text as a .txt file in the subfolder
					set theTextFileName to "Message_" & (id of thisMessage as string)
& ".txt"
					set theTextFileContents to content of thisMessage

					tell application "Finder"
						set theTextFile to (make new file at theSubFolder with
properties {name:theTextFileName}) as alias
						open for access theTextFile with write permission
						write theTextFileContents to theTextFile
						close access theTextFile
					end tell

					set read status of thisMessage to true

					--make a new message to notify art department, if anything was
uploaded
					if theFileList =E2=89=A0 "" then
						tell (make new outgoing message with properties {subject:"Files
Received - " & (subject of thisMessage), content:"File(s) for " &
(subject of thisMessage) & " were uploaded on " & (current date) & "."
& return & return & "Name" & tab & tab & "Size" & return & theFileList
& return})
							make new to recipient at end of to recipients with properties
{address:"somebody@[EMAIL PROTECTED]
"}
							send
						end tell
					end if
				end if

			end repeat

			--open the newly created folders in Finder

			tell application "Finder"
				activate
				open theFolderList
			end tell
		end tell

	end perform mail action with messages
end using terms from
 




 1 Posts in Topic:
Works in Script Editor, but not as Mail Rule ... why?
misterfriendly <Luke.J  2007-10-16 07:33:37 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 8:11:49 CST 2008.