1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

JavaScript for Automation

Discussion in 'iTunes & AppleScripts' started by UKenGB, Jan 23, 2017.

  1. UKenGB

    UKenGB Member

    I know, not AppleScript, but likely to become the more important MacOS scripting language in the future.

    I'm looking into interacting with iTunes using JavaScript. Anyone already looked at this and have any suggestions regarding accessing 'selection', 'playlists' etc?

    I'm reading up on all the docs I can find but most js stuff relates to web development. Not so easy to find info on this newer usage and in particular how it can be used to deal with iTunes.
     
  2. UKenGB

    UKenGB Member

    Having been working on this for a while now, I have to say I'm enjoying using JavaScript more than I ever did with AppleScript. I believe that trying to make a program language more 'English like' is the wrong paradigm and simply makes it harder to figure out what to do. A programming language should be concise and exact so for each required action there is a clear method to use, without having to figure out each time if you should or could be using 'to' or 'on' or 'with' or 'considering' etc. You can probably tell, I've never liked AppleScript. It's a dreadful solution to automation/scripting on the Mac.

    Apart from that, controlling iTunes with JavaScript is pretty straightforward, but the big problem is Script Editor. This always behaved impeccably with AppleScript, nicely 'formatting' the text and prompting you for almost all errors when 'compiling', before you run it. However, when working with JavaScript, it's a different ball game. 'Compiling' does not correctly indent all text and misses many, many minor errors so you only find them when you try to run the script. Also, a bigger problem is that when using JavaScript, Script Editor is woefully unstable.

    A problem I find with JavaScript is forgetting to add () to the end of some objects. This will no doubt improve as I get back into the JavaScript groove and should be picked up as a simple 'compile' error. Instead, it is missed until you run the script, at which time... boom. All gone. Script Editor just crashes instantaneously without so much as a by your leave and it may or may not have saved your precious code since you last manually saved it. You then have to restart it and you may also have to manually open the script you were working on since this may not be automatically opened again for you. Certainly it will NOT remember in which Space the various windows were open and they all just get opened in the current Space. Not life threatening, but very irritating when Script Editor keeps crashing while you try to figure out what minor indiscretion you have committed with your code.

    This often seems to occur when using console.log to record what's occurring as you construct your script. That seems to be the main culprit for crashing like this due to missing ().

    Even when you are being oh so very careful about your code when using console.log and avoiding crashing Script Editor, it may after a while simply throw itself into a black hole of a loop that never ends. Looping through an iTunes playlist and just grabbing some data to display in the log, until I get it right, happily logs the chosen data for each of the several hundred items in the playlist. Then you make a minor change, run it again and 'Running...' shows at the bottom of the window, but nothing gets logged and in fact, it never stops. This is not the faulty logic of a never ending loop since the loop logic has not changed since the last time it ran perfectly and anyway if that were the case, it would be possible to Stop it with the button or keyboard command. Indeed, after you 'Force Quit' Script Editor (the ONLY way out of this) and restart it, find and re-open the script (Ggrrr.) and simply run it again, it will work as it should. Since the code has NOT been changed since it stuck itself in an interminable loop of its own doing, this is obviously a Script Editor bug.

    Less serious and maybe intentional, even if a bit odd, is when using an external library of functions. Run the script that calls a library function and log the result. Change the library function to output different data, save it and run the main script again and... you will get the exact same result as before you changed the library function. Make a minor change to the calling process in the main script (I modified the text string I was sending to the library function for the test) and you now get the result of the actual current library function and not the last one before you edited it. So in this situation Script Editor (or the core JavaScript engine) is somehow caching the library function and this is NOT updated when you actually edit the library itself and save it. So is this a bug or intentional? Well if the latter, then they need to change this as it is VERY confusing until you figure out what's going on.

    Apart from these problems of Script Editor being so flaky, I'd say JavaScript on the Mac is a real improvement over AppleScript. Maybe we should have a new forum section for this, to assist others wishing to avail themselves of what, in my opinion, is a superior way to control iTunes. Kirk?
     
  3. UKenGB

    UKenGB Member

    Talking to myself again. Isn't anyone else using these technologies with iTunes?

    I now have what I need in JavaScript that iterates through about a dozen playlists, checks if folders need creating, modifying for each track etc. It works and does what I need, but the problem is it just gets slower as it runs. And that's if I can actually get it to run as about 50% of the time it sends itself into an interminable loop when I try to run it in the Script Editor, which cannot be a problem with my code as when I restart SE it will actually run. Programming environments should NOT randomly misbehave like this. Apple should be better than this. Anyway...

    I wrote some fancy logging to see what's going on and when the script first runs, it processes about 40 tracks per second. This decreases for each playlist it processes until it's down to about 4 tracks per second = and then it starts the biggest playlist of all :-(

    So I'm wondering, WTF is going on here. It makes me think it's most likely some memory problem. A portion of memory used for AppleEvents is getting filled up? Just a guess, but would make sense. However is it a problem with iTunes, the JavaScript core engine, the JXA 'bridge' or the underlying AppleEvents system built-in to MacOS? Anyone here got any thoughts on this?
     
  4. kirk

    kirk Administrator Staff Member

    No, I have neither the experience nor ability to code like this. And talking with Doug Adams recently, he didn't feel that JavaScript was worth spending much time on.

    Do you have an Apple developer account? If so, you might want to ask this sort of question on their forums.
     
  5. UKenGB

    UKenGB Member

    I think Doug may be right, in the short term. I think it is a better basis than AppleScript which I find too vague and verbose, but Apple's current implementation of JXA is not really of an acceptable standard. I just found a simple maths calculation get's it a bit wrong. Not a lot wrong, but that's not really the point. And Script Editor is woefully lacking when it comes to JavaScript. They don't even bother to change the contextual menu so e.g. commenting a block of JavaScript code still uses AppleScript comments which of course are not the same and so don't work. Then there's the constant hanging and crashing and simply getting itself stuck before actually executing any of your code. The scant regard Apple have paid to update their product when introducing JXA is scandalous.

    I still think it's a better way forward than AppleScript, but right now, I wouldn't recommend trying to use it.

    So I think it's back to AppleScript for now, for small stuff and get to grips with Python for heftier programming tasks that don't need to interact with other apps, which to be honest, is most of what I want to do and both Apple and Java Scripts' reliance on using other apps to do everything just gets in the way.
     

Share This Page