Monday, December 11, 2017

#1 2017-09-16 09:46:06 pm

Paddypatpat
Member
From:: Auckland, New Zealand
Registered: 2013-01-27
Posts: 5
Website

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/tutorial … --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:

Applescript:


-- 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

Last edited by Paddypatpat (2017-09-16 10:13:37 pm)


Filed under: Handlers

Offline

 

#2 2017-09-17 01:51:02 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Handlers and

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.


Applescript:

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)


regards

Stefan

Offline

 

#3 2017-09-17 04:37:20 am

Paddypatpat
Member
From:: Auckland, New Zealand
Registered: 2013-01-27
Posts: 5
Website

Re: Handlers and

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:

Applescript:


-- 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:

Applescript:


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.

Last edited by Paddypatpat (2017-09-17 04:38:53 am)


Filed under: Parameters, Handlers

Offline

 

#4 2017-09-17 06:54:35 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Handlers and

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.

Last edited by StefanK (2017-09-17 06:57:03 am)


regards

Stefan

Offline

 

#5 2017-09-17 11:22:35 pm

Paddypatpat
Member
From:: Auckland, New Zealand
Registered: 2013-01-27
Posts: 5
Website

Re: Handlers and

Ahh brilliant!

Thanks, I've learnt a heap.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)