AppleEvent Handler Failed

I had the same error until I included the “tell sheet 1 of sheet 1 of window 1”. Then that part worked. The script still works for me. This is more confusing then I thought was possible in AppleScript.:slight_smile:

Just ran the original script on my Laptop which is still using Snow Leopard and it worked perfectly, so like you I am confused.

Got stopped just then so will now run permissions repair and restart and let you know how I go.

Totally unrelated (I think) I am having trouble with my magic mouse a single click often (not always) opens dialog boxes like Icon, Icon Text etc. Have switched to the pad and that seems better.

Hi,

GUI scripting is a weird thing, because it’s time critical.
The static delay statements may work on the computer which the script has been written on.
But there’s no guarantee that the script works reliably on every computer.

Using dynamic delays (waiting for existence of certain UI elements) is much more reliable


set theFolder to (choose folder) --choose folder to save attachments (i did this just so my script would work)

activate application "Mail"

tell application "System Events"
	tell process "Mail"
		--here's where your script went slightly wrong. The three periods after "Save Attachments" are part of the name
		click menu item "Save Attachments." of menu "File" of menu bar 1
		tell window 1
			repeat until exists sheet 1
				delay 0.2
			end repeat
			keystroke "g" using {command down, shift down} --open go to folder sheet
			repeat until exists sheet 1 of sheet 1
				delay 0.2
			end repeat
			tell sheet 1 of sheet 1
				set value of text field 1 to POSIX path of theFolder --the go to folder text field
				click button "Go"
			end tell
			repeat while exists sheet 1 of sheet 1
				delay 0.2
			end repeat
			tell sheet 1 --this automatically switches to the first sheet again
				click button "Save" -- the button on the first sheet
			end tell
		end tell
	end tell
end tell


Hi Stefan

and thanks once again you may not remember but you helped me with the original script. Should have thought of dynamic delays myself. Still very strange that the scrip worked on this machine under snow Leopard and does not under Lion.

anyway thanks again all I have to do is adapt that to my original scrip.

Peter

try


set POSIXPathOfTheFolder to POSIX path of (choose folder) --choose folder to save attachments (i did this just so my script would work)
.

	set value of text field 1 to POSIXPathOfTheFolder --the go to folder text field
.

Hi Layne & Stefan

Works at last :slight_smile: clumsy in places but I can run it with a hotkey and it sure helps when tidying up mail. I wonder what other problems we will have with Lion.

thank you both for all your help.

Peter

--This script is underdevelopment 7/30/2011 to take a selected email message save it as a PDF file and then save any attachments to the same folder with the file name given to the attachment by the sender. Numbners files sequentially so rather than 1,11 we have 1,2 etc . 
global listOfFiles, filecount, Subject_, destinationFolder, d
activate application "Finder"
tell application "Finder"
	set destinationFolder to (choose folder)
	set listOfFiles to the name of every file in destinationFolder
	set filecount to count every item in listOfFiles
end tell
activate application "Mail" --Brings window to the front
tell application "Mail"
	set themsg to selection
	if (count of themsg) is less than 1 then --Makes sure a message has been selected in mail box
		display dialog "Please select a message in Mail before running this script."
	else
		set themsg to item 1 of themsg
		set theMsgCount to {}
		tell themsg
			set Subject_ to subject of item 1 of themsg
			set Sender_ to sender of item 1 of themsg
			set Recipients_ to to recipients
			set Recipients_ to address of first recipient of item 1 of themsg
			set Sender_ to extract name from Sender_
			set brkpos to offset of "@" in Recipients_ --Finds position of start of URL in email address
			--display dialog "Break " & brkpos
			set Recipients_ to characters 1 through (brkpos - 1) of Recipients_
			display dialog "Sent to " & Recipients_ & return & "Sent by " & Sender_
			
			set theMsgCount to mail attachments of themsg
			set theMsgCount to count every mail attachment of themsg
			
			set Subject_ to text returned of (display dialog "From " & Sender_ & return & "Proposed file name is in the box" & return & "Select OK or  start typing to erase and change" & return & "and then Select OK" buttons {"OK", "Change"} default button "OK" default answer Subject_)
			
			set Subject_ to my FixFileName(Subject_) --Calls handler to strip bad characters
			
			set Subject_ to my getEmptyPath((destinationFolder as string), Subject_)
			activate application "Mail" --Brings window to the front
		end tell
		
		my FileasPDF() --Calls handler to process the save part of routine
		my SaveAttach() -- Now using GUI in Lion to work
		
		
		set check to the button returned of (display dialog "Delete " & return & Subject_ & return & " from Mail " buttons {"Yes", "No"} default button 1)
		if check = "Yes" then
			delete themsg
		else
			set check to the button returned of (display dialog "Standard proceuder is to flag the message to show it has been saved" & return buttons {"yes", "No"} default button 1)
			if check = "Yes" then
				tell application "Mail"
					activate
					tell application "System Events"
						tell process "mail"
							keystroke "l" using {command down, shift down}
						end tell
					end tell
				end tell
			end if
		end if
	end if
end tell

------------------------------------------
on FileasPDF()
	tell application "System Events"
		tell process "Mail"
			keystroke "p" using command down
			repeat until exists sheet 1 of window 1
				delay 0.2
			end repeat
			tell sheet 1 of window 1
				click menu button "PDF"
				repeat until exists menu 1 of menu button "PDF"
					delay 0.02
				end repeat
				click menu item "Save as PDF." of menu 1 of menu button "PDF"
			end tell
			repeat until exists window "Save"
				delay 0.2
			end repeat
			tell window "Save"
				keystroke "g" using {command down, shift down}
				repeat until exists sheet 1
					delay 0.2
				end repeat
				tell sheet 1
					--Subject_Added Peter to test how to pass file name
					set value of text field 1 to POSIX path of destinationFolder & "/" & Subject_
					click button "Go"
				end tell
				repeat while exists sheet 1
					delay 0.2
				end repeat
				click button "Save"
			end tell
		end tell
	end tell
end FileasPDF
-----------------------------
on SaveAttach()
	tell application "System Events"
		tell process "Mail"
			--here's where your script went slightly wrong. The three periods after "Save Attachments" are part of the name
			click menu item "Save Attachments." of menu "File" of menu bar 1
			tell window 1
				repeat until exists sheet 1
					delay 0.2
				end repeat
				keystroke "g" using {command down, shift down} --open go to folder sheet
				repeat until exists sheet 1 of sheet 1
					delay 0.2
				end repeat
				tell sheet 1 of sheet 1
					set value of text field 1 to POSIX path of destinationFolder --the go to folder text field
					click button "Go"
				end tell
				repeat while exists sheet 1 of sheet 1
					delay 0.2
				end repeat
				tell sheet 1 --this automatically switches to the first sheet again
					click button "Save" -- the button on the first sheet
				end tell
			end tell
		end tell
	end tell
end SaveAttach
-----------------------------------------
on FixFileName(str) --Deletes characters that cannot be used in file names
	--display dialog "Check Filename for duplicates"
	--display dialog "Fixed str " & str
	set fixed_string to {}
	set bad_char to {":", "/"}
	repeat with c from 1 to (count every character in str)
		if bad_char contains (character c of str) then
			set end of fixed_string to "-"
		else
			set end of fixed_string to (character c of str)
		end if
	end repeat
	fixed_string as string
	set str to fixed_string
end FixFileName
---------------------------------------
-- See if a file with the passed name exists
-- If so, add an integer to the name, and check again
-- Keep checking until an unused name is found
-- Parameters:	path of container as text
--			             file name, no extension
-- This is only intended to save mail messages as PDF's so that extension is added in the handler
-- Returns:		updated file name

on getEmptyPath(destinationFolder, Subject_)
	set filePath to destinationFolder & Subject_
	set Ext to ".PDF" --only checking for file type PDf as that is how the message is being saved
	tell application "Finder"
		if exists file (filePath & Ext) then
			set x to 1 -- start counting
			repeat
				set cFilePath to filePath & " " & x & Ext
				if exists file cFilePath then -- found empty spot?
					set x to x + 1 -- no, keep counting
				else -- yes, leave
					exit repeat
				end if
			end repeat
			return Subject_ & " " & x -- this name is safe to use
		else
			return Subject_ -- use original name
		end if
	end tell
end getEmptyPath

-----------------------------------------
on ParseName(str) --Takes attachment name and strips extension not in use
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	set str to every text item of str as list
	set AppleScript's text item delimiters to " "
	set str to every text item of str as text
	set strcnt to count of words in str
	set str3 to {}
	set ext1 to last word of str
	if strcnt is greater than 2 then
		repeat with n from 1 to strcnt - 1
			set str2 to word n of str
			set str3 to str3 & str2
		end repeat
	else
		set str3 to word 1 of str
		display dialog "In Parse Name " & str3
	end if
	set str3 to str3 as string
	set str to my replaceChars(str3)
end ParseName
---------------------------------
on replaceChars(thisText) --Removes spaces not in use called from ParseName
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to the " "
	set the itemList to every text item of thisText
	set AppleScript's text item delimiters to the ""
	set thisText to the itemList as string
	set AppleScript's text item delimiters to ""
	return thisText
	set AppleScript's text item delimiters to d
end replaceChars