Git Product home page Git Product logo

osa2's Introduction

osa2

styled with prettier

Interact with Apple's Open Scripting Architecture from node.js

This module provides a Promise interface to macOS's automation and scripting APIs.

osa2 is intended for use in libraries or full applications. If you're looking to write simple automation scripts, check out jxa.

Installation

npm install osa2 --save

Usage

Get the currently playing iTunes track

var osa = require('osa2')

var track = osa(() => {
    return Application('iTunes').currentTrack.name()
})

track().then(console.log).catch(console.error)

Show an alert

var osa = require('osa2')

function alert(message) {
    return osa(text => {
        var app = Application.currentApplication()
        app.includeStandardAdditions = true
        app.displayAlert(text)
    })(message)
}

alert('Hello World')

API

Execute code in the JavaScript for Automation environment

osa(function) -> function

Wraps function to be run inside Apple's JavaScript for Automation environment.

function

Type: function

The code to be run inside the JXA environment. NOTE: function cannot close over variables in a parent's scope. Pass data as arguments explicitly instead.

return

Type: function

Returns a new function with the same arguments as function, but when called the code is run inside the JXA environment. This is done asynchronously, therefore promise is returned.

var greet = osa(name => `Hello from JXA, ${name}!`)

greet('Will').then(text => {
    console.log(text) // Hello from JXA, Will!
})

osa2's People

Contributors

wtfaremyinitials avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

osa2's Issues

Unable to install - error thrown

Here's the output when I try to install today:

gregorythorson@Gregorys-MacBook-Pro itunesjs % npm install osa2
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
npm WARN deprecated [email protected]: fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.
npm WARN deprecated [email protected]: This module relies on Node.js's internals and will break at some point. Do not use it, and update to [email protected].
npm WARN deprecated [email protected]: flatten is deprecated in favor of utility frameworks such as lodash.
npm WARN deprecated [email protected]: request-promise-native has been deprecated because it extends the now deprecated request package, see request/request#3142
npm WARN deprecated [email protected]: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated [email protected]: Legacy versions of mkdirp are no longer supported. Please update to mkdirp 1.x. (Note that the API surface has changed to use Promises in 1.x.)
npm WARN deprecated [email protected]: this library is no longer supported
npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated [email protected]: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated [email protected]: please upgrade to graceful-fs 4 for compatibility with current and future versions of Node.js
npm WARN deprecated [email protected]: use String.prototype.padStart()
npm WARN deprecated [email protected]: CircularJSON is in maintenance only, flatted is its successor.
npm WARN deprecated [email protected]: gulp-util is deprecated - replace it, following the guidelines at https://medium.com/gulpjs/gulp-util-ca3b1f9f9ac5
npm WARN deprecated [email protected]: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (debug-js/debug#797)
npm WARN deprecated [email protected]: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (debug-js/debug#797)
npm WARN deprecated [email protected]: some dependency vulnerabilities fixed, support for node < 10 dropped, and newer ECMAScript syntax/features added
npm WARN deprecated [email protected]: Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
npm WARN deprecated [email protected]: Please update to minimatch 3.0.2 or higher to avoid a RegExp DoS issue
npm WARN deprecated [email protected]: Debug versions >=3.2.0 <3.2.7 || >=4 <4.3.1 have a low-severity ReDos regression when used in a Node.js environment. It is recommended you upgrade to 3.2.7 or 4.3.1. (debug-js/debug#797)
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.
npm WARN deprecated [email protected]: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See https://v8.dev/blog/math-random for details.
npm WARN deprecated [email protected]: request has been deprecated, see request/request#3142
npm WARN deprecated [email protected]: support for ECMAScript is superseded by uglify-js as of v3.13.0
npm WARN deprecated [email protected]: core-js@<3.3 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
npm ERR! code 1
npm ERR! path /Users/gregorythorson/git/itunesjs/node_modules/node-expat
npm ERR! command failed
npm ERR! command sh -c node-gyp rebuild
npm ERR! CC(target) Release/obj.target/expat/deps/libexpat/lib/xmlparse.o
npm ERR! CC(target) Release/obj.target/expat/deps/libexpat/lib/xmltok.o
npm ERR! CC(target) Release/obj.target/expat/deps/libexpat/lib/xmlrole.o
npm ERR! LIBTOOL-STATIC Release/libexpat.a
npm ERR! CXX(target) Release/obj.target/node_expat/node-expat.o
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | darwin | arm64
npm ERR! gyp info find Python using Python version 3.8.9 found at "/Library/Developer/CommandLineTools/usr/bin/python3"
npm ERR! gyp info spawn /Library/Developer/CommandLineTools/usr/bin/python3
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args '/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args 'binding.gyp',
npm ERR! gyp info spawn args '-f',
npm ERR! gyp info spawn args 'make',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args '/Users/gregorythorson/git/itunesjs/node_modules/node-expat/build/config.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args '/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args '/Users/gregorythorson/Library/Caches/node-gyp/17.0.1/include/node/common.gypi',
npm ERR! gyp info spawn args '-Dlibrary=shared_library',
npm ERR! gyp info spawn args '-Dvisibility=default',
npm ERR! gyp info spawn args '-Dnode_root_dir=/Users/gregorythorson/Library/Caches/node-gyp/17.0.1',
npm ERR! gyp info spawn args '-Dnode_gyp_dir=/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp',
npm ERR! gyp info spawn args '-Dnode_lib_file=/Users/gregorythorson/Library/Caches/node-gyp/17.0.1/<(target_arch)/node.lib',
npm ERR! gyp info spawn args '-Dmodule_root_dir=/Users/gregorythorson/git/itunesjs/node_modules/node-expat',
npm ERR! gyp info spawn args '-Dnode_engine=v8',
npm ERR! gyp info spawn args '--depth=.',
npm ERR! gyp info spawn args '--no-parallel',
npm ERR! gyp info spawn args '--generator-output',
npm ERR! gyp info spawn args 'build',
npm ERR! gyp info spawn args '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! gyp info spawn make
npm ERR! gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
npm ERR! In file included from ../node-expat.cc:1:
npm ERR! In file included from ../../nan/nan.h:2884:
npm ERR! ../../nan/nan_typedarray_contents.h:34:43: error: no member named 'GetContents' in 'v8::ArrayBuffer'
npm ERR! data = static_cast<char*>(buffer->GetContents().Data()) + byte_offset;
npm ERR! ~~~~~~~~^
npm ERR! 1 error generated.
npm ERR! make: *** [Release/obj.target/node_expat/node-expat.o] Error 1
npm ERR! gyp ERR! build error
npm ERR! gyp ERR! stack Error: make failed with exit code: 2
npm ERR! gyp ERR! stack at ChildProcess.onExit (/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:390:28)
npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
npm ERR! gyp ERR! System Darwin 21.0.1
npm ERR! gyp ERR! command "/opt/homebrew/Cellar/node/17.0.1/bin/node" "/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd /Users/gregorythorson/git/itunesjs/node_modules/node-expat
npm ERR! gyp ERR! node -v v17.0.1
npm ERR! gyp ERR! node-gyp -v v8.2.0
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/gregorythorson/.npm/_logs/2021-11-08T03_59_00_628Z-debug.log

bug with babel-preset-env only in a production built electron app

I found this PR #1 caused serious issues in running osa2 in a built electron app using hardened runtime. When running in dev-mode no issues caused, so a gnarly bug to find.

Specifically, it caused the bug:
Error: Couldn't find preset "env" relative to directory "/"

And this happened every time I called osa2. To fix it, I reverted back to 0.2.2 and did a clean reinstall. I suspect this is a problem with the interaction with the old babel-core and babel-preset env. The newest versions are @babel/core and @babel/preset-env if anyone wants this functionality - I personally don't need it or the overhead!

Given how straightforward the use-case is for this library, it might be a very simple clean upgrade to the new babel and someone could find it worthwhile to drop it in.

But I know nothing about babel and am deep in a pre-launch marathon so won't be doing this until/unless I ever need it later!

To replicate: execute osa2 in an Electron app built with electron-builder and notarised with electron-builder-notarize, using hardened-runtime. (don't know if all these steps are needed, but that was my situation)

stdlib not found on macOS Mojave Beta (18A293u)

execution error: Error on line 2: Error: nothing found to import (-2700)

Possible workaround:

ObjC.import('Foundation')
var fn   = (${fn.toString()})
var args = JSON.parse(ObjC.unwrap($.NSProcessInfo.processInfo.environment.objectForKey("OSA_ARGS")))
var out  = fn.apply(null, args)
JSON.stringify(out)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.