AppleScript running under Cocoa - major speed issue

  • Folks;

    I have a Cocoa app that has some iPhoto integration.
    Since there is no Cocoa API for iPhoto, like there is for
    AddressBook, the only way I know to do this is through AppleScript.
    Any other ideas here would be appreciated....

    BTW: This is OS 10.4.10; Xcode 2.4.1; iPhoto 6.0.6; 2 GHz C2D

    The Cocoa app has a compiled AppleScript which I call into for the
    various handlers with the parameters I need.
    This is working well in every other case.
    For the record it looks like this:
      thisResult = [[[NSApp delegate] asEngine] executeAppleEvent:[ASCode
    callSub:@"getiPhotoImage" params:
      [NSAppleEventDescriptor descriptorWithString:path],
      nil] error:&errorDict];

    However I have a case where it is taking 25 seconds to execute a
    single AppleScript statement!
    This same statement has sub-second response when executed in the
    Script editor.
    If I simulate the Cocoa calls with various handlers in the Script
    Editor everything is fine - performance is fine!

    I have logging statements embedded in both Cocoa and AppleScript
    which have allowed me to hone in on the offending statement.

    The result is perfectly repeatable.  Every time it takes 25 SECONDS
    to execute the following:
    [Cut and Paste this into a Script Editor - should work fine...(just
    change the myPath variable to make it work or fail]
    [You can also save it as a run-only application to see the
    performance is that incarnation...]

    set myPath to "/Users/SOMEUSERNAME/Pictures/iPhoto Library/Originals/
    2007/Roll 1/SOMEPICTURENAME.jpg"
    tell application "iPhoto"
    try
      set thisPhoto to first photo whose image path is myPath  -- << THIS
    IS THE CULPRIT
      if thisPhoto exists then
      display dialog "'" & name of thisPhoto & "' was found"
      else
      display dialog "This branch will never occur"
      end if
    on error e number n
      if n = -2753 then
      display dialog "Requested photo was NOT found"
      else
      display dialog "Error: " & n & " - " & e
      end if
    end try
    end tell

    I know it may seem like an AppleScript issue but the shoddy
    performance is a ONLY when it is run underneath a Cocoa app so I am
    under the impression that there is something Cocoa-related which is
    causal.
    I don't believe any of our friends on the AppleScript list will have
    anything helpful, however I AM cross-posting to AppleScript list to
    see what I can find....

    Could anyone shed any light on what is going on here?
    Is there a hidden AppleScript feature like iTunes' 'fixed indexing'
    which might improve response times?
    Why does it work in Script Editor or as a AS run-only application?

    Thanks for any and all thoughts!
    Steve
  • Steve Cronin wrote:

    > I have a Cocoa app that has some iPhoto integration.
    > Since there is no Cocoa API for iPhoto, like there is for
    > AddressBook, the only way I know to do this is through AppleScript.
    > Any other ideas here would be appreciated....
    [...]
    > I know it may seem like an AppleScript issue but the shoddy
    > performance is a ONLY when it is run underneath a Cocoa app so I am
    > under the impression that there is something Cocoa-related which is
    > causal.

    No idea. AppleScript component memory allocation can be an issue for
    some things (e.g. recursion depth), but I dunno if it'd be a factor
    here. The ways of AppleScript are deep and mysterious, not to mention
    a bit mental too.

    At any rate, since you're presumably working in ObjC, I'd suggest
    bypassing AppleScript altogether and see if that solves the problem
    for you. e.g. Using objc-appscript:

    // To generate glue code: $ osaglue IP iPhoto

    #import <Foundation/Foundation.h>
    #import "IPGlue.h"

    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    IPApplication *iphoto = [[IPApplication alloc]
    initWithName:@"iPhoto.app"];

    NSString *path = @"/Users/has/Pictures/iPhoto Library/Originals/
    2001/21 Apr 2001/My Balcony:plants:tree.jpg";

    IPGetCommand *cmd = [[[[iphoto photos] byTest: [[IPIts imagePath]
    equals: path]] first] get];

    id result = [cmd send];

    if (result == nil)
      NSLog(@"iPhoto raised error: %i (%@)\n", [cmd errorNumber], [cmd
    errorString]);
    else if ([result isEqual: [NSNull null]])
      NSLog(@"iPhoto returned no result\n");
    else
      NSLog(@"iPhoto returned reference: %@\n", result);

    [iphoto release];
        [pool release];
        return 0;
    }

    No speed problems here:

    [Session started at 2007-10-20 22:26:34 +0100.]
    2007-10-20 22:26:34.954 iphototest[24868] iPhoto returned reference:
      [[IPApp photos] byID: 4294968071]
    iphototest has exited with status 0.

    HTH

    has
    --
    http://appscript.sourceforge.net
    http://rb-appscript.rubyforge.org
  • On 20 Oct 2007, at 16:46, Steve Cronin wrote:

    > The Cocoa app has a compiled AppleScript which I call into for the
    > various handlers with the parameters I need.
    > This is working well in every other case.

    [snip]

    > However I have a case where it is taking 25 seconds to execute a
    > single AppleScript statement!
    > This same statement has sub-second response when executed in the
    > Script editor.
    > If I simulate the Cocoa calls with various handlers in the Script
    > Editor everything is fine - performance is fine!

    Have you tried using Shark to see exactly what your machine is doing
    during those 25 seconds?  It might be enlightening.

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
  • Folks;

    The knuckle-head in me apologies to the list.  Arrggh!

    The delay was due to contention...
    iPhoto was waiting for me to provide a response to the the drag-event
    which triggered the AppleScript in the first place...

    Hopefully this clarification will help someone else..
    Steve

    On Oct 20, 2007, at 4:50 PM, Alastair Houghton wrote:

    > On 20 Oct 2007, at 16:46, Steve Cronin wrote:
    >
    >> The Cocoa app has a compiled AppleScript which I call into for the
    >> various handlers with the parameters I need.
    >> This is working well in every other case.
    >
    > [snip]
    >
    >> However I have a case where it is taking 25 seconds to execute a
    >> single AppleScript statement!
    >> This same statement has sub-second response when executed in the
    >> Script editor.
    >> If I simulate the Cocoa calls with various handlers in the Script
    >> Editor everything is fine - performance is fine!
    >
    > Have you tried using Shark to see exactly what your machine is
    > doing during those 25 seconds?  It might be enlightening.
    >
    > Kind regards,
    >
    > Alastair.
    >
    > --
    > http://alastairs-place.net
    >
    >
    >
    >
  • On Sat, 20 Oct 2007 10:46:22 -0500, Steve Cronin <steve_cronin...>
    said:
    > Folks;
    >
    > I have a Cocoa app that has some iPhoto integration.
    > Since there is no Cocoa API for iPhoto, like there is for
    > AddressBook, the only way I know to do this is through AppleScript.
    > Any other ideas here would be appreciated....
    >
    > BTW: This is OS 10.4.10; Xcode 2.4.1; iPhoto 6.0.6; 2 GHz C2D
    >
    > The Cocoa app has a compiled AppleScript which I call into for the
    > various handlers with the parameters I need.
    > This is working well in every other case.
    > For the record it looks like this:
    > thisResult = [[[NSApp delegate] asEngine] executeAppleEvent:[ASCode
    > callSub:@"getiPhotoImage" params:
    > [NSAppleEventDescriptor descriptorWithString:path],
    > nil] error:&errorDict];
    >
    >
    > However I have a case where it is taking 25 seconds to execute a
    > single AppleScript statement!
    > This same statement has sub-second response when executed in the
    > Script editor.
    > If I simulate the Cocoa calls with various handlers in the Script
    > Editor everything is fine - performance is fine!
    >
    > I have logging statements embedded in both Cocoa and AppleScript
    > which have allowed me to hone in on the offending statement.
    >
    > The result is perfectly repeatable.  Every time it takes 25 SECONDS
    > to execute the following:
    > [Cut and Paste this into a Script Editor - should work fine...(just
    > change the myPath variable to make it work or fail]
    > [You can also save it as a run-only application to see the
    > performance is that incarnation...]
    >
    > set myPath to "/Users/SOMEUSERNAME/Pictures/iPhoto Library/Originals/
    > 2007/Roll 1/SOMEPICTURENAME.jpg"
    > tell application "iPhoto"
    > try
    > set thisPhoto to first photo whose image path is myPath  -- << THIS
    > IS THE CULPRIT
    > if thisPhoto exists then
    > display dialog "'" & name of thisPhoto & "' was found"
    > else
    > display dialog "This branch will never occur"
    > end if
    > on error e number n
    > if n = -2753 then
    > display dialog "Requested photo was NOT found"
    > else
    > display dialog "Error: " & n & " - " & e
    > end if
    > end try
    > end tell
    >
    > I know it may seem like an AppleScript issue but the shoddy
    > performance is a ONLY when it is run underneath a Cocoa app so I am
    > under the impression that there is something Cocoa-related which is
    > causal.
    > I don't believe any of our friends on the AppleScript list will have
    > anything helpful, however I AM cross-posting to AppleScript list to
    > see what I can find....
    >
    > Could anyone shed any light on what is going on here?
    > Is there a hidden AppleScript feature like iTunes' 'fixed indexing'
    > which might improve response times?
    > Why does it work in Script Editor or as a AS run-only application?

    Steve -

    How do you know which line is "the culprit"? Could this be just an
    assumption you're making? Could the problem actually be the compilation
    time? I find it is necessary to "preflight" the AppleScript engine as the
    app starts up in order to avoid delays the first time a script is run inside
    a Cocoa app.

    Also, this could be just a wild and crazy idea, but I would try changing
    what you think is the troublesome line:

    set thisPhoto to first photo whose image path is myPath

    to this:

    set thisPhoto to (get first photo whose image path is myPath)

    Also, I would try doing a test where myPath is hard-coded into the script
    rather than being passed in as a param (in other words, test it without the
    callSub thing).

    m.
    --
    matt neuburg, phd = <matt...>, <http://www.tidbits.com/matt/>
    A fool + a tool + an autorelease pool = cool!
    One of the 2007 MacTech Top 25: <http://tinyurl.com/2rh4pf>
    AppleScript: the Definitive Guide - Second Edition!
    <http://www.amazon.com/gp/product/0596102119>
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today