applescript returning execution error

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

applescript returning execution error

Post by rodneyt » Sat Oct 22, 2022 2:03 am

Hi everyone,

I'm using Applescript to get selected events from Apple Calendar, building on https://macscripter.net/viewtopic.php?id=45861
When I try to parseDefaults I get execution error back when executed inside Livecode. Does anyone have any idea why?
FYI I have given Livecode full disk access in security settings.

Apple script snippet:

Code: Select all

set defaultsReply to (do shell script "defaults read com.apple.ical SelectedEvents")
set selectedEvents to parseDefaults(defaultsReply)
return selectedEvents

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

Re: applescript returning execution error

Post by bn » Sat Oct 22, 2022 5:29 pm

Hi,

I selected an event and tried

Code: Select all

get shell("defaults read com.apple.ical SelectedEvents")
and it returned
{
iCal = (
"9F712C50-5B6D-4385-B0DC-3F19F4957B8F"
);
}
without an event selected it returned
{
iCal = (
);
}
I am using LC 9.6.9 rc1 on macOS Monterey

Maybe that helps.

Kind regards
Bernd

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: applescript returning execution error

Post by rodneyt » Sat Oct 22, 2022 11:17 pm

Itś the next line in the sample code that throws the exception....

Rod

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

Re: applescript returning execution error

Post by bn » Sun Oct 23, 2022 12:46 am

Rod,

Your script runs in ScriptDebugger(when adding on parseDefaults(resultText) handler

Attention, this is AppleScript, not LCS

Code: Select all

set defaultsReply to (do shell script "defaults read com.apple.ical SelectedEvents")
set selectedEvents to parseDefaults(defaultsReply)
return selectedEvents

on parseDefaults(resultText)
	set localUIDs to {}
	set {TID, text item delimiters} to {text item delimiters, quote}
	set resultItems to text items of resultText
	set text item delimiters to TID
	repeat with i from 1 to (count resultItems)
		if i mod 2 = 0 then set end of localUIDs to resultItems's item i
	end repeat
	return localUIDs
end parseDefaults
I'm running run out of ideas.

Kind regards
Bernd

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: applescript returning execution error

Post by rodneyt » Mon Oct 24, 2022 11:22 pm

Thanks Bn, yes does run in script editor, but not in Livecode, and it's not obvious why...

Will let you know if I discover anything more, otherwise will file as a bug report.

Rod

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: applescript returning execution error

Post by Klaus » Tue Oct 25, 2022 8:40 am

rodneyt wrote:
Mon Oct 24, 2022 11:22 pm
Thanks Bn, yes does run in script editor, but not in Livecode, and it's not obvious why...
bn wrote:
Sun Oct 23, 2022 12:46 am
Attention, this is AppleScript, not LCS

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

Re: applescript returning execution error

Post by bn » Tue Oct 25, 2022 8:45 am

Klaus wrote:
Tue Oct 25, 2022 8:40 am
rodneyt wrote:
Mon Oct 24, 2022 11:22 pm
Thanks Bn, yes does run in script editor, but not in Livecode, and it's not obvious why...
bn wrote:
Sun Oct 23, 2022 12:46 am
Attention, this is AppleScript, not LCS
Klaus,

Rod knows that, "Scripteditor" is the name of Apples App to run AppleScript. "ScriptDebugger" is also an app to run AppleScript with more options than Apple's Scripteditor.

Kind regards
Bernd

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: applescript returning execution error

Post by Klaus » Tue Oct 25, 2022 9:15 am

Oh, sorry, my fault!

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

Re: applescript returning execution error

Post by bn » Tue Oct 25, 2022 8:59 pm

Rod,

I could not resist to play around with the AppleScript. I found a way that it works.

Note: this is AppleScript, not LC script

Code: Select all

set defaultsReply to (do shell script "defaults read com.apple.ical SelectedEvents")
set localUIDs to {}
set {TID, text item delimiters} to {text item delimiters, quote}
set resultItems to text items of defaultsReply
set text item delimiters to TID
repeat with i from 1 to (count resultItems)
	if i mod 2 = 0 then set end of localUIDs to resultItems's item i
end repeat
return localUIDs
resolving the subroutine somehow made it work. (?)

Please note that it can take up to a couple seconds until iCal updates the database.
The AppleScript works even when the Calendar App is not open.

Kind regards
Bernd

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: applescript returning execution error

Post by rodneyt » Wed Oct 26, 2022 5:36 am

Awesome Bernd, that was what I was going to try next. I will give this a go now.

It is very odd that calling this subroutine throws exception but "in-lining" it is fine. I wonder if we should try to reduce it to minimum viable bug producing recipe and file as a bug report....

Rod

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: applescript returning execution error

Post by rodneyt » Wed Oct 26, 2022 6:03 am

Now that we've got Applescript returning the selected event in the Calendar, I'm trying to get details of the event, like this:

Code: Select all

set defaultsReply to (do shell script "defaults read com.apple.ical SelectedEvents")

set localUIDs to {}
set {TID, text item delimiters} to {text item delimiters, quote}
set resultItems to text items of defaultsReply
set text item delimiters to TID
repeat with i from 1 to (count resultItems)
	if i mod 2 = 0 then set end of localUIDs to resultItems's item i
end repeat

tell application id "com.apple.iCal" -- Calendar
	set theProps to properties of event id (item 1 of localUIDs) of (last calendar whose name is "Work")
end tell
return theProps
This works in OSX Applescript Script Editor app, but not when executed in Livecode.

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

Re: applescript returning execution error

Post by bn » Wed Oct 26, 2022 10:27 am

rodneyt wrote:
Wed Oct 26, 2022 6:03 am
This works in OSX Applescript Script Editor app, but not when executed in Livecode.
Rod,
The script worked for me after giving Livecode permission to access Calendar(iCal) in Security Settings.

The funny thing is that your original script started to work when the permissions were given for LC to access iCal...

Kind regards
Bernd

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: applescript returning execution error

Post by rodneyt » Wed Oct 26, 2022 12:52 pm

Odd, shouldn we get a permissions request if this is needed? So youŕe saying you went in to security permissions and granted permissions manually... interesting. OK I will give this a go tomorrow!

Rod

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: applescript returning execution error

Post by rodneyt » Thu Oct 27, 2022 2:21 am

In Automation pane of Privacy and Security I see LiveCode 10.0.0 (dp4) listed with permissions for Daylite, Chrome,DropDMG,System Events and Calendar. I don't see Livecode 9.6.4 listed, which is what I am running. When I test in Livecode 10.0.0 (dp4) I get the same error.

I wonder if I have to reauthorse the app. Reading up on this, some interesting links: Using above advice I ran

Code: Select all

tccutil reset AppleEvents 
-- this resets the automation pane completely. After doing this I launched Livecode 9.6.4 and execute the applescript. I get prompted for permission to automate Calendar (click OK). The script returns execution error. When I check in automation pane in security and privacy, it shows Livecode 10.0.0 (dp4) even though that's not the version of Livecode I had open...

I also tried building my app to a standalone and executing the applescript from there. I got prompted to automate Calendar (click OK to accept) and the script returns execution error.
Attachments
Screen Shot 2022-10-27 at 2.23.18 PM.png
Last edited by rodneyt on Thu Oct 27, 2022 2:43 am, edited 1 time in total.

rodneyt
Posts: 128
Joined: Wed Oct 17, 2018 7:32 am

Re: applescript returning execution error

Post by rodneyt » Thu Oct 27, 2022 2:27 am

From this post https://www.felix-schwarz.org/blog/2018 ... cos-mojave I found that opening this URL will take user directly to the automation pane in security and privacy.

It works in Livecode:

Code: Select all

launch url "x-apple.systempreferences:com.apple.preference.security?Privacy_Automation"
This is a useful tip for anyone building apps that need user to grant permissions, as it will take them directly to the right place to review.

Rod

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”