IMPORTANT: case sensitivity when resolving note name matches differs from action code
AppleScript's named
operator is, unfortunately, case-insensitive. Though consistent with Finder it is unlike normal Tinderbox treatment of $Name data. So, be aware that whilst
note "X"
is only a short way to write
note named "X"
… i.e. both examples use named
, even if it is only explicit in the latter. This means the code acts case-insensitively to match the first ($OutlineOrder-based) match to notes named 'X' or 'x' which otherwise would be unique names in Tinderbox (action code). By contrast Tinderbox action code can distinguish note 'X' from note 'x' but if passed two notes called 'x' as a match, it would pick the first by outline order.
If affected by this consider resolving correct identity based on $Path or $IDString data instead and see the find note in
example further below under referencing notes. If needing to do multiple operations on a note (or in scope of a note) use a more precise method to set a reference to the note and then re-use the reference as needed.
Creating new notes
This creates a new top-level note, and creates an agent (with)in that note:
set myNote to make new note in document "Workspace.tbx"
set myAgent to make new agent in myNote
The default $Name values for a note made like this is 'new note' and for an agent 'agent'. Also note it is not possible to apply a note name until the note is created, i.e. not in the same line of code.
set myNote to make new note in document "Workspace.tbx"
To set the $Name of the note [sic] created in the code above:
set name of myNote to "inbox"
When using the make new command, note that the returned designator it gives is based on the current outline position of the newly created note, and subsequent calls that make or delete notes might render the invalid (use the find verb to re-set the variable's stored location.
Just as with $Name other attributes can be set (see also Attribute values):
set value of attribute "Width" of myNote to 5
Creating new notes other than at root
Whilst the most trivial example inserts a new note at the root of the TBX's outline, a more likely scenario is needing to generate something new at a particular location, e.g. inside an existing container.
There is no method to create a path-type AppleScript object from a literal path string value, so a rather roundabout method is needed. this uses a 'find' method to create the location)path) object. The latter can then be used to make a new note, like so:
tell front document
set newPath to find note in it with path "/Z/Y"
set newNote to make new note at newPath
set name of newNote to "X"
end tell
The 'it' in line #2 is not strictly necessary but '...find note in with...
' reads like an error in the code.
So the sequence is find the note by path setting a variable, make a note using the varaible as the path data, and lastly name the note.
Getting references to notes
To get a list of all the note inside the referenced 'myNote':
get notes in myNote
Or, the agents in 'myNote':
get agents in myNote
Or, the adornments in 'myNote':
get adornments in myNote
To return the name of the third top-level note in the specified document:
name of note 3 of document "Workspace.tbx"
Finding a specific note
To return a reference to the designated note:
find note in [note or document] with path "/path/to/note"
If the target is a document, the path should be an absolute path. If the target is a note, the path can be an absolute path or a relative path with respect to that note.
Acting on a selection of notes
A way to act on all selected notes (a common operation):
repeat with anItem in selection of front document
set value of attribute "Color" of anItem to "red"
end repeat
… see more on selections.
Returning an offset reference to a note
To return a reference to myNote's container, i.e. its 'parent' property (see Note properties below),:
set theContainer to find note in myNote with path "parent"
Moving a note
To move a note to the specified container (as specified in the example above):
move note named "X" to theContainer
Deleting a note
Use:
delete myNote
Note Properties
Each note object has a number of AppleScript properties:
-
child
(action code designator 'child' or 'child[0]') -
color
(i.e. $Color) -
lastChild
(action code designator 'lastChild') -
name
(i.e. $Name) -
nextSibling
(action code designator 'nextSibling') -
parent
(action code designator parent) -
previousSibling
> (action code designator 'prevSibling') -
text
(i.e. $Text)
In a few cases the property is an alternate way to access a specific system attribute for the note, but for most these properties are the equivalent of calculated item designators in action code.
See also—notes linking to here: