Tuesday, January 23, 2018

#1 2009-03-05 10:40:45 am

MRoig
Member
Registered: 2008-04-27
Posts: 111

How to exit a loop by pressing a button?

Okay, here is a little parred down script I wrote to show what I'm trying to do. By pressing a button, the user will set off a loop.

However, I want to add a button for the user to exit the loop, but the entire interface becomes completely unresponsive whilst inside the loop. Is there a way to keep a script responsive to user input while running?

Applescript:

on clicked theObject
   repeat with i from 1 to 20
       DoSomething()
       delay 0.5
   end repeat
end clicked

Offline

 

#2 2009-03-05 12:02:05 pm

SuperMacGuy
Member
From:: Amish Country, Lancaster, PA
Registered: 2004-06-23
Posts: 416
Website

Re: How to exit a loop by pressing a button?

I have an AStudio project that I can stop while its running. It's really complex, and requires a UI.
The interface has a button that is called "start" and when the user presses it, it changes to "stop".
In the code I have a variable, called something like "continue". When the loop starts, Continue is set to true. It checks if state of Continue multiple times through everything that happens. It's a lot of IF-Then statements. Like this:

If Continue is true then.... [do all this stuff func 1] ... end if
If Continue is true then.... [do all this stuff func 2] ... end if
If Continue is true then.... [do all this stuff func 3] ... end if

The user can press the Stop button at any time. This sets the Continue variable to FALSE. So then the next time Continue's trueness is checked, it's false and the rest of the script stops. The downside to this is that the function that is currently running will keep going until it's over and the next Continue check is fired. In my app there's a lot going on and sometimes the UI is not very responsive, but the button is clickable.
You also could use a While loop
repeat while myContinue is true
[do something]
end repeat
In either of these cases the largest hurdle is when does the Continue check happen, and how quickly is your script running that it can terminate when  you need it to.
This is my way of doing it, there might be a more elegant solution.

Chris


Scripts for InDesign: http://chris.paveglio.com
ASOC & Cocoa, specializing in InDesign, Photoshop, Ai, FileMaker

Offline

 

#3 2009-03-05 12:11:06 pm

MRoig
Member
Registered: 2008-04-27
Posts: 111

Re: How to exit a loop by pressing a button?

I see. The problem that I have is that my loop contains a "delay" command. Whilst the half second delay is happening, the program doesn't check the interface at all.

So what I'm really looking for is a way to create a program that can fire off a command every half second without ignoring the interface.

Offline

 

#4 2009-03-06 11:42:33 am

SuperMacGuy
Member
From:: Amish Country, Lancaster, PA
Registered: 2004-06-23
Posts: 416
Website

Re: How to exit a loop by pressing a button?

My Studio apps use an "on idle" loop so you get some lag time between runs.


Scripts for InDesign: http://chris.paveglio.com
ASOC & Cocoa, specializing in InDesign, Photoshop, Ai, FileMaker

Offline

 

#5 2009-03-10 05:12:14 am

looking_glass
Member
Registered: 2009-01-08
Posts: 20

Re: How to exit a loop by pressing a button?

SuperMacGuy wrote:

I have an AStudio project that I can stop while its running.
...

The user can press the Stop button at any time. This sets the Continue variable to FALSE.
...


This sounds really interesting. I tried it but could not get the button to respond while the script was running.

What versions of AStudio (Xcode?) are you using?

Care to upload a little demo app at scriptbuilders.net?

LG

Offline

 

#6 2009-03-10 07:16:15 am

MRoig
Member
Registered: 2008-04-27
Posts: 111

Re: How to exit a loop by pressing a button?

That's exactly the principle I was going for, but it doesn't work. The "Stop" button will simply not respond as long as the repeat loop is going. I made a little project based on your script that you can download here and it clearly doesn't work. If anybody can get this to work please chime in, but I have a sneaky suspicion that we're looking at a fundamental shortcoming of ASS..

Offline

 

#7 2009-03-10 07:56:44 am

looking_glass
Member
Registered: 2009-01-08
Posts: 20

Re: How to exit a loop by pressing a button?

MRoig wrote:

That's exactly the principle I was going for, but it doesn't work. The "Stop" button will simply not respond as long as the repeat loop is going. I made a little project based on your script....


I tried a similar project, same result.

AS cannot handle multi-threading, so while one handler is running, no action on any element is registered by AS.

SuperMacGuy wrote:

The interface has a button that is called "start" and when the user presses it, it changes to "stop".


I think the magic must lie in the Interface Builder work -- the response to the button click must be done outside of AS. Hence me asking for the sample project. It would give us a trick to add a little multithreading to AS.

LG

P.S.: Email of this subscribed topic says there was a reply by "Matt-Boy", it included a similar script as MRoig's project. Apparently it has been deleted. --?

Offline

 

#8 2009-03-10 08:24:24 am

MRoig
Member
Registered: 2008-04-27
Posts: 111

Re: How to exit a loop by pressing a button?

Yeah, I just copied Matt-Boy's script in an actual ASS project just to see what would happen. As I expected, it doesn't work. Not too sure why his post disappeared.

I was thinking of a solution where the applescript in ASS would run a second applescript. The first one taking care of the interface, the second one doing the actual work. Basically creating two threads by creating two apps. A bit ugly, and I'm not sure how it would work, if at all.

Could it be that SuperMacGuy uses some Obj-C to get his projects to work?

Last edited by MRoig (2009-03-10 08:25:35 am)

Offline

 

#9 2009-03-10 08:48:52 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11483
Website

Re: How to exit a loop by pressing a button?

MRoig wrote:

Could it be that SuperMacGuy uses some Obj-C to get his projects to work?


SuperMacGuy mentioned

SuperMacGuy wrote:

My Studio apps use an "on idle" loop so you get some lag time between runs.


that means the idle handler executes one iteration of the repeat loop,
it could look like

Applescript:


property idx : 0
property goOn : false

on clicked theObject
   if name of theObject is "Start" then
       set idx to 20
       set goOn to true
   else if name of theObject is "Stop" then
       set goOn to false
   end if
end clicked

on idle
   if goOn and idx > 0 then
       DoSomething()
       set idx to idx - 1
       return 0.5
   end if
end idle

Last edited by StefanK (2009-03-10 08:49:38 am)


regards

Stefan

Offline

 

#10 2009-03-10 09:06:21 am

looking_glass
Member
Registered: 2009-01-08
Posts: 20

Re: How to exit a loop by pressing a button?

Ah, so!  smile:):)
Thanks!!
LG

Offline

 

#11 2009-03-10 09:08:21 am

MRoig
Member
Registered: 2008-04-27
Posts: 111

Re: How to exit a loop by pressing a button?

Thanks Stefan, that does the trick. I never looked at the "on idle" handler, assuming that it would take maybe 30 seconds or so before it got called, but it's pretty much instantaneous. And it hardly seems to consume any CPU time at all.

The only thing to remember is to tick the "idle" event handler for the application in Interface Builder.

Here's an updated version of the project with Stefan's script.

Again, thank you very much.

Offline

 

#12 2009-03-10 09:55:06 am

Matt-Boy
Member
Registered: 2005-10-21
Posts: 480

Re: How to exit a loop by pressing a button?

Wow, you guys are fast. I posted a response and then less than a minute later realized that it wasn't much different from what SuperMacGuy had posted and probably wouldn't help so I deleted it.

-Matt

Offline

 

#13 2010-10-20 08:07:19 am

marlon4417
Member
From:: NJ
Registered: 2007-11-28
Posts: 62

Re: How to exit a loop by pressing a button?

I have an Applescript Studio Application where a progress panel is shown once the script initiates.  On the progress panel, a I have a progress indicator bar and a "Stop" button, which I need to abort the script when clicked.

The script involves  a repeat loop in MS Powerpoint, so I am not sure how the stop button can be implemented to abort the script.

I thought this may have worked, but not so:

Applescript:

on clicked theObject
if name of theObject is "Stop" then
quit -- even tried error number -128 but no luck
close panel window "progress panel"
...

I know I can force quite Powerpoint, but would rather not do that.  Hoping for some suggestions and thanks.

Offline

 

#14 2010-10-20 10:44:28 am

marlon4417
Member
From:: NJ
Registered: 2007-11-28
Posts: 62

Re: How to exit a loop by pressing a button?

RESOLVED.

With a little searching in this section of the forum (sorry, impatience on my part), this works well:

Applescript:


property stop_loop: false

on clicked theObject

if the name of theObject is "stop" then
set stop_loop to true

end clicked

repeat

if stop_loop is true then
end repeat
set stop_loop to false
end if
....
end repeat

Offline

 

#15 2010-10-25 07:59:26 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2728
Website

Re: How to exit a loop by pressing a button?

Soon there will be a long article how to achieve this in unscripted forum. If you can't wait you can mail me and will send a example project how to do this right. Because Applescript applications has only a main thread you're not able to create a second thread but you can fork your code to a new process and run it in the background. I don't like those repeat loops because they create many overhead that's not necessary.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)