Inline AppleScript Documentation

2023-01-0323:056415indiestack.com

While perusing Xcode's AppleScript scripting dictionary, I was surprised to discover a rather robust "example code" section included right there among the usually spartan reference of the app's…

While perusing Xcode’s AppleScript scripting dictionary, I was surprised to discover a rather robust “example code” section included right there among the usually spartan reference of the app’s scriptable entities:

Screenshot of Xcode's scripting dictionary, including a section with example script code for the 'build' command

Curious to learn more, I used a relatively little-known trick for examining the raw source code of a scripting dictionary. Simply click and drag from Script Editor’s document proxy icon, into a text editor such as TextEdit, Xcode, or BBEdit:

Screenshot of Script Editor window for a scripting dictionary, with annotations to show where the document proxy icon is

This trick is especially handy because even if the app in question doesn’t have a standard scripting definition (.sdef) file representing its interface, Script Editor will generate one dynamically for you. It’s a quick-and-dirty way to learn how specific outcomes are achieved, and how you might incorporate similar features in your own app’s scripting definition file. In this case, I discovered a new (to me) “documentation” element in the file:

Screenshot of source code reflecting the example script inline documentation pictured above

I was not aware of the feature until a few weeks ago, but apparently it’s been there since at least Mac OS 10.5. Script Debugger supports it, too! You can read more about the “documentation” element by invoking “man 5 sdef” from the Terminal:

When an element needs more exposition than a simple ‘description’ attribute can provide, use a documentation element. A documentation element may contain any number of html elements, which contain text that will be displayed at that point in the dictionary.

Given the sad state of support for AppleScript by Apple, and the continued low level of adoption by 3rd-party developers, a feature like this will probably never be widely used or celebrated. But at least in FastScripts 3.2.4, which I just released this morning, I’m now taking advantage of it:

Screenshot showing FastScripts 'search text' command with an inline documentation block

If you’re among the few remaining Mac developers who is investing time and effort into your AppleScript scripting definitions, hopefully you’ll find this feature useful!


Read the original article

Comments

  • By tempodox 2023-01-0421:361 reply

    Helpful Article. OSA is one more area where Apple's documentation is woefully lacking by now. For using JavaScript instead of AppleScript I don't find any docs at apple.com but on GitHub: https://github.com/JXA-Cookbook/JXA-Cookbook/wiki#table-of-c....

  • By favorited 2023-01-052:052 reply

    I use AppleScript nearly every day, mostly to grab little bits of info from one app & tell another app to do something with it. For example, I have one that grabs the URL & page title of the foremost Chrome tab, switches to Terminal, and starts a youtube-dl process with the Chrome URL. It's just a little shortcut to download whatever I'm currently watching.

    It's wonderful because it's so much simpler than building a real integration or application, but it's also terrible because of (A) how tedious it is to actually write AppleScript and (B) how flaky/racy UI automation can be unless you're using great tools.

    • By bobbylarrybobby 2023-01-055:50

      AppleScript is a terrible yet reasonably well documented language. At the other end of the spectrum is JavaScript for automation (JXA), which exposes all the same APIs AppleScript does (modulo the spelling of function names), is a much more sane language, and is barely documented. I find that AppleScript is like bash and JXA is like python in terms of how complicated a script can be before you swift from the former to the latter.

    • By WirelessGigabit 2023-01-058:13

      Ha. I run a shortcut on my phone to send the url to metube on my server.

  • By sgt 2023-01-0420:432 reply

    Used AppleScript as recently as 2018 - for a small commercial project even. It works great and I don't think anything else comes close, aside from proper programming/scripting which isn't a fair comparison for the typical AppleScript use-cases.

    • By navanchauhan 2023-01-0420:542 reply

      Can you talk a bit more about this? The only experience I have with AppleScripts, is attaching it to a folder so that any PDFs that were dropped in it were OCRed using Azure, and then converted to a blog post

      • By sgt 2023-01-0423:16

        It was actually for remote control via iMessage. Rendered less useful nowadays due to Apple releasing an API for business (https://register.apple.com/resources/messages/msp-rest-api/)

      • By ace2358 2023-01-0422:161 reply

        That’s pretty cool!

        I use AppleScript in my business for doing image conversion and sorting.

        It’s a pretty stealth feature built into the os that’s been there for years.

        • By sgt 2023-01-0423:17

          Yeah my thought too, that's a pretty cool use of AppleScript. Working with PDF's one generally wants to do from your own machine anyway, so it's ideal. You could even send documents or photos through to Azure's facial recognition API and order based on people/faces. Just an idea.

    • By pram 2023-01-050:461 reply

      I think the main problem AS has is you can accomplish a whole lot with Automator, and now Shortcuts.

      • By ace2358 2023-01-052:50

        I use AS inside my Automator actions along with shell scripts. It all works together!

HackerNews