Broken script after Safari 5

I have a mail script I use to drag & drop a PDF file and send a mail message with the PDF as an attachment. It has worked fine up to the time I installed Safari 5 - now the message has a solid black background over the content and can’t be used. Safari 5 may just be coincidental.
I know the attachment part of the script is the problem.

Does anyone have a better or more correct way to write this script?

Thanks

Here is the script:


on open of finderObjects
	set theEntireFileString to finderObjects as string
	
	
	set theFileString to searchReplace(finderObjects, ":", "/")
	set theFileName to Get_file_name(theFileString)
	set theJobName to text 1 thru -5 of theFileName
	
	tell application "Mail"
		activate
		
		--variables for template message
		set theSubject to theJobName
		set theRecipient to "abc@xyz.com"
		set theCopy to "me@ourcompany.com, you@ourcompany.com"
		set theBody to "Greetings," & return & return & "Attached is " & theJobName & "." & return & "Print file(s) also attached." & return & "Print file(s) uploaded to FTP." & return & return & "Please let us know if there's anything else you need, or if any questions arise." & return & return & "Thanks very much," & return & return & "Me" & return & return & "Our Company" & return & "Easy Street" & return & "Our City, ST 12345"
		
		
		set theAttachment to theEntireFileString as alias
		
		
		--create message with variables
		set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}
		tell newMessage
			set my_recipient to make new to recipient at end of to recipients with properties {address:theRecipient}
			set cc_recipient to make new cc recipient at end of cc recipients with properties {address:theCopy}
		end tell
		
		tell newMessage
			-- visible default is false
			set visible to true
			
			if (theAttachment is not equal to "") then
				-- position 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 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

Model: G5
AppleScript: 2.0.1
Browser: Safari 533.16
Operating System: Mac OS X (10.5)

Hi, molinus.

You must have rewritten the script at about the same time as you installed Safari 5, because the first three lines of the open handler have no hope of working! The first assumes that only one item has been dragged onto the droplet. (Not necessarily fatal, but sloppy.) The second passes the alias list instead of the text coercion to searchReplace() to have its colons replaced with slashes. (That definitely errors the script.) The third line would pass the colon-less result of the second to a handler that uses colons to find the file name.

The top of the handler should look something like this:


on open finderObjects
	if ((count finderObjects) > 1) then error "Too many items dropped."
	
	set theAttachment to item 1 of finderObjects
	tell application "System Events" to set theFileName to theAttachment's name
	if (theFileName does not end with ".pdf") then error "The dropped item isn't a PDF file."
	
	set theJobName to text 1 thru -5 of theFileName
	
	-- Mail stuff here.

The above also does away with the need for the searchReplace() and Get_file_name() handlers.

I don’t use Mail so I can’t check your code for it. I’ve already set theAttachment above to the required alias and haven’t used theEntireFileString, so you should omit the line:

set theAttachment to theEntireFileString as alias

Since theAttachment is an alias, it’ll never be equal to “”, so you may as well delete the ‘if’ condition a few lines further down too.

Hi,

this is a bug in Safari 5 respective WebKit, look here
A workaround is to switch text formatting to plain text right after sending the send command

Hi Stefan,

I actually wrote the script a couple of years ago, and although sloppy, it has worked fine. Only when I installed Safari 5 did the black background issue make it unusable.

I appreciate the more efficient script code you provided, and alerting me to the WebKit bug.

Thanks again