Using 'say' command in python or C++

Discussion in 'Mac Programming' started by Altair89, Jun 8, 2008.

  1. Altair89 macrumors newbie

    Joined:
    May 23, 2007
    #1
    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!
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    This is C. I can try to adapt it to C++ using strings instead of char *, but the concept is the same:
    Code:
    #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:
    Code:
    #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;
    }
    
     

Share This Page