Cocoa, iTunes, AppleScript - performance issue

  • Folks;

    I have a set of applescripts compiled into a .scpt resource which I
    deploy as part of an application.
    I have several utility type scripts which I call using something like
    this:

    NSDictionary *errorDict;
    NSAppleEventDescriptor *thisResult;
    NSAppleScript *myAppleScriptUtilities =  [[NSAppleScript alloc]
    initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
    pathForResource:@"ASUtilities" ofType:@"scpt"
    inDirectory:@"Scripts"]] error:nil];
    NSString *str = [myOSXUtilities launchApplication:@"com.apple.iTunes"];
    if ([str isEqualToString:@"True"]) {
    thisResult = [myAppleScriptUtilities executeAppleEvent:[self
    callSub:@"applyGoodnessToTunes" params:
      [NSAppleEventDescriptor descriptorWithString:theGoodness],
    [NSAppleEventDescriptor descriptorWithString:trackID], nil]
    error:&errorDict];

    I have been pretty happy with this setup - I have several different
    scripts (Finder & Mail) working.
    Now I have begun to tinker with iTunes.

    I have logging enabled in the applescripts so that I can debug and
    then later so the customer can help me debug.

    I have a simple script which writes some metadata to selected tracks:
    set startTime to (current date)
    my writeToLog("Tunes", "-- NEW SESSION --")
            ...
    set pidC to length of myPIDList
    repeat with j from 1 to pidC
      tell application "iTunes"
      my writeToLog("Tunes", "talking to iTunes - track count: " & pidC
    as text)
      set oldfi to fixed indexing
      set fixed indexing to true
      my writeToLog("Tunes", "iTunes fixed indexing set ")
              ...
      set fixed indexing to oldfi
      end tell
    end repeat
    set deltaTime to (current date) - startTime
    my writeToLog("TagTunes", "ready to return - total time: " &
    (deltaTime as text) & " secs")

    Now, this snippet rocks when testing in Script Editor.

    BUT when I run my Cocoa app and call out as above here is actual data
    from the log:
    Thursday, September 13, 2007 2:54:47 AM    -- NEW SESSION --
    Thursday, September 13, 2007 2:54:47 AM    talking to iTunes - track
    count: 5
    Thursday, September 13, 2007 2:54:47 AM    iTunes fixed indexing set
    Thursday, September 13, 2007
    2:54:47 AM    top of loop -- track
    pid: 93DF7AD675F07E3C
    Thursday, September 13, 2007
    2:55:11 AM    track pid access
    completed: 93DF7AD675F07E3C
    Thursday, September 13, 2007
    2:55:11 AM    ready to apply changes :
    Everything
    Thursday, September 13, 2007 2:55:11 AM    talking to iTunes - track
    count: 5
    Thursday, September 13, 2007 2:55:11 AM    iTunes fixed indexing set
    Thursday, September 13, 2007
    2:55:11 AM    top of loop -- track
    pid: 93DF7AD675F07E40
    Thursday, September 13, 2007
    2:55:11 AM    track pid access
    completed: 93DF7AD675F07E40
    Thursday, September 13, 2007
    2:55:11 AM    ready to apply changes : Eight
    Easy Steps
    Thursday, September 13, 2007 2:55:11 AM    talking to iTunes - track
    count: 5
    ....

    As you can see there is a nearly 25 SECOND delay as the session gets
    initiated.
    I have seen the delay stated before the 'fixed index'...
    but it always happens AND
    it always happens only once and
    it only happens at the start of the first time thru the loop.
    It seems to me to be related to the first command given to the iTunes
    tell block....

    I have tried several different approaches but nothing has changed the
    25 SECONDS!!
    (BTW: this is a 1G MacBook running 10.4.10 and XCode 2.4.1)

    I am not sure what to do next.

    Does anyone out there have any ideas on what could be causing this?

    I have not seen anything like this for the other AppleScripts I have
    developed.

    Thanks for any help!
    Steve
  • I'm not much of an Applescript expert, but I've noticed Applescript
    seems to want to launch the application before it will talk to it--
    could this simply be the time it takes for iTunes to launch or accept
    focus?

    Maybe try putting a dummy block before that to send a meaningless
    command to iTunes and test if the problem happens in every 'tell
    application "iTunes"' block, or just the first one.

    On Sep 13, 2007, at 1:11 AM, Steve Cronin wrote:

    > Folks;
    >
    > I have a set of applescripts compiled into a .scpt resource which I
    > deploy as part of an application.
    > I have several utility type scripts which I call using something
    > like this:
    >
    > NSDictionary *errorDict;
    > NSAppleEventDescriptor *thisResult;
    > NSAppleScript *myAppleScriptUtilities =  [[NSAppleScript alloc]
    > initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
    > pathForResource:@"ASUtilities" ofType:@"scpt"
    > inDirectory:@"Scripts"]] error:nil];
    > NSString *str = [myOSXUtilities
    > launchApplication:@"com.apple.iTunes"];
    > if ([str isEqualToString:@"True"]) {
    > thisResult = [myAppleScriptUtilities executeAppleEvent:[self
    > callSub:@"applyGoodnessToTunes" params:
    > [NSAppleEventDescriptor descriptorWithString:theGoodness],
    > [NSAppleEventDescriptor descriptorWithString:trackID], nil]
    > error:&errorDict];
    >
    > I have been pretty happy with this setup - I have several different
    > scripts (Finder & Mail) working.
    > Now I have begun to tinker with iTunes.
    >
    > I have logging enabled in the applescripts so that I can debug and
    > then later so the customer can help me debug.
    >
    > I have a simple script which writes some metadata to selected tracks:
    > set startTime to (current date)
    > my writeToLog("Tunes", "-- NEW SESSION --")
    > ...
    > set pidC to length of myPIDList
    > repeat with j from 1 to pidC
    > tell application "iTunes"
    > my writeToLog("Tunes", "talking to iTunes - track count: " &
    > pidC as text)
    > set oldfi to fixed indexing
    > set fixed indexing to true
    > my writeToLog("Tunes", "iTunes fixed indexing set ")
    > ...
    > set fixed indexing to oldfi
    > end tell
    > end repeat
    > set deltaTime to (current date) - startTime
    > my writeToLog("TagTunes", "ready to return - total time: " &
    > (deltaTime as text) & " secs")
    >
    >
    > Now, this snippet rocks when testing in Script Editor.
    >
    > BUT when I run my Cocoa app and call out as above here is actual
    > data from the log:
    > Thursday, September 13, 2007 2:54:47 AM    -- NEW SESSION --
    > Thursday, September 13, 2007 2:54:47 AM    talking to iTunes - track
    > count: 5
    > Thursday, September 13, 2007 2:54:47 AM    iTunes fixed indexing set
    > Thursday, September 13, 2007 2:54:47 AM    top of loop -- track pid:
    > 93DF7AD675F07E3C
    > Thursday, September 13, 2007 2:55:11 AM    track pid access completed:
    > 93DF7AD675F07E3C
    > Thursday, September 13, 2007 2:55:11 AM    ready to apply changes :
    > Everything
    > Thursday, September 13, 2007 2:55:11 AM    talking to iTunes - track
    > count: 5
    > Thursday, September 13, 2007 2:55:11 AM    iTunes fixed indexing set
    > Thursday, September 13, 2007 2:55:11 AM    top of loop -- track pid:
    > 93DF7AD675F07E40
    > Thursday, September 13, 2007 2:55:11 AM    track pid access completed:
    > 93DF7AD675F07E40
    > Thursday, September 13, 2007 2:55:11 AM    ready to apply changes :
    > Eight Easy Steps
    > Thursday, September 13, 2007 2:55:11 AM    talking to iTunes - track
    > count: 5
    > ....
    >
    > As you can see there is a nearly 25 SECOND delay as the session
    > gets initiated.
    > I have seen the delay stated before the 'fixed index'...
    > but it always happens AND
    > it always happens only once and
    > it only happens at the start of the first time thru the loop.
    > It seems to me to be related to the first command given to the
    > iTunes tell block....
    >
    > I have tried several different approaches but nothing has changed
    > the 25 SECONDS!!
    > (BTW: this is a 1G MacBook running 10.4.10 and XCode 2.4.1)
    >
    > I am not sure what to do next.
    >
    > Does anyone out there have any ideas on what could be causing this?
    >
    > I have not seen anything like this for the other AppleScripts I
    > have developed.
    >
    > Thanks for any help!
    > Steve
  • Folks;

    [Wiping his face somewhat disgustedly]  Hrmmph,  well uh let's see.

    I was running the script cited below in response to a drag event from
    iTunes and I had not returned from -[performDrag ..].
    So iTunes was thrashing on its first task - disposing of the drag
    request-  while I was held out twiddling waiting to get some priority
    on my script.

    Once restructred so my script runs after returning out of -
    [performDrag ..] all is well..

    I do have a question here tho:

    The normal response to a drag out of iTunes is to write an .mp3 file.
    I don't want the files. I can get all I need from the property list
    on promised files.
    I'm using the drag to transmit meta-data not files!!

    So I am grabbing what I need and then returning NO from -
    [performDrag ..] to avoid the unnecessary resource drain of all the
    file handling.
    This may seem disconcerting to the user and it feels like maybe I
    should clarify with a dialog but I don't want the interruption.

    Is there a better resolution?

    Steve

    On Sep 13, 2007, at 3:11 AM, Steve Cronin wrote:

    > Folks;
    >
    > I have a set of applescripts compiled into a .scpt resource which I
    > deploy as part of an application.
    > I have several utility type scripts which I call using something
    > like this:
    >
    > NSDictionary *errorDict;
    > NSAppleEventDescriptor *thisResult;
    > NSAppleScript *myAppleScriptUtilities =  [[NSAppleScript alloc]
    > initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
    > pathForResource:@"ASUtilities" ofType:@"scpt"
    > inDirectory:@"Scripts"]] error:nil];
    > NSString *str = [myOSXUtilities
    > launchApplication:@"com.apple.iTunes"];
    > if ([str isEqualToString:@"True"]) {
    > thisResult = [myAppleScriptUtilities executeAppleEvent:[self
    > callSub:@"applyGoodnessToTunes" params:
    > [NSAppleEventDescriptor descriptorWithString:theGoodness],
    > [NSAppleEventDescriptor descriptorWithString:trackID], nil]
    > error:&errorDict];
    >
    > I have been pretty happy with this setup - I have several different
    > scripts (Finder & Mail) working.
    > Now I have begun to tinker with iTunes.
    >
    > I have logging enabled in the applescripts so that I can debug and
    > then later so the customer can help me debug.
    >
    > I have a simple script which writes some metadata to selected tracks:
    > set startTime to (current date)
    > my writeToLog("Tunes", "-- NEW SESSION --")
    > ...
    > set pidC to length of myPIDList
    > repeat with j from 1 to pidC
    > tell application "iTunes"
    > my writeToLog("Tunes", "talking to iTunes - track count: " &
    > pidC as text)
    > set oldfi to fixed indexing
    > set fixed indexing to true
    > my writeToLog("Tunes", "iTunes fixed indexing set ")
    > ...
    > set fixed indexing to oldfi
    > end tell
    > end repeat
    > set deltaTime to (current date) - startTime
    > my writeToLog("TagTunes", "ready to return - total time: " &
    > (deltaTime as text) & " secs")
    >
    >
    > Now, this snippet rocks when testing in Script Editor.
    >
    > BUT when I run my Cocoa app and call out as above here is actual
    > data from the log:
    > Thursday, September 13, 2007 2:54:47 AM    -- NEW SESSION --
    > Thursday, September 13, 2007 2:54:47 AM    talking to iTunes - track
    > count: 5
    > Thursday, September 13, 2007 2:54:47 AM    iTunes fixed indexing set
    > Thursday, September 13, 2007 2:54:47 AM    top of loop -- track pid:
    > 93DF7AD675F07E3C
    > Thursday, September 13, 2007 2:55:11 AM    track pid access completed:
    > 93DF7AD675F07E3C
    > Thursday, September 13, 2007 2:55:11 AM    ready to apply changes :
    > Everything
    > Thursday, September 13, 2007 2:55:11 AM    talking to iTunes - track
    > count: 5
    > Thursday, September 13, 2007 2:55:11 AM    iTunes fixed indexing set
    > Thursday, September 13, 2007 2:55:11 AM    top of loop -- track pid:
    > 93DF7AD675F07E40
    > Thursday, September 13, 2007 2:55:11 AM    track pid access completed:
    > 93DF7AD675F07E40
    > Thursday, September 13, 2007 2:55:11 AM    ready to apply changes :
    > Eight Easy Steps
    > Thursday, September 13, 2007 2:55:11 AM    talking to iTunes - track
    > count: 5
    > ....
    >
    > As you can see there is a nearly 25 SECOND delay as the session
    > gets initiated.
    > I have seen the delay stated before the 'fixed index'...
    > but it always happens AND
    > it always happens only once and
    > it only happens at the start of the first time thru the loop.
    > It seems to me to be related to the first command given to the
    > iTunes tell block....
    >
    > I have tried several different approaches but nothing has changed
    > the 25 SECONDS!!
    > (BTW: this is a 1G MacBook running 10.4.10 and XCode 2.4.1)
    >
    > I am not sure what to do next.
    >
    > Does anyone out there have any ideas on what could be causing this?
    >
    > I have not seen anything like this for the other AppleScripts I
    > have developed.
    >
    > Thanks for any help!
    > Steve
    >
    >
  • On Thu, 13 Sep 2007 03:11:13 -0500, Steve Cronin <steve_cronin...>
    said:
    > Folks;
    >
    > I have a set of applescripts compiled into a .scpt resource which I
    > deploy as part of an application.
    > I have several utility type scripts which I call using something like
    > this:
    >
    > NSDictionary *errorDict;
    > NSAppleEventDescriptor *thisResult;
    > NSAppleScript *myAppleScriptUtilities =  [[NSAppleScript alloc]
    > initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
    > pathForResource:@"ASUtilities" ofType:@"scpt"
    > inDirectory:@"Scripts"]] error:nil];
    > NSString *str = [myOSXUtilities launchApplication:@"com.apple.iTunes"];
    > if ([str isEqualToString:@"True"]) {
    > thisResult = [myAppleScriptUtilities executeAppleEvent:[self
    > callSub:@"applyGoodnessToTunes" params:
    > [NSAppleEventDescriptor descriptorWithString:theGoodness],
    > [NSAppleEventDescriptor descriptorWithString:trackID], nil]
    > error:&errorDict];
    >
    > I have been pretty happy with this setup - I have several different
    > scripts (Finder & Mail) working.
    > Now I have begun to tinker with iTunes.
    >
    > I have logging enabled in the applescripts so that I can debug and
    > then later so the customer can help me debug.
    >
    > I have a simple script which writes some metadata to selected tracks:
    > set startTime to (current date)
    > my writeToLog("Tunes", "-- NEW SESSION --")
    > ...
    > set pidC to length of myPIDList
    > repeat with j from 1 to pidC
    > tell application "iTunes"
    > my writeToLog("Tunes", "talking to iTunes - track count: " & pidC
    > as text)
    > set oldfi to fixed indexing
    > set fixed indexing to true
    > my writeToLog("Tunes", "iTunes fixed indexing set ")
    > ...
    > set fixed indexing to oldfi
    > end tell
    > end repeat
    > set deltaTime to (current date) - startTime
    > my writeToLog("TagTunes", "ready to return - total time: " &
    > (deltaTime as text) & " secs")
    >
    >
    > Now, this snippet rocks when testing in Script Editor.
    >
    > BUT when I run my Cocoa app and call out as above here is actual data
    > from the log:
    > Thursday, September 13, 2007 2:54:47 AM -- NEW SESSION --
    > Thursday, September 13, 2007 2:54:47 AM talking to iTunes - track
    > count: 5
    > Thursday, September 13, 2007 2:54:47 AM iTunes fixed indexing set
    > Thursday, September 13, 2007
    > 2:54:47 AM top of loop -- track
    > pid: 93DF7AD675F07E3C
    > Thursday, September 13, 2007
    > 2:55:11 AM track pid access
    > completed: 93DF7AD675F07E3C

    You're not supplying enough information. The long delay is between the "top
    of loop" log message and the "track pid access completed" message. But your
    code, as shown, does not generate any "top of loop" or "track pid access
    completed" message so it's impossible to guess what your AppleScript code
    was doing in the meantime. 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 september 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
Go to today