Run a couple of actions in background

Hello everyone, I’m new here and I simply search some help to implement some function in my app.
Basically, there is a loop of instructions that can take a few time as it can take much more time.
By taking this much time, the button that contains the istructions keeps freeze and in general the whole app keeps freeze, until the loop finishes. I just want to run the loop in background to make sure that the app doesn’t freeze.
Any help?

Thanks to everyone who will answers!

There are various approaches. Your app has only one instance of AppleScript, and doing stuff on background threads doesn’t really help. The simplest approach is to make liberal use of displayIfNeeded().

Sorry, i’m a but new with this programming language…
What does displayIfNeeded() do? And how can I use it?

You have to know a bit more than the language – you need to understand how the event loop works. Basically, when you do something that calls a method, all the work in that method is carried out, and then the UI is updated. The system then gets the next queued event, and does the same thing. And so on. If you want to update the UI earlier, you have to prompt it to, using something like the window’s displayIfNeeded() method.

You can look up the event loop for more details of how it works.

I used something like:


repeat until aVariable1 = aVariable2
         set aVariable1 to (aVariable1 + 1)
         theProgress's incrementBy_(2)
         aWindow's displayIfNeeded()
end repeat

But the window still freezes… I guess I haven’t understood…

That should at least update the window’s views.

No, that isn’t what I were looking for, and it doesn’t update the view though, or I’m writing a wrong part of code…
However I wonder if is there another way to do this…

P.S. Sorry if I’m stressing you out…

I’m not stressed, but doing it in AS often involves compromises. If you need the app to be responsive, you need to poll the event loop yourself regularly. It’s not hard, just a bit difficult to describe here. FWIW, the options are covered in chapter 13 of my book.

AppleScriptObjC Explored? Is that just and e-book or even a paper book?

It’s in PDF form, with accompanying project files.

Shane, an NSTimer could do what I want?

Not really. An NSTimer is for delaying things. By it’s nature it results in updates as a byproduct, but you’d have to break your process up into smaller chunks to make it work, and at that stage you might as well not use a timer.