###How to Select (Filter) App Objects/Elements using of AppleScript & JXA (whose clause)
####Purpose of this topic:
- Show you how to learn essential information about scripting Mac applications
- Particularly, how to filter the object/element list from an App
tell application "Safari" --- GET A LIST OF SAFARI WINDOWS WITH VALID NAMES --- set winList to every window whose name is not missing value and name ≠ "" log winList -- log shows Window ID for each window element found. -->(*window id 14747, window id 12032*) --- GET THE NAME OF THE FIRST WINDOW --- set winFirst to item 1 of winList set winName to name of winFirst log (winName) -->(*Capture what’s on your mind | Evernote*) end tell
The KEY statement here is:
set winList to every window whose name is not missing value and name ≠ ""
This will show you how to compose statements like this one.
While I have shown an example in AppleScript, there is an analogous script for JXA.
Many of us Keyboard Maestro users are using and writing scripts more and more. One of the biggest challenges, whether it be AppleScript or JXA, is understanding the objects and elements exposed by an application, and then how to search/select/filter the entire list of available objects to those you wish to read or operate on.
####Where to Find Reference, How-to, Documentation
A syntax and command/function set unique to Mac automation scripting, is the “Reference Forms” for these application objects.
Reference Forms – AppleScript & JXA
- The best way to learn this, is to read the AppleScript Language Guide section on Reference Forms.
- You need to read this for coding in BOTH AppleScript and JXA.
- The examples are in AppleScript, but the same commands are available to JXA. There is no separate “Reference Form” guide for JXA.
Whose Clause for JXA – Filtering Arrays
IMO and IME, the best way to learn this stuff is trial and error. Open Script Editor, or even better Script Debugger, and just try stuff. Observe the results, mod your code, and run again. Do an internet search on "AppleScript " to get sample code to start with. For example: “AppleScript safari get open windows”
Please feel free to ask any questions about this subject (Reference Forms and Whose Clause) that you may have. The only dumb/stupid question, is the one not asked.