You define a variable setSubGroups
inside your first loop exactly count of theGroups
times without using it even once. Instead, you use it in the next repeat
loop.
I suggest to first clean up the repetitive stuff, i.e. move it into subroutines (“handlers” in AppleScript lingo, I think). And then rewrite your first repeat
loop as a recursive function that
- calls itself for all elements of
thesubGroups
(what’s the difference between the
something and this
something, anyway? And why is it not theSubGroups
?)
- does all this magical stuff with user custom metadata (there’s BTW a method to retrieve the value of those – easier than the longish
thisChild_CustomMetadata_Value
armageddon) for non-group children and appends the new object to theList
.
This has nothing to do with the transclusion thingies or what not: All that happens well after you’ve build your theListSubgroupChildren
and theListDirectChildren
and merged them into a simple theList
. That’s exactly what the sketchy function below does: it assembles the data in myData
for later use.
const myData = [];
function handleGroup(group) {
const children = children of g;
children.forEach(c => {
if (c is a group) {
handleGroups(c)
} else {
md = handleMetaData(c);
myData.push({sortBy: md.something, RefURL: c.referenceURL(), name: c.nameWithoutExtension()})
}
})
}
/* Do some more magic with myData */
This is no real code It’s just meant to illustrate how you might go about it. Although the AppleScript version is probably a bit more convoluted, it’ll certainly look very similar.
Evangelism below
Not to discourage you, but in my mind trying to go about this with all these overly long variable names and utterly broken string manipulations is not making things any easier. They are all distracting from the main tasks at hand.
If you really want to learn a programming language (one that has defined syntax that is) I’d suggest to look elsewhere.
set theList_sorted to script "AutoTransclusion_Script Library"'s sortedArrayUsingDescriptors(theList, true) -- replace TEST with your Script Library's name
then becomes
const sortedList = list.sort((a,b) => {a.sortBy - b.sortBy});
and doesn’t need an external library. And
repeat with i from 1 to (count of theList_sorted)
set end of theTransclusionRecord_Source_list to linefeed & linefeed & "<br>" & linefeed & "## " & (|NameWithoutExtension| of item i of theList_sorted) & ":" & linefeed & linefeed & "{{" & (|RefURL| of item i of theList_sorted) & "}}"
end repeat
becomes
sortedList.forEach(el => {
recordSource.push(`\n\n<br>\n## ${el.name)}:\n\n{{${el.RefURL}}}`)
})
Of course, you’re trading in the
, this
, those
, set to
and get from
for {}[](
and )
, but it’s till a lot less typing.