Do as Applescript Execution Error when looping Objects

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Do as Applescript Execution Error when looping Objects

Post by Simon Knight » Thu Feb 28, 2019 8:31 am

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 ?
best wishes
Skids

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Do as Applescript Execution Error when looping Objects

Post by SparkOut » Thu Feb 28, 2019 12:00 pm

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"

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Do as Applescript Execution Error when looping Objects

Post by Simon Knight » Mon Mar 04, 2019 3:09 pm

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
best wishes
Skids

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Do as Applescript Execution Error when looping Objects

Post by bn » Tue Mar 05, 2019 8:04 am

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Do as Applescript Execution Error when looping Objects

Post by bn » Tue Mar 05, 2019 8:45 am

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

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Do as Applescript Execution Error when looping Objects

Post by Simon Knight » Tue Mar 05, 2019 8:55 am

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
Attachments
ReadEmailsToLivecode.livecode.zip
simple LC script
(5.24 KiB) Downloaded 220 times
best wishes
Skids

Simon Knight
Posts: 854
Joined: Wed Nov 04, 2009 11:41 am
Location: Gunthorpe, North Lincs, UK

Re: Do as Applescript Execution Error when looping Objects

Post by Simon Knight » Tue Mar 05, 2019 8:58 am

Oh no - now I have to wear two pointy hats ! Doh!
best wishes
Skids

Post Reply

Return to “Mac OS”