A few beginner questions...

Hey guys - I’m not new to coding but I’m new to Applescript. I have a working script that does some clean up in my iTunes library, and I’ve been trying to implement a nice little interface for it…and having some trouble:

  1. I can get a start button to run my script, but while the script is running the interface seems to freeze up…I can’t even get into menu items etc. How do I avaoid this…does it need to be threaded somehow?

  2. My applescript displays a confirmation dialog before it actually runs the loop. When this is displayed in a panel, I can cancel out of it properly. However, if I display the dialog as a sheet instead, it doesn’t wait for me to click OK. (Which means the loop starts and I can’t click OK even if I wanted to, since the interface freezes a la problem 1)

  3. I would like some of my checkboxes to be enabled only when certain other checkboxes are selected. Is there an easy way to make this happen through connections? bindings? (I don’t know bindings at all…) or through handlers?

Thanks!

As far as I know, you can’t avoid this (though if exists a way, I’d also like to know it, perhaps some obj-c trick?)

If you use a sheet, then you click OK (if you can), the event will be handled by “panel ended” in your script. So, the behaviour is the expected. The dialog is opened in a sheet, then the script continues. One more time, I don’t know if it does exist a workaround (another trick?)

Well, i have a little workaround i use, you just set a property (ie 0), and make the button set that property to something else (ie 1). Then, you use an ‘on idle’ command, and have that check to see if the property was changed (ie 'if property_name is 1…) . i hope that helps

  1. I checked out the on idle handler. It appears that it is used within stay-open scripts to run periodic functions. In other words, it only gets called every 30 seconds by default, or by a user specified number of seconds. Even so, this is not what I really need because I’m running a time intensive loop that shouldn’t have to wait around between iterations. So I’m still stuck.

  2. About the “start” button: you’re saying that I should run the loop within an on panel ended handler rather than in the on clicked handler? That could work. A related question though…about connections. They are great for making a button, say, open another window. However, what if I have a button that is supposed to open a window, close another window, and start the animation of a progess bar all at once? Is there no way to make multiple connections? (I can always place the code inside the onClicked handler, but I like getting what I can “for free”…)

Thanks again for your help.

on clicked theObject
display dialog “Click a button” attached to window “main” buttons {“Cancel”, “Continue”} default button “Continue”
end clicked

on dialog ended theObject with reply theReply
display dialog theReply
end dialog ended

– Doesn’t fricken work…dialog ended handler never gets called. What am I missing?

In IB, select the window where the dialog will be attached -“sheeted”- and next go to the applescript pane (apple+7) and check the “panel ended” event handler, then the script of your app below.

Thanks for the help…that did the trick. I really appreciate the assistance.

I’m still looking for a way to work around the whole “freeze during applescript execution” thing. Any ideas would be great!

Thanks again.

well, it will work for what you want.

[fake script]
on_click
set property_thing to 1
end click

on idle
if property_thing is 1
whatever you want to do. the whole thing.
even if it repeats, etc.
set property_thing to 0
then just check this every couple seconds or something. it doesnt really matter.
end idle
[/fake script]

so, once it sees that property_thing is 1, it will run your script. include the ENTIRE script inside there, and it will do it all at once. then, at the end of that, switch the property thing back to 0, so it doesnt keep repeating it. since it just has to do 1 ‘if’ statement when it is not running, it won’t take much CPU, and that will allow you to have it repeat every second or so.

That’s the problem…the freeze up is only during the loop. So even if it’s within an on idle handler, if it all gets run at once it’s still going to freeze. This loop can take 2-3 minutes to run, after which it stops automatically. So basically, the on idle doesn’t help if the whole loop is inside it, since my script already works fine before and after the loop runs.[/u]
[/quote]

Have you tried? it was freezing up on me in my app, but when i implemented this, it stopped freezing. Granted, my app is probably pretty different, but it should be the same. if you put it in the on idle, it will still take the same amount of CPU power, but will not freeze the interface.

ok, the best way i can show you why that happens is to explain why is it freezing. what is happening is that when you click the button, it runs the script; but inside the button click. so, when you try to click on anything else, it won’t let you, because you are still ‘clicking’ the button.

Well, what you’ve said makes sense, and gives me hope. Still, I can’t seem to avoid the freeze. Here’s my program flow:

A button in the interface calls an onClicked handler. This handler generates a panel displayed as a sheet. This terminates in an on panel ended handler, which as you’ve suggested alters a boolean property of my script to true. An on idle is hooked up to my application and does fire properly every second. Within this I check for the boolean as you suggest, flip its value to false when it finally is true, and call some function(). function() contains the time intensive loop. Still, the interface freezes. I really can’t have this happen, since it IS time intensive and a cancel button is a necessity.

Hope that makes sense…and hope it can be solved. Thanks for trying!