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

Speaking in the shell UNIX
Jacob S. writes:
I wrote the following quick PERL script that uses Applescript to speak the contents of STDIN in the UNIX shell environment. See code comments for more information. Enjoy.

Some cool uses that I use this for are:
- Use in /etc/hosts.allow to alert you when a deny is triggered
- Use to speak ipfw log contents
Read the rest of the article for the (very short) script...

Copy the below text into a file called xspeak. Toss the xspeak file somewhere in your path and chmod +x it.
#!/usr/bin/perl -w

Program: xspeak v1.0 (05 Jan 2002)
# Author: jshaw at sps.lane.edu (http://www.sps.lane.edu/~jshaw)
# Platform: MacOS X 10.1.x
# Synopsis: Simple PERL script that uses Applescript to speak STDIN
# Usage: "foo | xspeak" or "xspeak < filetoread"

$|++; # Flush, just in case

while(<>) {
$args = "\'say \"$_\"\'";
system("/usr/bin/osascript -e $args");
}

    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[8,595 views]  

Speaking in the shell | 18 comments | Create New Account
Click here to return to the 'Speaking in the shell' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
GEH! It's not PERL it's Perl or perl
Authored by: Pi on Jan 07, '02 08:05:18PM

geez...... makes my head hurt like when people say MAC instead of Mac or mac! grr..

Pi
For Great Justice|ecitsuJ taerG roF



[ Reply to This | # ]
Get out more.
Authored by: Anonymous on Jan 07, '02 09:01:40PM

I'm sorry your head hurts when someone says "PERL" instead of "Perl". Perhaps you need to get out more?

In the sense that PERL is a pseudo-acronym of "Practical Extraction & Reporting" language, "PERL" is just fine to use. Sure, according to the PERL (yes, I'm doing it on purpose), "Perl" is proper, but who really gives a flip?

Instead of griping about such little tiny things, your time would be better suited to getting out more, or perhaps getting off your ass and submitting tips/code of value?

By the way, your comment was littered with improper capitalizations and grammar. And oh, those run-on sentences!

Good day.



[ Reply to This | # ]
My Mac thinks its existence is pointless..
Authored by: CoffeeNowDammit on Jan 07, '02 10:20:58PM

All I did was type this at the shell:
% yes | xspeak



[ Reply to This | # ]
My Mac thinks its existence is pointless..
Authored by: el bid on Jan 08, '02 03:23:20AM

Brilliant!

--
el bid



[ Reply to This | # ]
My Mac thinks its existence is pointless..
Authored by: el bid on Jan 08, '02 03:29:53AM

Brilliant!

--
el bid



[ Reply to This | # ]
Script to say the time.
Authored by: Anonymous on Jan 08, '02 01:19:04AM

I never posted this, but it's a script to speak the time. I have it running in my cron every 30 minutes, to remind me how late it is.

#! /bin/sh
if test `date +%H%M` = '1200'; then
/usr/bin/osascript 'say "It is 12 noon."';
elif test `date +%H%M` = '0000'; then
/usr/bin/osascript 'say "It is 12 midnight."';
elif test `date +%M` = '00'; then
date +'say "The time is, %I %p"' | sed s/AM/A-M/ | /usr/bin/osascript ;
else
date +'say "The time is, %I %M %p"' | sed s/AM/A-M/ | /usr/bin/osascript ;
fi

I guess I can explain these commands. The "date" command emits the date. The `` backticks around that command cause the output (the date) to be inserted into the command line, replacing the command itself. `date +'blah'` uses 'blah' as a format string, in this case, some speakable text interspersed with date numbers.

sed is a "stream editor" -- it's basically search and replace on piped data. Here, it turns "AM" into "A-M" so the computer won't speak "am" (rhymes with ham).

The if--elif-else statements make the program say the right thing at the top of the hour, and at noon and midnight.



[ Reply to This | # ]
Script to say the time.
Authored by: Anonymous on Jan 08, '02 11:08:41AM

Nice, thank you for posting this. The simple scripts are always the handiest ones.



[ Reply to This | # ]
Simple Alias to do the same thing
Authored by: cmar on Jan 09, '02 09:28:25AM
put this in your .tcshrc or aliases.mine file


alias say '/usr/bin/osascript -e '''say "!*"''' '


then just type say hello world




[ Reply to This | # ]
NOT the same thing
Authored by: Anonymous on Jan 09, '02 12:26:42PM

Actually it's not the same thing. The original script posted here reads from STDIN.

!* reads the given arguments. Completely different thing, but cool nonetheless.



[ Reply to This | # ]
Help!
Authored by: kernits on Jan 10, '02 02:12:00AM

I created the file and did the chmod +x on it but I get this error upon execution:

[localhost:~] username% ls | xspeak
xspeak: Command not found.

I placed the xspeak app in this location:

[localhost:~] username% locate xspeak
/usr/bin/xspeak

The same place as the clear command. Any help would be appreciated, thanks!

- Ralph



[ Reply to This | # ]
Help!
Authored by: rg200 on Jan 10, '02 11:40:29AM

Do you need to "rehash"? (I mean type rehash at the command prompt...)

It causes the machine to have a look to see if the contents of directories really are what it thinks they are, and usually you need to do it when you put a new command on the system, like xspeak.

I might be wrong - I have never used MacOSX (buying a Tibook next week!) but the above has been my experience with unix.

Russell



[ Reply to This | # ]
Help!
Authored by: Anonymous on Jan 10, '02 02:16:48PM

As rg200 said, you should just need to type "rehash" and it should work.

Might I also suggest that you create a "/User/yourusername/bin" directory, and add it to your path at the prompt:

% mkdir ~/bin
% vi ~/.cshrc and add/modify the line:
set PATH "${PATH}:/User/yourusernamehere/bin")


Good luck.

It's good to keep non-system stuff out of /usr bin directories itself, and store them in /usr/local/bin or ~/bin, instead. It also makes it easy for backups, because you don't have to dig through system directories and figure out which scripts/executables you installed, and which are OS X installed.



[ Reply to This | # ]
It would be great if there were a /dev/speech
Authored by: victory on Jan 10, '02 04:42:00PM

There was a nifty feature in the old AmigaDOS(!) -- a SPEAK: device. Basically anything that you redir/piped to it got spoken. Maybe Apple will consider doing something similar (such as a /dev/speech or something). That way all one would have to do is 'cat myfile.txt > /dev/speech', etc.

Nice job on the Perl script.



[ Reply to This | # ]
Here's a Speakable Fortune
Authored by: thinkyhead on Jan 19, '02 06:39:57PM

First you need to have "fortune" installed. It's posted here:
http://homepage.mac.com/slahteine/macosx/

Then paste this into your favorite editor and save it with the name "gag" or whatever you like.

#!/usr/bin/perl
#
# gag : say what fortune says, cleanly
# by slur, Jan 18 2002
#

$gag2 = $gag = `fortune`;
$gag2 =~ s/\\n/ /g;
$gag2 =~ s/"/\\"\\"/g;
$gag2 =~ s/'/''/g;

print "\\n$gag\\n";
`/usr/bin/osascript -e 'say "$gag2"'`;

-------------

Don't forget to "rehash."



[ Reply to This | # ]
Here's a Speakable Fortune
Authored by: thinkyhead on Jan 19, '02 06:42:00PM

#!/usr/bin/perl

$gag2 = $gag = `fortune`;
$gag2 =~ s/\n/ /g;
$gag2 =~ s/"/\"\"/g;
$gag2 =~ s/'/''/g;

print "\n$gag\n";
`/usr/bin/osascript -e 'say "$gag2"'`;




[ Reply to This | # ]
Here's a Speakable Fortune (bloody slashes!)
Authored by: thinkyhead on Jan 19, '02 06:44:18PM

#!/usr/bin/perl

$gag2 = $gag = `fortune`;
$gag2 =~ s/\n/ /g;
$gag2 =~ s/"/\"\"/g;
$gag2 =~ s/'/''/g;

print "\n$gag\n";
`/usr/bin/osascript -e 'say "$gag2"'`;




[ Reply to This | # ]
Sorry, Preview is Wacked - Use this one
Authored by: thinkyhead on Jan 19, '02 06:49:44PM

#!/usr/bin/perl

#
# gag : say what fortune says, cleanly
# by slur, Jan 18 2002
#

$gag2 = $gag = `fortune`;
$gag2 =~ s/\n/ /g;
$gag2 =~ s/"/\\"\\"/g;
$gag2 =~ s/'/''/g;

print "\n$gag\n";
`/usr/bin/osascript -e 'say "$gag2"'`;



[ Reply to This | # ]
laundry reminder
Authored by: Thom on Jun 19, '03 01:37:31AM
I wrote the following in order to remind myself of laundry, etc.
It's a 'nag me in N minutes with message M' sort of deal.

With the exception of the control-D's below, you should be able to cut and paste pretty easily, and it'll work (provided you've got a ~/Library/init/tcsh directory and a ~/bin that's in your $PATH).

If you don't have ~/bin or init/tcsh set up, you may want to consult this hint.


cat > ~/bin/speak_reminder
#!/bin/zsh
time=$1; shift; message="$@";
`sleep $(( 60 * $time )); osascript -e "say \"$message\"";`
^d (hit control + d to stop dumping into the file)

chmod 755 ~/bin/speak_reminder

rehash

cat >> ~/Library/init/tcsh/aliases.mine
alias laundry 'speak_reminder \!^ laundry done &'
^d (Again with the control-d)

source ~/Library/init/tcsh/aliases.mine

laundry 5


The first piece, speak_reminder, is used like
speak_reminder 5 blah blah blah blah
where the first argument is how many minutes to wait, and the remainder of the line is what to say.

As others have suggested, there are other things to do with AppleScript, such as putting up a dialogue box with an 'OK' button, so in case you don't hear the speech, you would still see the box on the screen, etc.

The usage of the second piece, the 'laundry' alias, is shown -- laundry N -- where N is how many minutes to wait before saying the 'laundry done' message.

Note that in the alias 'laundry' (in tcsh) I'm using !^ to insert the command line the user types in the middle of the alias. I'm also using & at the end of the line to 'background' the job so I get my prompt back immediately.

I like this setup better than some of the ones I've seen before on here because the script is generic, but you can make as many aliases to it as you want, which could either have predefined times, messages, or both.

Have fun -- I have to go put my laundry in the dryer.

Thom

[ Reply to This | # ]