r/applescript • • Oct 30 '24

out-of-sequence execution in script?

I use an AppleScript (running directly from the Script Editor application) to control some apparatus-- it is a legacy system running macOS 10.9. The script talks to a couple of applications and loops until the "done" condition from one of the applications is reached. After that is code to tell an app to download the data.

Very occasionally the script will seem to execute lines from the script out of sequence. For example today it seemed to execute the "data download" section of code before the "done" condition had been reached.

Is it a thing that applescript can execute lines of a script out of order? If so, is there any way to strictly enforce execution order?

2 Upvotes

7 comments sorted by

View all comments

Show parent comment

1

u/agrajag63 Oct 31 '24

The problem seems to happen after the "repeat until" loop in the middle, which is intended to wait until the data collection is done. Each data scan runs 20 to 60 seconds, so there is the short delay in the loop so it doesn't loop too many times but does detect the end condition pretty quickly

1

u/duquesne419 Oct 31 '24

Just giving this a quick look, sorry if I'm overlooking something.

I don't see where the variable "currentpass" is instantiated. If it is being set to a standard number, you might be able to predetermine the number of loops needed, and switch the problematic while loop to a discrete for loop. I tend to favor for loops because the downstream code matters less if you're always running a set number of loops. But if the counting on currentpass isn't standard then it may not matter. Just a thought for something to try.

Alternatively, can you force the download to wait 75 seconds instead of trying to calculate length? If you know you're going to be done in 60 seconds or less but having issues with an early exit, maybe keep the door closedpast 60?

2

u/agrajag63 Oct 31 '24

Sorry it's not clearer. The "currentpass" is an integer parameter read from the "Data Controller" application - running from 1 at the start up to the preset limit (here 1000) based on the triggers sent.

Seem to have veered off-topic a bit in wondering if out of order execution is possible in the script as it is run. It's true I could try to calculate a better approximate delay before looking for the can to finish, but that might just mask the problem. It seems tricky as the issue arises very infrequently so hard to debug.

Thanks for the suggestions and scrutiny though.