Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.5: Create an XKCD-influenced Mac 'doorbell' System 10.5
A friend of mine wrote recently on his blog:
Randall Munroe's XKCD has inspired interesting product features in the past. A recent one has sent a lot of Mac users scurrying to set up an audio doorbell on their Mac Minis.
Here's his solution to the problem; paste the following code into a text editor and save it as, for example, say.php in the top-level /Library » WebServer » Documents folder of the Mac you wish to turn into the doorbell:
<?php
if (isset($_GET['say'])) {
  $cmd = sprintf('say "%s"', preg_replace('/[^\w\d ]/', '', $_GET['say']));
  `$cmd`;

} else if (isset($_GET['up'])) {
  $cmd = 'set output_vol to output volume of (get volume settings)
  set volume output volume (output_vol + 10)';
  `osascript -e "$cmd"`;

} else  if (isset($_GET['down'])) {
  $cmd = 'set output_vol to output volume of (get volume settings)
  set volume output volume (output_vol - 10)';
  `osascript -e "$cmd"`;
}
?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="get">
  <p><input type="text" name="say">
  <input type="submit" value="Say"></p>
  <p>Volume:
  <input type="submit" name="up" value="Up">
  <input type="submit" name="down" value="Down"></p>
</form>
<p>Hey Randall, here's how you
  <a href="http://xkcd.com/530/">change the volume</a>.<br/>
  &ndash;<a href="http://manas.tungare.name/">Manas</a>.
</p>
To test from the Mac your created the file on, make sure Web Sharing is enabled in the Sharing System Preferences panel, then load http://localhost/say.php in your browser. To access the "doorbell" from another machine on the same network, use the Bonjour name (as shown in the Sharing System Preferences panel) instead: http://My_MacPro.local/say.php.

For a more detailed walk through, see the original post on my friend's blog.

[robg adds: We've had a lot of hints on using say in Terminal, but as far as I can tell, we've not run anything about changing the remote Mac's volume settings.]
    •    
  • Currently 1.89 / 5
  You rated: 4 / 5 (9 votes cast)
 
[11,565 views]  

10.5: Create an XKCD-influenced Mac 'doorbell' | 9 comments | Create New Account
Click here to return to the '10.5: Create an XKCD-influenced Mac 'doorbell'' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Create an XKCD-influenced Mac 'doorbell'
Authored by: mistersquid on Jan 21, '09 08:22:53AM

VERY NICE!!!!!



[ Reply to This | # ]
10.5: Create an XKCD-influenced Mac 'doorbell'
Authored by: soapbeard on Jan 21, '09 08:31:15AM

Be careful with this, that script contains a command injection vulnerability.
You should fix this if you want to make it accessible by the internet.

eg.

/say.php?say=hello%22%3B+rm+-rf+%2F%3B+echo+%22

would get it to run this:

say "hello" ; rm -rf / ; echo ""

Which would say hello, then try and delete all your files. With the right url it could do anything, like use your machine to send spam.

I would suggest changing:

$cmd = sprintf('say "%s"', $_GET['say']);

to

$cmd = sprintf('say "%s"', preg_replace('/[^\w\d ]/','',$_GET['say']));

which should remove anything other than a letter, digit or space and stop any command injection.




[ Reply to This | # ]
Thanks, skicker; fix added
Authored by: ManasTungare on Jan 21, '09 08:51:52AM

Thanks for highlighting the command injection vulnerability! I added the fix to the blog post.
–Manas.



[ Reply to This | # ]
10.5: Create an XKCD-influenced Mac 'doorbell'
Authored by: ArtemisG3 on Jan 21, '09 08:56:28AM
If all you are looking to do is remotely increase the output volume, try this:

osascript -e 'set volume 7'

via I'm An Idiot - Geek stuff

[ Reply to This | # ]
Setting volume versus Increasing volume
Authored by: ManasTungare on Jan 21, '09 10:38:56AM

What if the volume was 8 to start with? The script, as presented, increases or decreases volume irrespective of the current volume (except, of course, at the extremes).



[ Reply to This | # ]
Setting volume versus Increasing volume
Authored by: ArtemisG3 on Jan 21, '09 11:15:22AM

7 is the maximum setting.



[ Reply to This | # ]
Setting volume versus Increasing volume
Authored by: ManasTungare on Jan 21, '09 11:53:38AM

Ah, OK. Thanks for the clarification!



[ Reply to This | # ]
SECURITY VULNERABILITY
Authored by: prnl on Jan 21, '09 09:58:19AM

Please don't! This lame code allows anybody to execute any shell command they want! (attacker can remotely take over your machine)

To be secure, every use of variable data in shell arguments MUST be filtered using escapeshellarg() command. Shell-specific escapes are neccessary, as PHP's magic_quotes (and addslashes()) is not sufficient for shell.



[ Reply to This | # ]
10.5: Create an XKCD-influenced Mac 'doorbell'
Authored by: penguify on Jan 25, '09 11:18:29AM

To change system volume to max via shell:

osascript -e "set volume output volume 100"

Or replace the 100 with an integer between 0 and 100 to get the corresponding volume. (mute=0, half volume=50, etc.)



[ Reply to This | # ]