AppleScript: traversing nested groups with recursive handlers

194 views
Skip to first unread message

James Lo

unread,
Aug 17, 2023, 10:37:44 AM8/17/23
to QLab
I'm sharing my first substantial AppleScript script in case someone finds it useful or interesting.  I haven't seen anyone use recursive handlers with Qlab and it occurred to me that they would be ideal for traversing nested groups if you needed to know the nesting structure.

global selectedCues
global nrSelected
global currentIndex
set majorCueNumber to 1 -- the number you want assigned to the first cue of the selection

tell application id "com.figure53.QLab.5" to tell front workspace
set selectedCues to selected as list
set nrSelected to count of selectedCues
set currentIndex to 1
--renumber top-level cues
repeat while currentIndex <= nrSelected
set currentCue to item currentIndex of selectedCues
set q number of currentCue to majorCueNumber
set currentIndex to currentIndex + 1
if q type of currentCue is "Group" then
my scanGroup(currentCue, majorCueNumber, 1)
else if continue mode of currentCue is not do_not_continue then
my scanContiguousAuto(majorCueNumber, 1)
end if
set majorCueNumber to majorCueNumber + 1
end repeat
end tell

--recursively scan group heirarchy and assign cue numbers to reflect nesting depth
on scanGroup(groupCue, prefix as text, minorCueNumber as integer)
tell application id "com.figure53.QLab.5"
repeat while currentIndex <= nrSelected
set currentCue to item currentIndex of selectedCues
if parent of currentCue is not groupCue then
return
end if
set q number of currentCue to prefix & "." & (minorCueNumber as text)
set currentIndex to currentIndex + 1
if q type of currentCue is "Group" then
my scanGroup(currentCue, prefix & "." & (minorCueNumber as text), 1)
end if
set minorCueNumber to minorCueNumber + 1
end repeat
end tell
end scanGroup

--treat contiguous blocks of autofollow/continue as a group for numbering
on scanContiguousAuto(prefix as text, minorCueNumber as integer)
tell application id "com.figure53.QLab.5"
repeat while currentIndex <= nrSelected
set currentCue to item currentIndex of selectedCues
set q number of currentCue to prefix & "." & (minorCueNumber as text)
set currentIndex to currentIndex + 1
if continue mode of currentCue is do_not_continue then
return
end if
set minorCueNumber to minorCueNumber + 1
end repeat
end tell
end scanContiguousAuto

Before:
before.png
After:
after.png

Rich Walsh

unread,
Aug 26, 2023, 6:18:46 PM8/26/23
to ql...@googlegroups.com
I wrote something that burrows down into groups back in 2016: https://groups.google.com/g/qlab/c/ldbL71jGawA/m/y1jwhoBmBgAJ.

I tend to clear all cue numbers in a selection before starting to write new ones, to avoid trying to use a number that’s still in use – but won’t be when I’m done.

I’m guessing you’ve seen all Mic’s AS work on https://qlab.app/cookbook/Sam’s Toolbox, the support examples at https://qlab.app/docs/v5/scripting/examples/ and my collection of stuff at wiki.allthatyouhear.com (which I need to update for v5 at some point)?

Rich

<before.png>
After:
<after.png>

--
Contact support anytime: sup...@figure53.com
Follow QLab on Twitter: https://twitter.com/QLabApp
User Group Code of Conduct: https://qlab.app/code-of-conduct/
---
You received this message because you are subscribed to the Google Groups "QLab" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qlab+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/qlab/92ce11e2-c4ef-4d66-be74-43c16c86d56dn%40googlegroups.com.
<after.png><before.png>

James Lo

unread,
Aug 27, 2023, 12:26:37 PM8/27/23
to QLab

Thanks!  I saw your iterative group burrower used in one of micpool's scripts--he called it "Rich Walsh's magic..." :)  Not sure if you are drawing a comparison, but since you process all cues below the top level the same way regardless of how deeply nested, recursion wouldn't add anything useful.  Plus my script doesn't burrow--each group's children have to have already been selected.  Your script introduced me to plurals ("cues of each cue") which had me giggling as I read about them.

I was only aware of the cookbook and the examples, thanks for letting me know about those other resources.  I probably shouldn't write things like "I haven't seen..." because I haven't searched very thoroughly either.  I'm mostly just reading through Apple's AppleScript Language Guide, which is how I found out that recursive handlers were possible. 

Rich Walsh

unread,
Aug 27, 2023, 5:12:03 PM8/27/23
to ql...@googlegroups.com
You’re right: I hadn’t fully unpicked your code to notice the handler calling itself – I’d skimmed it and thought you were doing something else. Sorry.

I started by reading all of the AppleScript Language Guide (the most recent pdf version I could find is here), and also found value in https://learning.oreilly.com/library/view/applescript-the-definitive/0596102119/ – which I had as an iOS app somehow…

Rich
Reply all
Reply to author
Forward
0 new messages