Handlers and

Hi there,

I’m trying to learn about using handlers. I’ve been reading Ben Wardie’s posts and trying to apply handlers to the Ultimate Beginners Guide to AppleScript example.

https://computers.tutsplus.com/tutorials/the-ultimate-beginners-guide-to-applescript--mac-3436

My script is below. My problem is that while the code works outside of the handlers, as soon as I put part of the code into a handler, then I get the error:

“Mail got an error: Can’t make or move that element into that container.”

Can someone point me in the right direction please?

Here is the code:


-- Variables
set recipientName to "Pat"
set recipientAddress to "me@example.com"
set theSubject to "An AppleScript email"
set theContent to "This email was sent using AppleScript."


-- Creating the message
on createMessage(messageSubject, messageContent)
	-- currently empty until I can get the next one working.
end createMessage


-- Set a recipient
using terms from application "Mail" -- Is this necessary?
	on setRecipient()
		tell application "Mail" 
			make new to recipient with properties {name:"Pat", address:"me@example.com"}
		end tell
	end setRecipient
end using terms from


-- create the Message
tell application "Mail"
	set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent}
	
	-- Pass the recipient details to the message
	tell theMessage
		my setRecipient()
	end tell
	
	-- Send this message
	tell theMessage
		send
	end tell
	
end tell

This main issue is that the “tell message” block is not passed to the handler, you have to add parameters for the message and name of address of the recipient.

The handler to create the recipient must be on the top level of the script (object).

using terms from is not necessary.

set theRecipientName to "Pat"
set theRecipientAddress to "me@example.com"
set theSubject to "An AppleScript email"
set theContent to "This email was sent using AppleScript."

-- Creating the message
on createMessage(recipientName, recipientAddress, messageSubject, messageContent)
	-- create the Message
	tell application "Mail"
		set newMessage to make new outgoing message with properties {subject:messageSubject, content:messageContent}
		
		-- Pass the recipient details to the message
		my setRecipient(newMessage, recipientName, recipientAddress)
		
		-- Send this message
		tell newMessage
			-- send
		end tell
		
	end tell
end createMessage

on setRecipient(theMessage, recipientName, recipientAddress)
	tell application "Mail"
		tell theMessage
			make new to recipient with properties {name:recipientName, address:recipientAddress}
		end tell
	end tell
end setRecipient

createMessage(theRecipientName, theRecipientAddress, theSubject, theContent)

Ahhhhh this makes a lot of sense. Thanks for this Stefan.

I’ve had a play around with the code and ended up with the following:


-- Variables
set recipientName to "Pat"
set recipientAddress to "me@example.com"
set theSubject to "An AppleScript email"
set theContent to "This email was sent using AppleScript."

-- Handlers

-- Create the Message
on createMessage(theSubject, theContent, recipientName, recipientAddress)
	
	-- Write the message
	set theMessage to my writeMessage(theSubject, theContent)
	
	-- Pass the recipient details to the message
	my setRecipient(theMessage, recipientName, recipientAddress)
	
	-- Send this message
	my sendTheMessage(theMessage)
	
end createMessage


-- Writing the message
on writeMessage(messageSubject, messageContent)
	tell application "Mail"
		make new outgoing message with properties {subject:messageSubject, content:messageContent}
	end tell
end writeMessage


-- Set a recipient
on setRecipient(theMessage, messageRecipientName, messageRecipientAddress)
	tell application "Mail"
		tell theMessage
			make new to recipient with properties {name:messageRecipientName, address:messageRecipientAddress}
		end tell
	end tell
end setRecipient

-- Send the message
on sendTheMessage(theMessage)
	tell application "Mail"
		tell theMessage
			send
		end tell
	end tell
end sendTheMessage

createMessage(theSubject, theContent, recipientName, recipientAddress)


I am wondering though, should the names of the parameters for a handler be different to what they are called elsewhere, for example:


on setRecipient(theMessage, messageRecipientName, messageRecipientAddress)

… uses messageRecipientName rather than the variable name of recipientName which contains the same data.

I’m guessing its a question of scope but is there a rule I should stick to now so that I’m practicing the right methods? Should parameter names always be unique I guess is my question.

It is indeed a question of scope. There is no rule how to name variables.

Personally I like to use different names to indicate that the local variables in a handler are not the same objects as the passed parameters in the call of the handler.

PS: my is only needed if the handler call is in a (application) tell block.

Ahh brilliant!

Thanks, I’ve learnt a heap.