Thursday, August 17, 2017

#1 2017-08-02 05:09:39 pm

MoisiePants2000
Member
Registered: 2017-08-02
Posts: 3

Structure to Keep AppleScript Studio app UI responsive

Hello all:

[Posting this on the normal AppleScript forum, as I think the discussion is not strictly ASS-related.]

I've got an AppleScript Studio application which I'm doing some maintenance on; unfortunately I need to retain compatibility with Mac OS X 10.6, so moving to ASOC is not an option.

My app is working generally well; it does a lot of looping actions, so I present a progress bar and label showing the current item being operated on in the UI. However, the progress bar and label don't accurately match the state of the app - they get somewhat sluggish and left behind, even though the actual processing is proceeding as expected.

I presume that I'm falling foul of AppleScript's single-threaded model here, and that I should look to move the 'do work' code off the main thread and onto a background thread, along the lines of discussion here: http://www.macscripter.net/viewtopic.php?id=34563 - thanks Bastiaan!

However, I can't see how to get variables from the background thread back into the main thread, in order to update the UI elements.

The options I've thought of are:

- Call a function in the main thread from the background thread, passing the variables as arguments. How would I address the main thread function from the background thread? And does this just move the blockage from the main thread to the background thread?
- Update a global variable from the background thread. But I can't find the equivalent of a 'didChange' notification, on which to trigger a UI update function in the main thread;
- Update a global variable from the background thread, and have the main thread update the UI on a loop with a delay - but then the UI isn't really much more responsive than in the original case!


Any thoughts on this are much appreciated.

Best wishes,

Pants.

Offline

 

#2 2017-08-02 06:52:08 pm

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

Re: Structure to Keep AppleScript Studio app UI responsive

MoisiePants2000 wrote:

thanks Bastiaan!



You're welcome smile. Nice to tho see that post is still useful to some people.

MoisiePants2000 wrote:

However, I can't see how to get variables from the background thread back into the main thread, in order to update the UI elements.



There are properties of the UI element of the applications that can be shared, don't forget that AS-Studio applications are fully scriptable. Instead of sharing a variable you can put the variable content into the one of the UI element properties or just create an hidden UI element. I know it's not the most beautiful solution but it works and in user land you don't notice it at all. I remember using an hidden window with UI elements to share data between the app and external scripts. You can even trigger events with hidden buttons that are clicked by the background application.

It's been an long time but IIRC you need something like this:

Applescript:

--In the background application
Tell application "My AS-Studio appliciation"
tell window "share"
tell button "shared data 1"
set title to "some data"
click it
end tell
end tell
end tell

Applescript:

-- in your application
on clicked theObject
if name of theObject is "Shared data 1"
set theContents to title of theObject
end if
end on clicked

Offline

 

#3 2017-08-03 03:38:18 am

MoisiePants2000
Member
Registered: 2017-08-02
Posts: 3

Re: Structure to Keep AppleScript Studio app UI responsive

Hello:

Thanks Bastiaan - that's absolutely genius! I'm sure this will work great.

Best wishes,

Pants.

Offline

 

#4 2017-08-07 08:03:10 am

MoisiePants2000
Member
Registered: 2017-08-02
Posts: 3

Re: Structure to Keep AppleScript Studio app UI responsive

Hello:

For the benefit of anyone else following this:

I was successfully able to follow Bastiaan's suggestion to pass a variable back to the main application from the background application by entering it into a text field - but I then encountered a couple of other problems:

(1) (Unless I'm mistaken) I'd forgotten that you need to enable access for assistive devices to allow AppleScript to click the button of another application - and this is something I want to avoid. I deploy my application across quite a wide user base, and I don't want them to have to go through this step to do it. So I can't use Bastiaan's suggestion to provide an update notification path;
(2) I consequently put the UI updating code in the main application into an idle handler - but I found that this was only calling at a maximum rate of about once per second - and I ideally wanted my UI to update more frequently than that.


So I've decided to keep my processing code inside the main application - but have found that by adding a 'delay 0.01' statement in my repeat loop, immediately after the UI update code, my UI is now super-super-responsive! big_smile


Hope this is helpful to anyone else still writing code back in the dark ages...

Thanks,

Pants.

Offline

 

#5 2017-08-07 09:15:34 am

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

Re: Structure to Keep AppleScript Studio app UI responsive

MoisiePants2000 wrote:

For the benefit of anyone else following this:



Thanks for sharing. I forgot about the 'access for assistive devices'.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)