[HN Gopher] AppleScript: Graphic User Interface (GUI) Scripting
___________________________________________________________________
AppleScript: Graphic User Interface (GUI) Scripting
Author : behnamoh
Score : 52 points
Date : 2022-09-06 15:21 UTC (7 hours ago)
(HTM) web link (www.macosxautomation.com)
(TXT) w3m dump (www.macosxautomation.com)
| crazygringo wrote:
| For those complaining about bizarre centered code formatting...
|
| Seems like it's a bug where this one page isn't loading the
| CSS/formatting the rest of the site has.
|
| The parent page:
|
| https://macosxautomation.com/applescript/learn.html
|
| shows the site's intended formatting.
| cosmotic wrote:
| I must admit, I don't think I've ever seen centered code.
| jitl wrote:
| AppleScript is an amazing facility and undervalued strength of
| macOS. I've found it useful for automating content management
| workflows for people that prefer a GUI over a shell script. You
| can also use it directly from a shell script via `osascript -e
| "XXXX"` to show pop-up windows or take simple inputs.
|
| It's really a pity that AppleScript / Open Script Architecture
| doesn't have more documentation love, and that support in the
| macOS ecosystem for scripting is falling off as developers adopt
| web-focused or iOS-focused app architectures.
|
| AppleScript itself is a weird, slow language, but you can use
| JavaScript (executed by JavaScriptCore) instead with the same
| scripting libraries. In fact, there's a whole architecture to
| extend the macOS scripting system with different interpreters. On
| my system, the following are available: $
| osalang -l ascr appl cgxervdh AppleScript jscr
| appl cgxe-v-h JavaScript scpt appl cgxervdh Generic
| Scripting System
|
| But, there are constants defined in Swift for a bunch of
| scripting languages:
| https://developer.apple.com/documentation/uniformtypeidentif...
| pbreit wrote:
| More amazing and undervalued is Google Apps Script which allows
| for attaching JavaScript to Google Docs (most useful with
| Sheets). Including a "fetch(url)" function for calling APIs.
|
| https://developers.google.com/apps-script/reference/url-fetc...
| shp0ngle wrote:
| AppleScript is a HORRIBLE language. It still gives me
| nightmares, as I needed to do something non-trivial there.
| In... I think Tiger days? Brrr.
|
| I liked the idea that all apps will be easily scriptable - lots
| of apps back then even exported some AppleScript actions. But
| it never worked that well in reality.
|
| And nowadays, with all big and important apps being Electron,
| it doesn't really matter anyway.
|
| I never knew that it works with JavaScript.
| latexr wrote:
| > AppleScript is a HORRIBLE language.
|
| Agreed.
|
| > But it never worked that well in reality.
|
| Disagreed. As bad as the AppleScript _syntax_ is, it's the
| only way to automate certain things in macOS and it generally
| does it competently. Plus it has an Objective-C bridge,
| allowing you to run lower-level commands from a scripting
| language.1
|
| > with all big and important apps being Electron
|
| Chromium itself (and all major browsers derived from it) has
| a fantastic AppleScript dictionary which is even better than
| Safari's.2 Firefox is the single major browser without
| AppleScript support. Plenty of apps still include an
| AppleScript dictionary and Electron has an open issue for
| it.3
|
| > I never knew that it works with JavaScript.
|
| It was added in Yosemite.4 It's not perfect and has some bugs
| compared to AppleScript, but it also makes other things
| significantly easier or possible, such as dealing with JSON.
|
| 1 Technically one can use Swift as a scripting language too,
| but it requires installing the Developer Tools.
|
| 2 Quick example: one can't open a new private Safari window
| via AppleScript, except by scripting the GUI.
|
| 3 https://github.com/electron/electron/issues/4418
|
| 4 https://developer.apple.com/library/archive/releasenotes/In
| t...
| okennedy wrote:
| AppleScript's syntax was a mess of pseudo-english that was a
| pain to write (although considerably easier for newbies to
| read). The editor helped here quite a bit.
|
| That said, the infrastructure backing Applescript was amazing
| (especially for the time). The MacOS scripting interface was
| just as powerful as /proc, but far more structured. Every
| application and system service defined its own object-
| relational schema, and Applescript provided a SQL/Sparql-like
| language for querying that schema ("set foo to all of the
| Documents"). The schema and associated documentation were
| self-contained within the applications themselves via the
| resource fork, and Apple's HIGs set fairly high expectations
| for the quality of this documentation.
|
| And of course, as the GP notes, other languages could tie
| into the scripting framework (I have fond memories of using
| both Frontier and Hyperscript in this way).
| dmitriid wrote:
| > and Apple's HIGs set fairly high expectations for the
| quality of this documentation.
|
| Ah. And herein lies the problem with modern day Apple
| Someone wrote:
| "Every application and system service defined its own
| object-relational schema"
|
| On the minus side, every application and system service
| _had to implement_ its own object-relational schema, and
| there was little support for doing that.
|
| In theory, you could tell any text editor or text processor
| such things as
|
| _"tell every sentence whose third word is "foo" to set
| last word to first character"_
|
| and that entire code fragment would then run in the process
| of the editor, but in practice nobody implemented the
| support for such complex commands.
|
| That every application basically chose its own subset of
| the language is part of the reason AppleScript is so hard
| to write.
| scelerat wrote:
| It's been said that Perl is a write-once, read-never language.
| Applescript is the opposite: read-many, write-never.
| mlajtos wrote:
| As jitl mentioned, macOS can be scripted also with JS [1]. You
| can also create GUI in an imperative style. Or write a React
| reconciler and have something like React Native via JXA, without
| any ObjC bridge. Not that you should do it, but its possible.
|
| https://developer.apple.com/library/archive/releasenotes/Int...
| russellbeattie wrote:
| AppleScript pops up every once in a while on HN and the general
| consensus seems to have been: It can be powerful and in some
| instances it works well, but the syntax is insane, the JavaScript
| isn't documented, the edge cases are numerous, security updates
| and popups can be frustrating, it never really works as expected
| and takes you way longer to make it work than it should. It's a
| neglected part of the OS and given Apple's focus on consumers not
| developers, it's likely a dead-end feature not worth your time to
| learn.
|
| I use it for "launcher" apps that I can add to the Finder toolbar
| to do things like create a new text document or open the console
| in that directory [1]. I wrote that a decade ago, and little has
| changed since. You use it when you have to, but then once
| whatever itch is scratched, you'll avoid it like the plague.
|
| 1. https://www.russellbeattie.com/blog/fun-with-the-os-x-
| finder...
| ttepasse wrote:
| As usual a lot of discussions about AppleScript are about the
| english-like syntax, adopted for inexperienced users, less than
| the - in theory - amazing object based App scripting.
|
| Fun fact: Apple in 1991 in fact planned to have alternative
| dialects of AppleScript, meaning divorcing the logic of the
| script from its textual representation and storing the script in
| a language-independent representation.
|
| One application would have been internationalization, instead of
| writing english-like writing in french-like or japanese-like and
| early 90s Apple seem to have released those dialects for a little
| time, although they depended on localization of the AppleScript
| dictionaries of the scriptable apps.
|
| Also planned was a Professional Dialect, but sadly not released.
| According to William Cook, creator of AppleScript (the
| language/dialect) that would have look something like this:
|
| English-like dialect: the first character of
| every word whose style is bold
|
| French-like dialect: le premier caractere de tous
| les mots dont style est gras
|
| Professional dialect: { words | style == bold
| }.character[1]
|
| I would have really liked something like that. One wonders what
| would have been. William Cook himself believes in hindsight that
| AppleScript should have adopted the professional dialect.
|
| AppleScript from Mac OS X onwards had adopted the Open Script
| Architecture with bridges which can be awkward because of some
| mismatches between features and syntax of the bridging language.
| Even Javascript (JXA), the most prominent, needs a somewhat
| awkward `whose` function to replicate the builtin filtering of
| AppleScript.
|
| [1] Cook's paper about AppleScript:
| https://www.cs.utexas.edu/~wcook/Drafts/2006/ashopl.pdf
| vaxman wrote:
| Oh do Tell.. #roflmao
|
| Seriously, this is not The Way. It was The Way, but hasn't been
| for a long time (notice it was never included on iOS/iPadOS?)
| Sirened wrote:
| Apple Script is a weird but cool feature. There's also another
| thing called "Remote Apple Events" which essentially lets you use
| Apple Script over the internet via some weird RPC system. It's
| super bizarre and I have yet to find a legitimate use for it but
| it works nonetheless.
| cmg wrote:
| Remote Apple Events are pretty fantastic. Back in 2002/2003, I
| wrote an AppleScript Studio application (with a little bit of
| Objective-C) that let you control iTunes on another machine.
|
| It seemed like absolute magic at the time to me and my friends
| - from my PowerBook on the couch, we could change the song
| playing on the iMac on the other side of the room! Wish I still
| had that source code to look at.
| oneplane wrote:
| If I'm not mistaken, that is still used in some parts of the
| pre-Home Sharing remote Library and remote playback
| functionality in iTunes (pre-Music.. man, that's a lot of
| prerequisites).
|
| Remote events even worked across AppleTalk and TCP/IP, across
| macOS and Mac OS Classic, so you could make use of some of
| the features between computers on different networks, with
| 10+ years difference in software, and it would still
| 'magically' work.
|
| After RAOP and mDNS etc. the event system was less used and
| most of the newer features are implemented as classic client-
| server applications. But it still exists, and still works.
| Apple Events are even advertised over Bonjour!
| kalleboo wrote:
| Remote Apple Events were super useful back in the classic Mac
| days - it used it for instance to dial up or hang up the 56k
| modem on the Mac that was acting as our router. It also made it
| super easy to write networked games in HyperCard.
|
| I guess it lives on since enough people have some vital
| QuarkXPress workflow they can't abandon...
| yborg wrote:
| Hasn't Apple effectively deprecated all of the scripting
| interfaces? They fired the guy that was in charge of this stuff
| years ago, and the last updates to the JSX documentation are for
| El Capitan. Apps that didn't previously have scripting
| dictionaries no longer support them. I believe that the issue
| ultimately is that these scripting interfaces are a security
| issue since they allow malware to leverage a built-in general
| programmatic interface without having to load any additional
| tooling. I'm surprised that Apple hasn't disabled them already.
| chipotle_coyote wrote:
| Well, "effectively" in "effectively deprecated" is doing a lot
| of work there. :) I think one could make a plausible argument
| that the old Open Scripting Architecture is on its way out, but
| given that they've brought Shortcuts to the Mac, it's obvious
| that the idea of having "a built-in general programmatic
| interface" isn't being deprecated. Shortcuts will probably
| replace both Automator and OSA in the long run, but it's not
| going to get there for a few years.
| classified wrote:
| Worst possible formatting for code examples. Did the author even
| look at the rendered page?
___________________________________________________________________
(page generated 2022-09-06 23:01 UTC)