Re: AppleScriptability Meets NSOperation
Re: AppleScriptability Meets NSOperation
- Subject: Re: AppleScriptability Meets NSOperation
- From: has <email@hidden>
- Date: Mon, 29 Mar 2010 22:30:15 +0100
Jerry Krinock wrote:
> I do some lengthy, multi-step tasks in an app like this:
>
> * Wrap each of their dozen or so steps in an NSOperation.
> * Set dependencies so that they execute in sequence.
> * Add them to a suspended NSOperationQueue.
> * Un-suspend the queue.
>
> If an error occurs, all operations in the queue are cancelled and all ends gracefully.
>
> But many of the operations involve updating progress in the user interface, or accessing the document's managed object context, so they actually call back to performSelectorOnMainThread:, via a handy little wrapper in my NSOperation subclass. The user interface unblocks between operations, and I'm happy with the way this works.
>
> But when I invoke a task like this from AppleScript, of course, it returns immediately after loading the queue. That's no good, especially if an error occurs -- the script has already moved on.
The Apple Event Manager supports suspend and resume, but I have no idea if you'll be able to take advantage of this feature in your application; I suspect it'll largely depend on how your scripting support is implemented (Cocoa Scripting?).
Another option might be to use a similar approach to that used by iTunes and other applications where a task may run for an indeterminate length of time: implement a command to start the operation (c.f. iTune's play command), then provide a status property that the script can periodically poll to see if the task is complete. This also avoids the need for the user to mess around with 'timeout' blocks and won't freeze your GUI while the script is waiting for a response. (I think the SIG recommends this approach in these situations.)
A third option would be to go the notification route, where you require client scripts to be run as stay-open applets; when the task completes, your application sends a command to the applet to let it know. Less common, though I have heard of it being used by scriptable backup software (I forget the name).
HTH
has
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden