npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

osascript

v1.2.0

Published

A stream for controlling Macs through AppleScript or JavaScript.

Downloads

432

Readme

osascript

Node.js module for doing Apple Open Scripting Architecture (OSA) in JavaScript or AppleScript.

Install

npm install --save osascript

Requirements

For using JavaScript as Automation language, Mac OS X Yosemite is required. On versions before that, you can pass AppleScripts.

Usage

By default, if no other type is defined and the passed file is not a AppleScript file (with extensions .scpt or .applescript), JavaScript is used.

See the last example for overriding this behaviour and passing on AppleScript instead. All API's are the same.

Default stream

var osascript = require('osascript');
var fs = require('fs');

// Run JavaScript file through OSA
fs.createReadStream('someFile.js')
  .pipe(osascript())
  .pipe(process.stdout);

This will pipe the data from someFile.js to the Apple Open Scripting Architecture (OSA) and print the result to the standard output.

You can also do this in a short-hand:

// note the file method after require ¬
var osascript = require('osascript').file;

// Run JavaScript file through OSA
osascript('someFile.js').pipe(process.stdout);

Or if you only have a string, you can do that as well

// note the eval method after require ¬
var osascript = require('osascript').eval;

// Run JavaScript text through OSA
osascript('console.log("Hello, world!");').pipe(process.stdout);

Using a callback

If you don't want to use a stream (just get the buffered output) you can do this on the file and eval methods by passing a function as the last argument:

// note the file method after require ¬
var osascript = require('osascript').file;

// Run JavaScript file through OSA
osascript('someFile.js', function (err, data) {
  console.log(err, data);
});
// note the eval method after require ¬
var osascript = require('osascript').eval;

// Run JavaScript text through OSA
osascript('console.log("Hello, world!");', function (err, data) {
  console.log(err, data);
});

Using AppleScript

As JavaScript as OSA isn't working on versions before Yosemite, we can use AppleScript as well. JavaScript is the default to try to encourage JS instead of AppleScript. When a filename is passed, AppleScript will be used if the filename has an AppleScript extension (.scpt or .applescript).

var osascript = require('osascript');
var fs = require('fs');

// Run JavaScript file through OSA
fs.createReadStream('someAppleScript.applescript')
  // Need to override options to define AppleScript
  .pipe(osascript({ type: 'AppleScript' }))
  .pipe(process.stdout);
// note the file method after require ¬
var osascript = require('osascript').file;

// No need to pass options, as it can be deduced from filename.
osascript('someFile.applescript', function (err, data) {
  console.log(err, data);
});

See more examples.

API

API from base function required in this way:

var osascript = require('osascript');

All endpoints uses options:

var defaultOptions = {
  type: 'JavaScript',
  args: [] // List of string arguments
};

Type is passed as language (option -l) to osascript. Can be either JavaScript (in Yosemite) or AppleScript.

flags can be used to change the output style of return values from functions executed by osascript:

// JSON parsable return
osascript('(function(){return ["foo",5, {foo: "barz"}]})()', {flags: ['-s', 's']}, function (data) {
  console.log(data); // ["foo", 5, {"foo":"barz"}]
});

args is a list of strings passed in as arguments to your scripts. In JavaScript you can access them using:

ObjC.import('Cocoa');
var args = ObjC.deepUnwrap($.NSProcessInfo.processInfo.arguments).slice(4);

.slice(4) is to cut away program name and language argument. In addition, node-osascript has to put in a - to indicate that the following list of strings should be passed as arguments. This - is on index 3.

osascript([options: Object])

Creates a PassThrough stream that can get piped text manually (text or files).

osascript.file(file[, options: Object, callback:function (err, data)])

See options as defined above.

If callback function is passed, the buffered output from the OSA is passed as data (initiates the data immediately)

osascript.eval(scriptText[, options: Object, callback:function (err, data)])

scriptText is script in the language type as defined.

See options as defined above.

If callback function is passed, the buffered output from the OSA is passed as data (initiates the data immediately)

TODO

  • [ ] Tests
  • [x] Error handling

License

MIT License