Scanner --> PDF --> Mail attachment script

i read recently that teens in ginza have found a new way to instant message: instead of thumb typing a message into their phones, they scribble a message onto a piece of paper, snap a picture of it with their phone-cam, and zap that to their friend’s phone (and toss the original).

i thought i could do something like that with my scanner and my tiBook: scan a quick note to a pdf and email that pdf and trash the pdf (and the original). (my canon lide doesn’t recognize mail as a program so i cannot just use the email button on it; which is fine – i like doing it with applescript)

i went back to a script sal from cupertino was kind enough to write in response to a post at a macnn forum. i’ve humbly edited it a bit so that the new message has my default signature and i’ve added a line to trash the pdf original.

what i cannot fix: before the message appears that gets the attachment and everything (which works out fine), a message is created that stays empty – so, when the script is done, i have the message with my signature and the attached pdf; but behind it is another empty message the script created; two messages, one full and good and one empty and useless. how can i not get that empty message? i know i’m not too bright; i just do not see how it gets created.




property theName : "My Addressee"
property theAddress : "addressee@mac.com"

on adding folder items to this_folder after receiving these_items
	
	tell application "Mail"
		activate
		set new_message to make new outgoing message at beginning of outgoing messages
		tell content of new_message
			set visible of new_message to true
			tell the new_message
				make new to recipient at end of to recipients �
					with properties {name:theName, address:theAddress}
				
				repeat with i from 1 to number of items in these_items
					set this_item to item i of these_items
					set this_info to info for this_item
					set the file_name to the name of this_info
					tell content
						make new attachment with properties �
							{file name:this_item} at �
							before the first character
					end tell
					set the subject to subject & the file_name & space
				end repeat
			end tell
		end tell
	end tell
		
	tell application "Finder" to move (every file of this_folder whose name ends with ".pdf") to the trash
	
end adding folder items to

Try commenting out the line ‘tell application “Finder” to move (every file of this_folder whose name ends with “.pdf”) to the trash’. I think that when a file is moved to the trash by the Finder, the file is actually duplicated then moved and the duplicate may kick off the folder action script again but this time the file isn’t there so you end up with a blank message. If this is the case, you may want to use an alternate method to delete the added items (such as shell rm command).

Jon

thanks, but that doesn’t cure it.

it’s really odd, the script creates one message, then ignores it, creates a second message and, with that 2d message, sets the addressee, adds the attachment, etc. it happens at the top of the script somehow.

Here’s a slightly modified version of the script (it will add the attachments in the same order they are listed in the subject and the the subject list will be separated by commas):

property theName : "My Addressee"
property theAddress : "addressee@mac.com"

on adding folder items to this_folder after receiving these_items
	set file_count to (count these_items)
	tell application "Mail"
		activate
		set new_message to make new outgoing message at beginning
		tell new_message
			set visible to true
			make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
			repeat with i from 1 to file_count
				set this_item to (item i of these_items) as alias
				tell content to make new attachment with properties {file name:this_item} at after character -1
				set subject to subject & (name of (get info for this_item))
				if i is not file_count then set subject to subject & ", "
			end repeat
		end tell
	end tell
	tell application "Finder" to delete these_items
end adding folder items to

You had some strange nested tells in your original script “tell content…tell message…tell content” that I also eliminated. The first time I ran the script it did indeed create a blank message and then the proper message. On all subsequent runs, however, only the one message with the attachments was created.

Jon

thanks for your help. but i still get a first, ghost message.

i changed one of your lines (your script is more concise) so that the file would attach:

tell content to make new attachment with properties {file name:this_item} at before the first character

it’s not a big deal, of course, but i would like to not produce that first message.

i guess i could just accept that it will come and close it; this works, actually (that first message opens, closes, then the 2d message opens and gets an addressee, an attachment, and a subject):

 property theName : "My Addressee"
property theAddress : "addressee@mac.com"

on adding folder items to this_folder after receiving these_items
	set file_count to (count these_items)
	tell application "Mail"
		activate
		set new_message to make new outgoing message at beginning
		tell application "Mail" to close window 1
		tell new_message
			set visible to true
			make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
			repeat with i from 1 to file_count
				set this_item to (item i of these_items) as alias
				tell content to make new attachment with properties {file name:this_item} at before the first character
				set subject to subject & (name of (get info for this_item))
				if i is not file_count then set subject to subject & ", "
			end repeat
		end tell
	end tell
	tell application "Finder" to delete these_items
end adding folder items to


perversely, commenting out the line above the close window code breaks the script.

this works, but it seems pretty odd.

If you change:

tell content to make new attachment with properties {file name:this_item} at after character -1

to

tell content to make new attachment with properties {file name:this_item} at before the first character

then the attachments will be in reverse order from how they are listed in the subject line. I changed this so that the order they are attached and the order of the subject line will match, it shouldn’t make any other difference as to how the files are actually attached except for their order.

As to adding your “close window 1” line, that is fine but you shouldn’t do a double tell (you are already telling application “Mail”) and you should add a test so that it will only close the window if indeed it is a new, blank outgoing message window. On my system, since the “ghost” message is not created, that would have unintended consequences.

Jon

it’s just that there was no attaching going on with your line; i’m not trying to substitute my wisdom for yours, just trying to get functionality.

i don’t see why we would get different results with the same script, it seems pretty straightforward. i have always had the impression that scripting Mail gets wonky results.

the script seems to be working now, 'though it my lack elegance.

(if it matters, i’m running 10.3.2 on a tiBook, G4, 667)

I am also running Mac OS X 10.3.2 and my version of Mail.app is 1.3.3 (v612). The two variations on the line to actually attach the file to the content both work on my machine but using your line results in the files being attached in reverse order since the next file is always placed before the previous file. Are you sure the files aren’t actually being added using my line? Try sending the message created using my line to yourself and see if the files are actually attached.

Jon

yup, i’m sure. everything else seems to work. the script works for me, so i’m pretty happy. i am wondering why it all seems so wonky, but it seems good enough for my use.

thanks for your help. really appreciate you taking the time.

(i’ve got to do some work now; if you post again, i’ll respond but it won’t be for a bit. thnaks again)