apple-java-script

1.0.1 • Public • Published

AppleJavaScript

Installation

npm install apple-java-script

Usage

Use AppleJavaScript to run AppleScript in JavaScript language straight from node.js.

All you have to do is to directly call it. And it will try to parse the rutruning result.

  // Getting iTunes Playlists
 
  var ajs = require('apple-java-script');
 
  // Return iTunes Playlists
  var playlists = ajs(function() {
    var iTunes = Application('iTunes');
    return iTunes.playlists.name();
  });
 
  console.log(playlists)
  // =>
  //  ['Library', 'Music', 'Music Videos', 'Movies', 'Home Videos', 'TV Shows', 'Podcasts', 'iTunes U', 'Audiobooks', 'Books', 'PDFs', 'Audiobooks', 'Genius']

Passing variables

Behind the scenes, AppleJavaScript converts function to the text and runs it with shell command osascript. So the function itself actually doesn't have acces to outside scope. And neither of global or outside variables are accessible from the function. To pass vars to it you have to pass them to function as an argument.

  // Showing modal and passing message from outer script
 
  var ajs = require('apple-java-script');
 
  var message = 'Hello';
 
  try {
    ajs(message, function(message) {
      var app = Application.currentApplication();
      app.includeStandardAdditions = true;
      return app.displayDialog(message);
    });
 
    console.log('You pushed Ok');
  } catch (e) {
    console.log('You pushed Cancel');
  }

AppleJavaScript direct call could be dangerous because it uses eval behind the scenes to parse returned AppleScript value. So you better know what you are doing all inputs. If you are not that sure you can use AppleJavaScript.runSafe. It works the same way, but doesn't try to parse returning value. So you'll have to do it yourself.

  var ajs = require('apple-java-script');
 
  var message = 'Hello';
 
  var fn = function(message) {
    let app = Application.currentApplication();
    app.includeStandardAdditions = true;
    return app.doShellScript('echo Babylon');
  }
 
  ajs.runSafe(message, fn);
  // =>
  //  '"Babylon"'
  ajs(message, fn);
  // =>
  //  'Babylon'

Readme

Keywords

none

Package Sidebar

Install

npm i apple-java-script

Weekly Downloads

2

Version

1.0.1

License

MIT

Last publish

Collaborators

  • welldan97