CaseLeaf

Handbook / Asking from AppleScript

Asking from AppleScript

Anything the command can do, a script can ask for too, and it can ask the window in front what it is showing while it does.

tell application "CaseLeaf"
    perform "marks" on "/path/to/report.pdf"
end tell

One call, every command

There is a single command, perform, and it takes the name of the command as its direct parameter. A dictionary with sixty entries would have to be rewritten whenever a command is added, and an out-of-date one is worse than a short one. So ask the application instead:

tell application "CaseLeaf" to get commands

answers with the commands perform will accept, from the running build rather than from a manual. They are the commands the command offers, because they are the same operations, plus the ones that only make sense with a window in front of you: arming a tool, the pane, the layout, the zoom, the tab, the sheets, what is showing, what is saved, picking, reading aloud, recording and playing, and the version.

on names the document to work on. options takes the rest, spelled as the command spells it:

tell application "CaseLeaf"
    perform "mark" on "/path/to/report.pdf" ¬
        options "--kind highlight --find \"force majeure\" --page 4"
end tell

Asking the window what it is showing

The quickest answer is a property rather than a command:

tell application "CaseLeaf" to get state

state gives what the front window is showing and doing, as JSON: the document and its path, the status line, the sheet and how many there are, the zoom, the armed tool, how many marks the list is showing and how many threads there are and how many are unanswered, the selected text and marks, whether a form is being edited, the words read-aloud is on and where the first of them sits, and what the window is doing (reading aloud, recording or playing). Whether the document is saved is a command of its own, saved. Reach for state when a script needs to know where it is.

With no window open it answers with an empty string rather than an error, so check for that first.

The rest are commands. They are about the window rather than a file, so they need no on: write perform "sheets", not get sheets, which asks AppleScript for a variable nobody defined. Every one of them but version refuses while no window is open, and says so in those words; version answers regardless, because which build is running is not a question about a window.

  • version - which build is running.
  • sheets · documents · showing - the page count and printed page labels, the open tabs left to right, and what a mark list currently holds.
  • saved - whether the document in front has unsaved edits.
  • arm - reports the armed tool, or arms one.
  • pane - shows one of the nine lists: history, marks, talk, bookmarks, snapshots, changes or fields in the right-hand panel, or contents or found in the sidebar beside the pages.
  • pick - selects a row in the marks, talk, bookmarks, snapshots or history list, counted from one, and scrolls it into view.
  • say · record · play - read the selection aloud, record while you read, or play the recording the mark in hand was made during. Each takes --do start or --do stop, start if left out; asking to start what is already running does nothing and says so.
  • stop - stops whichever of those the window's badge shows running.
  • layout - reports how the pages are laid out, or sets it: single, single-continuous, two, two-continuous, horizontal, book or book-continuous, with rightleft and breaks as on or off.
  • zoom - reports the zoom as a percentage, or sets it: width fits the width, page fits the page, actual is 100%, and a number of your own is a percentage.
  • tab - reports what is open, which is in front and what each is coloured; show, close and move take a tab's place in the strip counting from one, and colour takes red, orange, yellow, green, blue, purple, brown or none.

Each of these answers with what is in force when you ask for nothing. None of them toggles: a script cannot see which way a switch is, so it says which way it wants it.

Two more properties, for linking

address gives a caseleaf:// link to the place the window is showing, the mark being pointed at or the page being read. subject name gives what that place should be called. They exist for Hookmark, which uses exactly those two to make a link you can come back to, and they are useful to any script that wants to note where somebody was. Like state, both are empty rather than an error when no window is open.

Watch: a short video for this page is still to come.


The command · Hookmark · Contents