Skip to content

Instantly share code, notes, and snippets.

@andrewodri
Created December 17, 2021 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewodri/67176ead708af7b254fbd883ba5fd06e to your computer and use it in GitHub Desktop.
Save andrewodri/67176ead708af7b254fbd883ba5fd06e to your computer and use it in GitHub Desktop.
Roku TV power on/off via macOS shortcuts

Follow the steps below for each script: (i.e. Power on, and power off)

  1. Open Automator
  2. Create a new Quick Action
  3. Add "Run Shell Script" to the workflow
  4. Select "no input" in "any application"
  5. Paste one of the scripts below (minus the shebang line) into the shell script window
  6. Set DEVICE_HOSTNAME to the value of the Roku TVs hostname
  7. Select "/bin/sh" as the shell
  8. Test your automation
  9. Save it with the desired name

Next, follw these steps to attache dthe automations to a keyboard shortcut:

  1. Open System Preferences > Keyboard > Shortcuts
  2. Select Services
  3. Scroll down to General

Each of the autoamtions should be checked. Now, assign your desired shortcut key.

This portion should easily automated with default read ~/Library/Preferences/pbs.plist NSServicesStatus and /usr/libexec/PlistBuddy --help on macOS Bug Sur at least...

#!/bin/sh
DEVICE_HOSTNAME="rokutv.hostname"
DEVICE_IP="$(host ${DEVICE_HOSTNAME} | sed -En 's|.* ([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+).*|\1|p')"
get_device_power_mode() {
POWER_MODE=$(curl -sfS -X GET "http://${1}:8060/query/device-info" | sed -En "s|^.+<power-mode>([[:alpha:]]+)</power-mode>|\1|p")
if [ "${POWER_MODE}" == "${2}" ]; then return 0; else return 1; fi
}
set_device_keypress() {
curl -sfS -X POST "http://${1}:8060/keypress/${2}"
return 0
}
if get_device_power_mode "${DEVICE_IP}" "PowerOn"
then
set_device_keypress "${DEVICE_IP}" "PowerOff"
fi
#!/bin/sh
DEVICE_HOSTNAME="rokutv.hostname"
DEVICE_IP="$(host ${DEVICE_HOSTNAME} | sed -En 's|.* ([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+).*|\1|p')"
get_device_power_mode() {
POWER_MODE=$(curl -sfS -X GET "http://${1}:8060/query/device-info" | sed -En "s|^.+<power-mode>([[:alpha:]]+)</power-mode>|\1|p")
if [ "${POWER_MODE}" == "${2}" ]; then return 0; else return 1; fi
}
set_device_keypress() {
curl -sfS -X POST "http://${1}:8060/keypress/${2}"
return 0
}
if get_device_power_mode "${DEVICE_IP}" "Ready"
then
set_device_keypress "${DEVICE_IP}" "PowerOn"
fi
while get_device_power_mode "${DEVICE_IP}" "Ready"
do
sleep 0.25
done
sleep 3
set_device_keypress "${DEVICE_IP}" "InputHDMI1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment