Page 1 of 1

Do as Applescript Execution Error when looping Objects

Posted: Thu Feb 28, 2019 8:31 am
by Simon Knight
Hi,
I am attempting to get the text and some header information from Apple Mail. I have written a working AppleScript that I plan to use from within Livecode. However, it throws an Applescript execution error when called using the Do command. A very truncated version of the script is listed below. The variable tMessageList is being set to a list of email objects. The error occurs when the code attempts to loop through the list of objects in the repeat loop. This same code works o.k. from applescript editor.
tell application "Mail"
-- get all the messages in the list with same subject.
set tMessageList to selection
set tEmail to ""
set tbreak to "----------------------- ! -----------------------"
set tOutput to ""
set aMessage to ""

repeat with aMessage in tMessages -- loops from first to last

--From:
--set tSender to the sender of (item x) of tMessages
end repeat

end tell -- Apple Mail
The applescript is held in a field and run from a normal button. The script of the button is here:

Code: Select all

on mouseUp pMouseButton
   put textencode(field "Applescript","UTF-8") into tScript
   
   do tScript as applescript
   
   answer the result
   
end mouseUp
I added textencode as part of my clutching at straws debugging.

Any ideas on why the repeat loop throws an execution error ?

Re: Do as Applescript Execution Error when looping Objects

Posted: Thu Feb 28, 2019 12:00 pm
by SparkOut
Not being a Mac user, I reserve the right to be wrong, but it looks like your AppleScript has LiveCode script lines embedded in it. When you give the code to "do as AppleScript" the Apple interpreter is hitting the LiveCode statements and failing.
You would need to avoid mixing the script languages together. Perhaps have a LiveCode handler that does all the looping and writes a series of lines after the field (or just into a variable) to build up the whole AppleScript routine and once the whole script is constructed, then you can "do as AppleScript"

Re: Do as Applescript Execution Error when looping Objects

Posted: Mon Mar 04, 2019 3:09 pm
by Simon Knight
Hi Sparkout,

Thanks for your reply. I am uncertain which lines of the Applescript code you are referring to. The code is similar to Livecode but is more obscure. The "set" statements are used in the same way as Livecode uses "put" but in the case of the "tMessageList" the variable is populated with an Applescript List of mail objects. Applescript allows the objects to be looped through in a repeat statement which is where the full code reads the values held in properties of the mail objects. For example the line "set tSender to the sender of aMessage" populates the variable tSender with the email address of the sender of the subject message which is held in the property "sender".

All the Applescript has been tested from the Applescript editor and works. The problem is that the same code throws the error when it hits the repeat statement when executed from within Livecode script.

best wishes
Simon

Re: Do as Applescript Execution Error when looping Objects

Posted: Tue Mar 05, 2019 8:04 am
by bn
Hi Simon,

maybe this is the problem
repeat with aMessage in tMessages -- loops from first to last
you declare the variable as
set tMessageList to selection
tMessage vs tMessageList.

The script you posted throws an error when executing in AppleScript.

Kind regards
Bernd

Re: Do as Applescript Execution Error when looping Objects

Posted: Tue Mar 05, 2019 8:45 am
by bn
Hi Simon,

I now tried:

Code: Select all

tell application "Mail"
	-- get all the messages in the list with same subject.
	set tMessageList to selection
	set tEmail to ""
	set tbreak to "----------------------- ! -----------------------"
	set tOutput to ""
	set aMessage to ""
	
	repeat with aMessage in tMessageList -- loops from first to last
		
		--From:
		set tSender to the sender of aMessage
		set tOutput to tOutput & tSender & return
	end repeat
	return tOutput
end tell -- Apple Mail
in field 1

and

Code: Select all

on mouseUp pMouseButton
   local tScript
   put field 1 into tScript
   
   do tScript as "applescript"
   
   put the result into field 2
   
end mouseUp
in a button

This worked for me
I am on MacOS Sierra 10.12.6 using LC 9.0.3 DP1
Kind regards

Re: Do as Applescript Execution Error when looping Objects

Posted: Tue Mar 05, 2019 8:55 am
by Simon Knight
Dear All,

I have discovered my error : being uncertain of how the "do" command works I simplified my Applescript by deleting anything complicated and as I said above it worked. When I added the repeat loop I failed to restore the following two lines

Code: Select all

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


With these added the script runs without error.

I have attached my LC stack which includes the full Applescript should anyone else be interested in getting data from Applemail.

Now I'm off to stand in the corner wearing a pointy hat......

best wishes
Simon

Re: Do as Applescript Execution Error when looping Objects

Posted: Tue Mar 05, 2019 8:58 am
by Simon Knight
Oh no - now I have to wear two pointy hats ! Doh!