PDA

View Full Version : Using 'say' command in python or C++




Altair89
Jun 8, 2008, 02:01 PM
First off, hello everyone.

Secondly: I have a pretty simple desire, i want you use Mac OS X's 'say' utility in either python or c++. (ex. in terminal type "say Hello World")

I basically want to make a small program to manipulate text strings and pass them to the say utility while the program continues to run. I have some (limited) experience to python and C++ so which ever is easiest I'll choose to use.

Any advice? I'll be sure to post the final project, i imagine some of you would enjoy it :).

Thanks!



lee1210
Jun 8, 2008, 02:24 PM
This is C. I can try to adapt it to C++ using strings instead of char *, but the concept is the same:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
char command[2048];
int len;
if(argc < 2) {
printf("Usage: runsay \"String to speak\"\n");
return -1;
}
len = strlen(argv[1]);
if(len > 2040) {
printf("Please enter a string to say that is less than 2040 characters.\n");
return -2;
}
snprintf(command,2048,"say \"%s\"&",argv[1]);
system(command);
return 0;
}


-Lee

Edit:
Worked up a C++ version:

#include <iostream>
#include <string>

using namespace std;
int main(int argc, char *argv[]) {
string command = "say \"";
if(argc < 2) {
cout << "Usage: runsay++ \"String to say\"" << endl;
return -1;
}
command.append(argv[1]);
command.append("\"&");
system(command.c_str());
return 0;
}