Introduction: Controlling AppleScript From Spark.io With Node.js
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:
- A recent-ish OS X Machine
- Node.js installed
- A Spark Core (and internet button)
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Connect the Spark Core to Your WiFi
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.
Step 2: Triggering Events From Spark Core
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.
Step 3: Receiving Events in Node.js and Firing Apple Script
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