Tuesday, May 21, 2013

#1 2012-03-08 03:28:52 pm

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

Will OBJ-C wait for long ASOC events?

I was going to build an app in Objective-C, but I need to have some specific ASOC components. And some of these actions are going to take a long time, like 3-20 seconds.
Will Objective-C "wait" for replies to come back from a script?

Like if I say

NSString *aResult = [asocScripts doSomeLongEvent:parameter];

will it wait for aResult to be something? Or will it assume nil/null or failed function after X seconds?
I don't want to start down this road if it would be better off in a straight script because of the application's delays (Adobe illustrator functions to be specific).


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

Offline

 

#2 2012-03-08 03:53:47 pm

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

Re: Will OBJ-C wait for long ASOC events?

Unlike single threaded AppleScript the Objective-C runtime works quite completely different, because you're be able to perform tasks asynchronously on multiple threads.

The big advantage is: there is no need for those creepy AppleScript wait loops which are very expensive.
The Objective-C way is to use notifications or protocols / delegates to inform the main task about the several states of the child task, those protocol methods appear often like

- (BOOL)shouldDoSomething:(id)sender // the boolean value indicates to allow/deny the task
- (void)willDoSomething:(id)sender // will be called before starting the task
- (void)didSomething;(id)sender // will be called after finishing the task

notifications work similarly, in an ASOC environment I guess notifications are preferable

Last edited by StefanK (2012-03-08 03:54:55 pm)


regards

Stefan

Offline

 

#3 2012-03-08 04:56:21 pm

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

Re: Will OBJ-C wait for long ASOC events?

SuperMacGuy wrote:

Like if I say
NSString *aResult = [asocScripts doSomeLongEvent:parameter];
will it wait for aResult to be something?

Yes... it waits for the method doSomeLongEvents to be finished like any other Objective-C method will wait for a method to be finished before continuing. Doesn't have anything to do with AppleScript directly it is simply following the rules of Objective-C.


"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
- Brian W. Kernighan

Online

 

#4 2012-03-08 04:57:11 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 2615

Re: Will OBJ-C wait for long ASOC events?

As Stefan says, you can use separate threads, but a lot of AS stuff needs to run on the main thread, as does your UI. There's no risk of timeout, so much as frozen UI. I usually just sprinkle the AS code with fordEvent() from Myriad Helpers, and it works fine. Some of the Illustrator examples I call take a couple of minutes to run.


Shane Stanley <sstanley@myriad-com.com.au>
'AppleScriptObjC Explored' www.macosxautomation.com/applescript/apps/
AppleScriptObjC Explorer www.macosxautomation.com/applescript/apps/explorer.html

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.021 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)