Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ZildjianKX

macrumors 68000
Original poster
May 18, 2003
1,610
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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"
 

ZildjianKX

macrumors 68000
Original poster
May 18, 2003
1,610
0
robbieduncan said:
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.

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.
 

ZildjianKX

macrumors 68000
Original poster
May 18, 2003
1,610
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
At the top of the script file just define your variables as properties:

property myNumber : 0;
property myString : "";
 

ZildjianKX

macrumors 68000
Original poster
May 18, 2003
1,610
0
robbieduncan said:
At the top of the script file just define your variables as properties:

property myNumber : 0;
property myString : "";

Yet again I must thank you. You're an AppleScript Studio guru.
 

guifa

macrumors 6502
Sep 19, 2002
260
0
Auburn, AL
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
ZildjianKX said:
Yet again I must thank you. You're an AppleScript Studio guru.
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!
 

skateme

macrumors newbie
Jul 14, 2008
3
0
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.

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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.