TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The unexpected return of JavaScript for automation

73 pointsby goranmoominover 3 years ago

12 comments

nishsover 3 years ago
Interestingly I learnt recently that macOS includes a ``jsc&#x27;&#x27; command, which provides a built-in JavaScript REPL and interpreter [0]. It is part of the JavaScriptCore framework. I can find it at:<p><pre><code> &#x2F;System&#x2F;Library&#x2F;Frameworks&#x2F;JavaScriptCore.framework&#x2F;Versions&#x2F;Current&#x2F;Helpers&#x2F;jsc </code></pre> But a Stack Overflow comment says it moves around between releases, so you may want to find it first using:<p><pre><code> $ find &#x2F;System&#x2F;Library&#x2F;Frameworks&#x2F;JavaScriptCore.framework -iname jsc </code></pre> It doesn&#x27;t support web-specific APIs such as window. But it includes extras such as a print function and a readline function.<p><pre><code> $ cat greet.js print(&quot;What is your name?&quot;) const name = readline() print(`Hello, ${name}!`) $ jsc greet.js What is your name? ns Hello, ns! $ </code></pre> Have fun :)<p>[0]: <a href="https:&#x2F;&#x2F;furbo.org&#x2F;2021&#x2F;08&#x2F;25&#x2F;jsc-my-new-best-friend&#x2F;" rel="nofollow">https:&#x2F;&#x2F;furbo.org&#x2F;2021&#x2F;08&#x2F;25&#x2F;jsc-my-new-best-friend&#x2F;</a>
评论 #29349935 未加载
评论 #29357077 未加载
lwouisover 3 years ago
macOS APIs have poor documentation overall, but JXA is on another level. It&#x27;s not even like Carbon APIs which are still necessary to use today, but which documentation was removed from Apple&#x27;s website. At least here, you can find an archive somewhere. No, JXA was simply never documented.<p>You basically have the AppleScript docs, which themselves are under-documented, and you have to guess what JXA APIs exist based off it. My recommendation is to use Safari and play with the <i>debugger</i> at runtime. That way you can test and see which APIs are available by trial and error. You can also hack around and display JS objects internal variables&#x2F;prototypes to get an idea of what things may be available.<p>I&#x27;ve had to dig deep into this for my FOSS app recently, and I&#x27;ll like to share these 2 gems of very-hard-to-find information:<p>- How AppleEvents (AppleScript or JXA triggered events) permission works (<a href="https:&#x2F;&#x2F;www.felix-schwarz.org&#x2F;blog&#x2F;2018&#x2F;08&#x2F;new-apple-event-apis-in-macos-mojave" rel="nofollow">https:&#x2F;&#x2F;www.felix-schwarz.org&#x2F;blog&#x2F;2018&#x2F;08&#x2F;new-apple-event-a...</a>)<p>- (Bitter) testimony from someone involved at JXA&#x27;s inception (<a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21803874" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=21803874</a>)<p>Hopefully this message saves someone hours of research~
wdavidwover 3 years ago
We have been deploying infrastructures composed of Hadoop and Kubernetes clusters in JavaScript for years with Nikita (<a href="https:&#x2F;&#x2F;nikita.js.org" rel="nofollow">https:&#x2F;&#x2F;nikita.js.org</a>) and the developer experience is great. We released patches and features very fast, writing unit tests is easy and the all process is very flexible. Most of our customers impose the usage of Ansible to accomplish similar tasks, and we lose the simplicity, control and agility. To achieve expressiveness similar to YAML, we use CoffeeScript which looks and feel like YAML with the benefit of a programming language. No need to work around templating limitations, not speaking about configuration management (it is so complex to work around it that we have an article just on this topic in the pipeline).
评论 #29350123 未加载
eins1234over 3 years ago
One really cool little JS library I&#x27;ve been using for a bunch of desktop automation tasks lately is nut.js and the lower level libnut library it&#x27;s implemented on top of:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;nut-tree&#x2F;nut.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nut-tree&#x2F;nut.js</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;nut-tree&#x2F;libnut" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nut-tree&#x2F;libnut</a><p>It provides a means to send user input (mouse movement&#x2F;clicks and key presses) and read and react to changes in visual state (through screenshots), and works across Windows, Linux and MacOS. It automates at a much lower level of abstraction than the approaches mentioned in the article that script against programmatic APIs.<p>What I really like about this lower level approach is that you don&#x27;t need anyone&#x27;s permission to automate anything, since there&#x27;s no programmatic API that the system owners has to provide for you and thus can limit or take away when it becomes inconvenient (or more likely they&#x27;d just never provide it in the first place).<p>Any task that can be accomplished though looking at stuff on the screen and clicking a mouse and pressing keys on a keyboard (i.e. what a real person would do to accomplish the same task) can be automated, and it&#x27;s actually surprisingly easy and effective to do this with nut.js. What really helps is that OpenCV has become ridiculously good and ridiculously fast at matching&#x2F;identifying objects from a screenshot, with latencies usually in the low double digit miliseconds, so latency-based flakiness isn&#x27;t nearly as much of an issue as I remember it being in the old days. I&#x27;ve also played around with OCR with tesseract but haven&#x27;t had as much success with it in terms of perf, and remember seeing latencies of several seconds for even recognizing a single word from a tiny pre-cropped screenshot containing only the word itself, so these days I end up just taking screenshots of text to identify with OpenCV instead.<p>The main tradeoff to this approach compared to automation through APIs is that because it works by simulating real user inputs, it&#x27;s not very amenable to running in the background while a user is actively interacting with the same machine, so a separate machine or VM is often needed. That&#x27;s an acceptable tradeoff for some use cases but complete deal breaker for others, so YMMV, but just wanted to bring this cool little tool to people&#x27;s attention.
评论 #29353660 未加载
aasasdover 3 years ago
I don&#x27;t know about PyObjC, but JXA is very awkward when coming from JS: it brings in conventions from Objective-C, and it&#x27;s not explained anywhere what patterns I&#x27;m supposed to use. E.g. with collections, it seems that instead of just values, you get some very lively views that change while you&#x27;re trying to use them and refuse to act the same on subsequent access—so the robust way to get values from a collection repeatedly is to obtain the collection each time anew (or convert it to a sane Array). You have to figure out by yourself how AppleScript is translated to JS—or, more realistically, by copying examples from the web. Either I didn&#x27;t find the proper documentation, or it&#x27;s pretty much nonexistent. As the article mentions, the Github repo is the primary knowledge compendium on how the hell one uses JXA.<p>It&#x27;s pretty much impossible to casually write JXA scripts now and then, just knowing JS and consulting reference docs—instead, it seems that you&#x27;ll have to learn arcane ObjC conventions from the 80s and keep translating those to JXA, without much help from Apple. Add to this that Swift&#x2F;ObjC docs themselves seem terse, done with a hefty bit of ‘fuck off’ attitude, and are more of ‘here&#x27;s what we have’ reference with too little of ‘here&#x27;s how to do the thing you want’. IIRC Swift docs just drop pages on old versions of classes and functions, so when Apple keeps rearranging things again in new versions, and you inevitably have old examples from the web, you have to grope for how each change works, instead of just opening the deprecated page and reading there that this new thing is now used.<p>So you have to translate JS ← JXA ← AppleScript ← Swift&#x2F;ObjC, with resistance on every step.<p>Oh, and the system settings? I haven&#x27;t seen comprehensive docs on them either. Just apple.stackexchange and a bunch of howto sites. I guess Apple prefers to provide living for these sites instead of spending some of the billions on a technical writer.
评论 #29350405 未加载
lewisjoeover 3 years ago
I believe this is just the start. The more businesses move to SaaS models, automation takes a different shape as well, i.e automation scripts involving business objects within&#x2F;outside the system. Most systems call them &quot;custom scripts&quot; - and the primary language of choice is Javascript. Why? Javascript engines have done a phenomenal job at sandboxing and extensions.<p>So that&#x27;d be another reason why JS will live long as a language of choice of automating cloud objects.
评论 #29348555 未加载
评论 #29348160 未加载
评论 #29350539 未加载
评论 #29349043 未加载
onion2kover 3 years ago
You do get a lot of unexpected returns in JS.
bryanrasmussenover 3 years ago
might as well throw jsdb into the mix here <a href="http:&#x2F;&#x2F;www.jsdb.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.jsdb.org&#x2F;</a>
manbartover 3 years ago
And Windows still comes with Jscript too.
评论 #29349143 未加载
flohofwoeover 3 years ago
Re: &quot;Monterey has deprecated the pre-installed python on macOS&quot;<p>Interestingly, there&#x27;s a &#x2F;usr&#x2F;bin&#x2F;python3 (3.8.9) on my new MBP which doesn&#x27;t have the Apple deprecation message when starting into the REPL. Not sure if it was preinstalled or Xcode put it there though.
评论 #29351333 未加载
tzsover 3 years ago
I&#x27;ve found JXA useful for simple web automation.<p>There is a site where I want to go to a few pages and extract some data. Their terms of service allow this as long as you do not access the site faster than a human browsing would do.<p>I used to do it with curl, with suitable delays between each page fetch.<p>Then they apparently got hit with some DOS attacks and started using Cloudflare protection and curl no longer worked.<p>I then switched to Selenium. That got stuck at hCaptcha, refusing to go on no matter how many times I correctly identified all the buses or trains or whatever.<p>Adding some experimental options in Selenium got around the hCaptcha loop:<p><pre><code> options.add_experimental_option(&quot;excludeSwitches&quot;, [&quot;enable-automation&quot;]) options.add_experimental_option(&#x27;useAutomationExtension&#x27;, False) options.add_argument(&quot;--disable-blink-features=AutomationControlled&quot;) </code></pre> A while later though the site started occasionally bringing up that Cloudflare &quot;Checking your browser before accessing &lt;site_name&gt;&quot; automatic check. That just sits there, occasionally reloading.<p>Same thing if I use undetected-chromedriver [1] which is supposed to be less likely to trigger anti-bot services.<p>Next up was using a bookmarklet. It was easy to make a bookmarklet that used an XMLHttpRequest to post the content of document.documentElement.innerHTML to a server of mine for processing. That bookmarklet could then advance to the next page of interest. It wouldn&#x27;t be as nice as the prior solutions because I&#x27;d have to click the bookmarket for every page, but that wouldn&#x27;t be too bad.<p>The bigger annoyance there is if my server isn&#x27;t HTTPS the XMLHttpRequest is blocked because of mixed content. In Chrome you can tell it to ignore that on a per site basis, but it isn&#x27;t persistent across browser launches. There are also issues if the server is on my LAN, which runs into the &quot;not let JavaScript on a page from the internet access the LAN because it might be trying to hack your IoT&quot; stuff.<p>I was just starting to look into browser extensions to see if that would be a viable approach. I don&#x27;t know if (1) an extension can save to the local filesystem (which I&#x27;d prefer instead of having to do that XMLHttpRequest runaround), or (2) if an extension can carry out actions on multiple pages without user intervention (if I still would have to do the click per page I can just stick with the bookmarklet approach).<p>I was even contemplating trying to make a fork of Chromium or Firefox with built-in support for this stuff.<p>Then I remembered AppleScript, and that things that support AppleScript can also be controlled with JavaScript which is much much much less weird than AppleScript.<p>Here&#x27;s some JXA code that saves current tab&#x27;s source from Safari:<p><pre><code> var cur = Application.currentApplication() cur.includeStandardAdditions = true; var app = Application(&#x27;Safari&#x27;); app.includeStandardAdditions = true; function save_page(file_name) { let source = app.windows[0].currentTab.source() let path = Path(file_name) let f = cur.openForAccess(path, {writePermission: true}) cur.setEof(f, {to: 0}) cur.write(source, {to: f, startingAt: 0}) cur.closeAccess(f) } save_page(&quot;&#x2F;tmp&#x2F;page.html&quot;) </code></pre> Here are the changes to do that for Chrome (sort of...it doesn&#x27;t quite get the full source):<p><pre><code> 3c3 &lt; var app = Application(&#x27;Safari&#x27;); --- &gt; var app = Application(&#x27;Google Chrome&#x27;); 8c8,9 &lt; let source = app.windows[0].currentTab.source() --- &gt; let ct = app.windows[0].activeTab &gt; let source = app.execute(ct, {javascript: &quot;document.documentElement.innerHTML&quot;}) </code></pre> Changing pages is also easy. For Safari,<p><pre><code> app.windows[0].currentTab.url = &quot;...&quot; </code></pre> I don&#x27;t remember what it is for Chrome, but it is similarly easy. And on both there is also the option of injecting JavaScript onto the page to do the navigation, if for some reason just setting the URL isn&#x27;t good enough.<p>(No Firefox examples because Firefox has absolutely abysmal AppleScript support).<p>With JXA then I can write a handful of short command line utilities that can get the current URL, get the current page content, and navigate to a given URL and I&#x27;ve got everything I need.<p>(There might be a way to sort of make it work with Firefox. I believe (but am not certain) that with JXA I can tell the system send mouse-click events to anywhere I want in an application window. Firefox does provide enough AppleScript support to get the title of the current page. So maybe the bookmarklet approach, with JXA watching for the page changes and clicking the bookmarklet, would work).<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;ultrafunkamsterdam&#x2F;undetected-chromedriver" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ultrafunkamsterdam&#x2F;undetected-chromedrive...</a>
评论 #29348290 未加载
a-dubover 3 years ago
<i>sigh</i>