my very first program! have a look...

Discussion in 'Mac Programming' started by divinemercy, Jul 15, 2008.

  1. divinemercy macrumors regular

    Jul 12, 2008
    Okay... So i've been playing around with applescript since yesterday as i wait on my objective-c books to come in the mail.......

    ANYways. I did all the classic starters (beep test, dialog boxes, etc)...

    Well, this evening after like 6 hours of trial and error i finally made my own little program, and i would LOVE your feedback! Before i copy in the code so that you can try it and see what you think, let me explain.

    I play a game online where i attack something, then have to wait a long time, then come back and finish the commands... Well, i ofter switch internet windows during this. The problem is that i often get engrossed in this new window and forget my game, which leads to lots of problems losing money and dying and things haha....

    SO i wanted a program to act as a timer to remind me of the game..
    But i wasnt satisfied with that, i wanted it to be voice controlled, which is what took so long.... I only wanted to have to open it once, so i had to figure out how to implement a repeating loop in the program... THEN i wanted it to close itself based on a voice command as well (which was confusing because of the looping....) Frankly i think that this way of ending the program is fairly ingenious... i couldnt find anything like it online haha.

    ANYhow. I finally think i got it! It's only a couple of lines long, and obviously super simple... so im kinda embarassed that it took so many hours haha, but then again it works great for me so im happy to have made my first useful program!

    Below is the code. Please paste it into script editor, and then save it as an application (named myGameTimer )to your desktop to try it out (because of the way i put in a voice-command-shutdown, script editor thinks its an error so you cant just run it from the editor. but it works great once you save it to an app file)

    with timeout of 1800 seconds
    tell application "SpeechRecognitionServer"
    set theResponse to listen for {"Thank You", "towers timer", "quit timer"}
    if theResponse is "Thank You" then
    say "You're Welcome"
    else if theResponse is "towers timer" then
    tell application "Finder"
    delay 5
    say "Ryan, please check your game status."

    end tell

    else if theResponse is "quit timer" then
    tell application "myGameTimer"
    end tell
    tell application "System Events"
    keystroke "." using {command down}
    end tell

    end if
    end tell
    end repeat
    end timeout

    OK... So there it is. Basically you start it, and then you can say "towers timer" to start the timer, and "Thank you" to thank the machine as often as you want interchangably... then "quit timer" whenever you are done. I put the longer timout cause it kept stopping during gameplay, and as a side note i usually have the "delay 5" as "delay 45" since that is about the time i need for the game....

    So what do you think lol? Don't be too mean ^^ :D

    P.S. Edited to add "activate myGameTimer" section... before you had to click the icon before saying "quit timer"... Now you can just say it while surfing the web or whatever haha... So make sure you title it myGameTimer when you save it to your desktop as an app.
  2. toddburch macrumors 6502a

    Dec 4, 2006
    Katy, Texas
    I didn't run it, but it looks good. Good job!
  3. iSee macrumors 68040


    Oct 25, 2004
    It's a very cool app, especially for a first-time effort.

    I like the way you can thank the app whenever you're feeling grateful :D

    Your way of quitting the script is clever. Maybe you can simplify it, though. This might also allow you to run the script in the editor. Try just exiting the repeat loop. Then the script should just end on its own (because there are no more lines of script to run). So, it would be:
    			else if theResponse is "quit timer" then
    				exit repeat
    			end if
    Anyway, it's a nice app, and a good demo of interactive voice scripting.
  4. divinemercy thread starter macrumors regular

    Jul 12, 2008
    Thanks iSee! That is alot simpler haha.... do you ever feel bad using someone's idea like that? Like i feel its less of my script now or that im cheating lol ^^


    Every once in a great while (i think most of the time i have just closed the program and re-opened it quickly), i get the following error message:

    {"Thank You","towers timer","quit timer"} doesn't understand the <<event sprcsrls>> message

    Anybody have any ideas about why that pops up and how to fix it?

    Question 2:

    I'm still getting a timeout after a min or two if i dont use it.... what is the deal? Did i not set up the timeout extention correctly?


  5. luckylefty01 macrumors member

    Apr 8, 2008
    If you have any qualms whatsoever about taking advantage of the advice or ideas other people have offered you then you'll never get anything done (I'm much the same way as you:p). Consider it an opportunity to take the time you would have otherwise spent figuring out the improvements they offered by yourself and putting it into going one level higher and making your app even better instead.

    (With that said there is certainly something to be said for occasionally taking somebody else's code and rewriting basically the same thing for yourself, just because you can and to show that you can and to try to really understand what you're doing. Good ideas, however, should be begged, borrowed, and stolen whenever possible.)

    And, finally, enjoy yourself!
  6. Halloween macrumors newbie

    Jul 14, 2008
  7. iSee macrumors 68040


    Oct 25, 2004
    Nah, you're not cheating. One thing good programmers do is take good ideas where they find them, even when they aren't their own (well, you shouldn't plagiarize or steal IP, but that's a different kind of thing).

    When you're in learning mode and you encounter a tricky problem, the best thing to do is first make a good effort to solve it on your own. You'll learn a lot doing that even if you can't figure it out. Then, ask others for help. That gives you a chance to learn what they already know. In this case you did make a good effort to solve the problem of closing the script. In fact, you were successful, though there were some limitations to your solution. And you also learned a little more about Applescript ("exit repeat" breaks out of a loop). BTW, most languages have some way to break out of a loop like that.

    If it makes you feel better, I learned from your script: how to script the speech recognition server and how to send keystrokes to an app.
  8. divinemercy thread starter macrumors regular

    Jul 12, 2008
    Lol... glad you learned something too...

    Any ideas about the timeout errors im still getting? I tried moving around the timout of___ end timeout parts of the script, and increasing the number of seconds, but neither attempt worked...

    In fact, it is timing out after 1 min, and even normal apps arent supposed to timeout for a default of 2 min right?



Share This Page