Controlling AppleScript From Spark.io With Node.js

by BenjieGillam in Circuits > Remote Control

1553 Views, 11 Favorites, 0 Comments

Controlling AppleScript From Spark.io With Node.js

Spark.io / Node.js / AppleScript

In this Instructable we learn to run arbitrary AppleScript commands via Node.js triggered from a Spark.io board over WiFi.

In the video you can see I'm using the buttons when pressed individually to emulate the arrow keys, and when pressed all together to simulate a Command-Tab (application switch) command.

You will need:

  1. A recent-ish OS X Machine
  2. Node.js installed
  3. A Spark Core (and internet button)

Connect the Spark Core to Your WiFi

2014 22:10.jpg

Register an account on spark.io if you haven't already.

Plug the Spark Core into a USB power supply (no data is necessary, just power) - the LED should be flashing blue.

Use the Android or iOS app "Spark Core" to connect to your Spark Core device. It took two attempts for it to work for me, but you will know it is working when the Spark Core's led is pulsing cyan.

Triggering Events From Spark Core

Screen Shot 2014-11-23 at 22.03.00.png

The buttons on the "internet button" Spark Core shield are hooked up to D1-D4. You can upload this sketch from GitHub via the spark.io website, and then flash it to your board with the lightning bolt (top left). This will have the Spark Core monitor the buttons and emit events when one or all buttons are released after being depressed. (Pressing just 2 or 3 buttons and then releasing will not trigger an event, this is deliberate.)

To see the sketch is working you should see the light nearest the depressed button light up when you press it.

Receiving Events in Node.js and Firing Apple Script

Screen Shot 2014-11-23 at 22.07.46.png

Clone this repository from GitHub, and then open the folder in a Terminal and run "npm install". Once done you can run:

SPARK_USERNAME=YourUsername SPARK_PASSWORD=YourPassword node index.js

This will then track down your Spark Core device and listen to the button release events from it. The script is preprogrammed to send the up/down/left/right commands for the relevant buttons and Command-Tab for all buttons being depressed, but you can easily replace the AppleScript that runs with anything you like.

Here's some ideas:

  • tell application "Skype" to send command "SET USERSTATUS ONLINE" script name "Blah"
  • tell application "Skype" to send command "SET USERSTATUS AWAY" script name "Blah"
  • tell application "Finder" to do shell script "afplay '/System/Library/Sounds/Blow.aiff'

  • tell application "Finder" to sleep