Control a Mac from any Bluetooth-enabled device

Oct 25, '04 09:14:00AM

Contributed by: TwoTimes

This trick will let you execute shell scripts and AppleScripts written on Bluetooth-enabled mobiles or handhelds. I wrote it for those phones not compatible with Romeo or Salling Clicker, like my Siemens S55.

The idea is simple: a 'listener' shell script will watch the download folder for any incoming text file. If the file's content begins with the string cmd=, it will be executed as a shell command. If the header is app=, it will be processed as AppleScript. In both cases, the file will then be deleted. To make it work well, it's better to select "Accept files without warning" in System Preferences -> Bluetooth. As a download folder, I chose /Users -> Shared -> Scripts.

This is the bash script. I saved it as /usr/local/bin/bluecommand (with admin privileges):


#!/bin/bash
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# :: Bluecommand - Execute shell commands sent form a bluetooth device ::
# ::   2004 Salvatore Scandurra - http://www.salvatorescandurra.com/   ::
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

WORKING_DIR='/Users/Shared/Scripts'
HEADER_COMMAND='cmd='
HEADER_APPLESCRIPT='app='
SECS=10 # Seconds before a new check is performed
echo "Waiting for command files in $WORKING_DIR."
echo "Hit Control-C to exit."

while true
do
  for FILE in $WORKING_DIR/*
  do
    if [ -f "$FILE" ]
      then
      IS_CMD=`cut -c 1-4 "$FILE"`
      if [ "$IS_CMD" == "$HEADER_COMMAND" ]
        then
        `cat "$FILE" | cut -c 5-`
        rm "$FILE"
      fi
      if [ "$IS_CMD" == "$HEADER_APPLESCRIPT" ]
        then
        echo `cat "$FILE" | cut -c 5-` | osascript
        rm "$FILE"
      fi
    fi
  done
  sleep $SECS
done
Here are some examples of what you can now do. Just write them down on a text file on your mobile, and send them to your Mac: And so on ... yes, the same goal could also be reached via Folder Actions, but I really don't want to have them enabled.

Comments (14)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20041022175838683