AppleScript Studio "on clicked theObject" question

Discussion in 'Mac Programming' started by ZildjianKX, Apr 2, 2006.

  1. macrumors 68000

    ZildjianKX

    Joined:
    May 18, 2003
    #1
    I'm still new with AppleScript studio but not to AppleScript and I'm having a bit of an issue making a program work the way I want to.

    Right now I have a program that when you hit the "start" button it goes through a long loop that can take a long time to finish. The problem is since this happens inside "on clicked theObject" no other buttons are responsive after you click the "start" button.

    Ideally I'd like to allow the user to hit the "cancel" button and break out of loop that starts when they hit the "start" button, just not sure how to do it since no other keys can even be pressed while the loop is going in "on clicked theObject."

    A second question that is much less important... but still curious about. Is there anyway to rename a key after it has been pressed? I'd like the "start" key to become the "stop" key after the user presses it.

    Thanks in advance, hope what I said made sense.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    You need to go multithreaded to allow the other buttons to function. What is happening is that your loop is stopping the run loop from processing events. Unfortunately there is no way of going multithreaded in ASS.

    But all is not lost, there is a solution :)

    Perform your long task in an "on idle" handler. This will run when the app is idle. Use variables to track your position in the loop, perform a single step each time you are "on idle" (do not simply put the whole loop in here and run it through completely otherwise the first time on idle is called the app will freeze again). Use a flag to stop the loop when cancel is clicked.
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    Oh and changing the title of the button is easy. Assuming the start button is named startButton and is added directly to window named window (both named via the AppleScript tab in Interface Builder) you can do

    set title of button "startButton" of window "window" to "Stop"
     
  4. thread starter macrumors 68000

    ZildjianKX

    Joined:
    May 18, 2003
    #4
    Thanks for your response, I was wondering if there was some way to do it with on idle.

    So to get this straight, have a "on idle" handler outside of the "on clicked theObject" handler.

    Then in the "on idle" handler I can have it check to see if the start button is pressed, and if so, start looping. But in the loop, have it check at different stages to see if the cancel button has been pressed?

    Errr, not sure if that is what you meant.
     
  5. thread starter macrumors 68000

    ZildjianKX

    Joined:
    May 18, 2003
    #5
    I got it working, thanks so much!

    If I could just pick your brain about one more thing...

    Have you had any luck getting global variables to work in AppleScript Studio?

    No matter where I define them in my script, I always get the undefined variable error if I access them in my handlers.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    At the top of the script file just define your variables as properties:

    property myNumber : 0;
    property myString : "";
     
  7. thread starter macrumors 68000

    ZildjianKX

    Joined:
    May 18, 2003
    #7
    Yet again I must thank you. You're an AppleScript Studio guru.
     
  8. macrumors 6502

    Joined:
    Sep 19, 2002
    Location:
    Auburn, AL
    #8
    well, you CAN do multithreading in ASS but it's just easier to do it in the on idle handler unless you need two or three things going on at once. You just launch faceless scripts that then interact with the main application ...

    Also, note that properties don't hold like they do in traditional scripts

    Also, making a variable global does nothing more than say "once this is set I want to use it all over the script". You have to first set at some point. That's why using properties can sometimes make more sense, since you can start using them immediately.
     
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    Actually I've used it to build one little thing once! I just happened to have the answers to your questions in that one short script!
     
  10. macrumors newbie

    skateme

    Joined:
    Jul 14, 2008
    #10
    Sorry to bump this really old thread but I was wondering whether anyone could explain to me how he got it working lol. Thanks in advance!
     
  11. macrumors newbie

    skateme

    Joined:
    Jul 14, 2008

Share This Page