PDA

View Full Version : AppleScript Studio "on clicked theObject" question




ZildjianKX
Apr 2, 2006, 01:20 PM
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
Apr 2, 2006, 05:47 PM
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
Apr 2, 2006, 05:58 PM
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
Apr 2, 2006, 06:21 PM
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
Apr 2, 2006, 10:01 PM
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
Apr 3, 2006, 02:18 AM
At the top of the script file just define your variables as properties:

property myNumber : 0;
property myString : "";

ZildjianKX
Apr 3, 2006, 08:20 PM
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
Apr 3, 2006, 09:32 PM
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
Apr 4, 2006, 02:33 AM
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
Jul 14, 2008, 11:55 AM
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!

skateme
Jul 14, 2008, 09:20 PM
Anyone?