Further Adventures with Twitter, node-red and Sonic Pi

Building upon the methods explored here, I’ve been experimenting with using node-red’s Twitter node to interface directly with ruby files in Sonic Pi by means of shell scripting. By creating multiple instances of the Twitter nodes in a single flow, I can set unique search terms, strings, hash tags, etc per node, which correspond to individual ruby files. These files are then called up in Sonic

Pi, via the command line interface sonic-pi-cli. Using AppleScript syntax within a .sh file, we have the following:

#! /usr/bin/env osascript 
tell application "System Events" 
if "Terminal" is not in name of processes then 
launch application "Terminal" 

tell process "Terminal" 
set frontmost to true 
if (count windows) is 1 then 
keystroke "n" using command down 
if (count windows) is 2 then 
end if 
end tell 
end tell 

tell application "Terminal" 
do script "`sonic_pi \"run_file '~/Dropbox/Code/Ruby/SonicPi/multiRandom.rb'\"`" in window 1 
end tell

Instead of connecting Twitter nodes to OSC nodes (to communicate with QLab), I am using exec nodes to run the shell scripts. When configuring the exec nodes, the Command is ‘exec’, uncheck the option to ‘append msg.payload’, and in the ‘extra input parameters’ enter the file pathway to your .sh file, including the extension. Make sure your .sh files are executable once you create them. And, obviously, you will need to set the file pathway to a location pertinent to your machine.

The json file of the Node-RED flow is:

[
    {
        "id": "68689119.e0bbb8",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": ""
    },
    {
        "id": "fad8a911.f12618",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "README - Runner shell file formatting",
        "info": "#!/usr/bin/env osascript\n\ntell application \"System Events\" \n  if \"Terminal\" is not in name of processes then launch application \"Terminal\" \n    tell process \"Terminal\" \n    set frontmost to true \n      if (count windows) is 1 then keystroke \"n\" using command down \n      if (count windows) is 2 then \n    end if \n  end tell \nend tell\n\ntell application \"Terminal\" \n  do script \"`sonic_pi \\\"run_file '~/Dropbox/Code/Ruby/SonicPi/multiRandom.rb'\\\"`\" in window 1\nend tell",
        "x": 170,
        "y": 680,
        "wires": [],
        "icon": "node-red/alert.png"
    },
    {
        "id": "5851ef74.e5d77",
        "type": "exec",
        "z": "68689119.e0bbb8",
        "command": "exec",
        "addpay": false,
        "append": "/Users/HoganSound/Dropbox/Code/Ruby/SonicPi/Launchers/runner.sh",
        "useSpawn": "false",
        "timer": "",
        "oldrc": false,
        "name": "runner.sh",
        "x": 700,
        "y": 40,
        "wires": [
            [
                "2f2edc04.2e97c4"
            ],
            [
                "2f2edc04.2e97c4"
            ],
            []
        ]
    },
    {
        "id": "d5dc63ed.bc90d",
        "type": "twitter in",
        "z": "68689119.e0bbb8",
        "twitter": "",
        "tags": "",
        "user": "false",
        "name": "Twitter watcher",
        "topic": "tweets",
        "inputs": 1,
        "x": 100,
        "y": 180,
        "wires": [
            [
                "5851ef74.e5d77",
                "2f2edc04.2e97c4"
            ]
        ],
        "outputLabels": [
            "trigger"
        ]
    },
    {
        "id": "86176a2d.e35d8",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "Sh launcher",
        "info": "",
        "x": 710,
        "y": 80,
        "wires": []
    },
    {
        "id": "a6ae1f1c.89795",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "Enter search terms",
        "info": "",
        "x": 110,
        "y": 240,
        "wires": []
    },
    {
        "id": "79ab5e06.8960f8",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "Duplicate the Twitter - exec node series above for additional search terms and shell script launchers",
        "info": "",
        "x": 360,
        "y": 540,
        "wires": []
    },
    {
        "id": "2f2edc04.2e97c4",
        "type": "debug",
        "z": "68689119.e0bbb8",
        "name": "CONSOLE",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "x": 990,
        "y": 140,
        "wires": []
    },
    {
        "id": "b294a92b.b6ce7",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "README - Exec node formatting",
        "info": "Running .sh files from node-red exec node:\nCommand = 'exec'\nadditional info = /path/to/your/fileAndExtension\nOutput = when the command is complete - exec mode",
        "x": 150,
        "y": 640,
        "wires": [],
        "icon": "node-red/alert.png"
    },
    {
        "id": "ca23c478.12907",
        "type": "exec",
        "z": "68689119.e0bbb8",
        "command": "exec",
        "addpay": false,
        "append": "/Users/HoganSound/Dropbox/Code/Ruby/SonicPi/Launchers/stopper.sh",
        "useSpawn": "false",
        "timer": "",
        "oldrc": false,
        "name": "stopper.sh",
        "x": 710,
        "y": 260,
        "wires": [
            [
                "2f2edc04.2e97c4"
            ],
            [
                "2f2edc04.2e97c4"
            ],
            []
        ]
    },
    {
        "id": "97adfb95.66f6a",
        "type": "inject",
        "z": "68689119.e0bbb8",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 100,
        "y": 440,
        "wires": [
            [
                "ca23c478.12907"
            ]
        ]
    },
    {
        "id": "8ed5f43d.cb004",
        "type": "inject",
        "z": "68689119.e0bbb8",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 100,
        "y": 40,
        "wires": [
            [
                "5851ef74.e5d77"
            ]
        ]
    },
    {
        "id": "891ccced.59c548",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "Manual stop",
        "info": "",
        "x": 90,
        "y": 480,
        "wires": []
    },
    {
        "id": "9dfacc49.732b5",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "Manual start",
        "info": "",
        "x": 90,
        "y": 80,
        "wires": []
    },
    {
        "id": "3054b841.f3e56",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "Valid terms will trigger shell script and print to debug console",
        "info": "",
        "x": 240,
        "y": 280,
        "wires": []
    },
    {
        "id": "4b60587e.3bed28",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "README - Stopper shell file formatting",
        "info": "#! /usr/bin/env osascript\n\ntell application \"System Events\"\n\tif \"Terminal\" is not in name of processes then launch application \"Terminal\"\n\ttell process \"Terminal\"\n\t\tset frontmost to true\n\t\tif (count windows) is 1 then keystroke \"n\" using command down\n\t\tif (count windows) is 2 then\n\t\tend if\n\tend tell\n\t\nend tell\n\ntell application \"Terminal\"\n\tdo script \"`sonic_pi \\\"stop\\\"`\" in window 1\nend tell",
        "x": 170,
        "y": 720,
        "wires": [],
        "icon": "node-red/alert.png"
    },
    {
        "id": "34460a5f.27c886",
        "type": "comment",
        "z": "68689119.e0bbb8",
        "name": "Stop playback",
        "info": "",
        "x": 710,
        "y": 300,
        "wires": []
    }
]

And the stopper shell is:

#! /usr/bin/env osascript 
# AppleScript
# halt playback 

tell application "System Events" 
if "Terminal" is not in name of processes then 
launch application "Terminal" 
tell process "Terminal" 
set frontmost to true 
if (count windows) is 1 then 
keystroke "n" using command down 
if (count windows) is 2 then 
end if 
end tell 
end tell 

tell application "Terminal" 
do script "`sonic_pi \"stop\"`" in window 1 
end tell

The whole thing should look like this:

This method uses only freely available tools, rather than paid commercial software – which is not to say you shouldn’t use QLab if you want, but Script cues are a paid feature.

Share:

Author: kreivalabs