Page 1 of 3

applescript returning execution error

Posted: Sat Oct 22, 2022 2:03 am
by rodneyt
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

Re: applescript returning execution error

Posted: Sat Oct 22, 2022 5:29 pm
by bn
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

Re: applescript returning execution error

Posted: Sat Oct 22, 2022 11:17 pm
by rodneyt
Itś the next line in the sample code that throws the exception....

Rod

Re: applescript returning execution error

Posted: Sun Oct 23, 2022 12:46 am
by bn
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

Re: applescript returning execution error

Posted: Mon Oct 24, 2022 11:22 pm
by rodneyt
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

Re: applescript returning execution error

Posted: Tue Oct 25, 2022 8:40 am
by Klaus
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

Re: applescript returning execution error

Posted: Tue Oct 25, 2022 8:45 am
by bn
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

Re: applescript returning execution error

Posted: Tue Oct 25, 2022 9:15 am
by Klaus
Oh, sorry, my fault!

Re: applescript returning execution error

Posted: Tue Oct 25, 2022 8:59 pm
by bn
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

Re: applescript returning execution error

Posted: Wed Oct 26, 2022 5:36 am
by rodneyt
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

Re: applescript returning execution error

Posted: Wed Oct 26, 2022 6:03 am
by rodneyt
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.

Re: applescript returning execution error

Posted: Wed Oct 26, 2022 10:27 am
by bn
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

Re: applescript returning execution error

Posted: Wed Oct 26, 2022 12:52 pm
by rodneyt
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

Re: applescript returning execution error

Posted: Thu Oct 27, 2022 2:21 am
by rodneyt
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.

Re: applescript returning execution error

Posted: Thu Oct 27, 2022 2:27 am
by rodneyt
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