droplet to attach files to a new Mail message problem

I’m having a little trouble with a script. The script, quite simply creates a new message in mail.app and automatically fills in the subject and body. That’s no problem. Actually, I found a script online that pretty much does it all, but that’s beside the point. I’ve adapted the script as a droplet that’s prepared to accept files dragged to it.

The problem is that I’d like to drag an item to the droplet and then attach the item that I dragged to the mail message. It fails. I can launch the droplet when an item is dragged to it, and I can display the file name and path. But it doesn’t attach properly to the mail message.

However, if I set the Attached file to an absolute path – using slashes as delimiters – it works fine, and I can easily attach a file.

I’m reaching the conclusion that the problem is that AppleScript stores the file’s path with colons as delimiters, and Mail.app doesn’t recognize such a path. I started poking around for a method to convert the delimiters from colons to slashes, but I couldn’t find a simple solution. Actually I couldn’t find any solution that I could get to work. Which leads me to believe that there’s got to be another problem that I’m not seeing.

The script, in its current state looks like this:


on open of finderObjects
	--display dialog finderObjects as string
	--this alerts the proper path delimited with colons.

	tell application "Mail"
		activate
		
		--set some variables for use in template message
		set theSubject to "PO# [first 5 characters of filename]"
		set theBody to "Attached is the art for PO# [first 5 characters of filename]" & return & "Please call if there are any questions." & return & return 
		set theAttachment to finderObjects
		--set theAttachment to "Kinigot/Users/john/Desktop/attach.jpg"
		--when I uncomment the line above, I can attach that file, but I can't seem to attach finderObjects.
		
		--create the message with variables
		set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
		
		tell newMessage
			-- Default is false. Determines whether the compose window will
			-- show on the screen or whether it will happen in the background.
			set visible to true
			
			if (theAttachment is not equal to "") then
				-- Position must be specified for attachments
				make new attachment with properties {file name:theAttachment} at after the last paragraph
			end if
		end tell
	end tell
end open

I’m stumped, and any help or advice is appreciated. Thanks in advance.

hmmm… it seems that isn’t problem after all. I succeeded in replacing the colons in the path name with slashes, and I still can’t get the file to attach properly unless I hard code the pathname.

edit: it seems that the problem was including the hard disk name in the path. It works now.

now to parse that file name down the first 5 characters!

Hi there, I’m new here but I hope this helps. It won’t bold in the code, but you can see where I tried to add the bold command. I added one line and changed another. Hope this helps you out there, sport.

on open of finderObjects
	[b]set theEntireFileString to finderObjects as string[/b]
	
	
	set theFileString to searchReplace(finderObjects, ":", "/")
	set theFileName to Get_file_name(theFileString)
	set theJobNumber to first character of theFileName & second character of theFileName & third character of theFileName & fourth character of theFileName & fifth character of theFileName
	
	tell application "Mail"
		activate
		
		--set some variables for use in template message
		set theSubject to "PO# " & theJobNumber
		set theBody to "Attached is the art for PO# " & theJobNumber & return & "Please call if there are any questions." & return & return
		
	[b]	set theAttachment to theEntireFileString as alias[/b]
		
		
		--create the message with variables
		set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
		
		tell newMessage
			-- Default is false. Determines whether the compose window will
			-- show on the screen or whether it will happen in the background.
			set visible to true
			
			if (theAttachment is not equal to "") then
				-- Position must be specified for attachments 
				make new attachment with properties {file name:theAttachment} at after the last paragraph
			end if
			
			
		end tell
		
		
	end tell
end open



on searchReplace(origStr, searchStr, replaceStr)
	set old_delim to AppleScript's text item delimiters
	set AppleScript's text item delimiters to searchStr
	set origStr to text items of origStr
	set AppleScript's text item delimiters to replaceStr
	set origStr to origStr as string
	set AppleScript's text item delimiters to old_delim
	return origStr
	
end searchReplace

on Get_file_name(x) -- Get the file name from the string “disk:folder:file” 
	set i to length of (x as text)
	set y to ""
	repeat until character i of (x as text) is ":"
		set y to character i of (x as text) & y
		set i to i - 1
	end repeat
	return y
end Get_file_name

is it possible to do this with several indiv files? so dropping several files on the droplet would create several messages with one attached file each, not one message with several files (which is what you get if you just drop the files on the mail dock icon, even).

so each (large) file would be attached to its own mail message; and the subject would be “copies for project; of 3, 1”, “copies for project; of 3, 2”, “copies for project; of 3, 3”

(the files i am thinking of are too big (PDFs of about 2.1 mb each) to email together in a single message; they are all going to the same recipient and cc people, but just one per message).

i figure it has to be code like “repeat with i from 1 to the count of finderObjects” or something but i cannot figure it out.

thanks.