The thing to be aware of with collections like notes, is that Apple has created some syntactic sugar to make them look a little like JS arrays, but they are really osascript objects.
documents[0]
gets pre-preprocessed to the real underlying
documents.at(0)
and
documents['text.tbx']
gets rewritten to
documents.byName('text.tbx')
before the JS interpreter itself actually sees them.
(I personally just use .at(n)
and .byName(nameString)
, finding them slightly less confusing, and even fractionally faster at the last time of testing, though not perceptibly)
Meanwhile the .push
and .unshift
methods on that object are not really the JS Array methods which they look like, and don’t behave the same way.
We can obtain an actual Array of note values by writing:
documents['text.tbx'].byName('text.tbx').notes()
The trailling ( ) there creates an array. Without it, a plain
documents['text.tbx'].byName('text.tbx').notes
Is a reference to a function object.
(Manipulating an Array of note values, doesn’t however, make any changes to the order of the actual collection of notes in the app)
The note moving pattern is limited, as far as I can see, (in AS and JS) to moving an item into a new container.
So, for example, we could create a new note (the special collection.push()
method always appends), and then move it from the end of its current container to become a child of some other note.
Here, we move the freshly created note to become a child of the first note in the document:
(() => {
'use strict';
const main = () => {
// DECLARATIONS
const
tinderbox8 = Application('Tinderbox 8'),
// '~'' expanded to full path.
fp = filePath('~/Documents/test.tbx'),
newNote = new tinderbox8.Note({
name: 'an additional note of some kind'
});
// EFFECTS
tinderbox8.open(fp);
tinderbox8.activate();
const
doc = tinderbox8.documents.byName('test.tbx'),
notes = doc.notes; // Collection reference, not a JS array.
notes.push(newNote); // A special 'push', not the Array.push() method.
tinderbox8.move(
notes.at(-1), {
to: notes.at(0)
}
);
};
// GENERIC FUNCTIONS ----------------------------
// https://github.com/RobTrew/prelude-jxa
// filePath :: String -> FilePath
const filePath = s =>
ObjC.unwrap(ObjC.wrap(s)
.stringByStandardizingPath);
// CALL MAIN FUNCTION
return main();
})();