• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Timing issues with Java executing AppleScript calling iTunes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Timing issues with Java executing AppleScript calling iTunes


  • Subject: Timing issues with Java executing AppleScript calling iTunes
  • From: Zuhayr Khan <email@hidden>
  • Date: Thu, 28 Feb 2008 07:20:42 +0000

Hi,

I'm cross-posting this to AppleScript and JavaDev as I'm not sure where my issue lies.

I'm writing an application that needs to have iTunes import a file into it's library.

The simple script I started with...

tell application "iTunes"
open "Macintosh HD:Users:zuhayrkhan:Music:Test.mp3"
end tell

...worked, but ended up playing the track - this isn't what I wanted.

I was hoping I could just add a "stop" call - but because open seems to be asynchronous; this doesn't work.

I was also hoping that I could maybe register a handler that iTunes would invoke once the open was complete - but I can't find anything like that.

I searched on the Internet for solutions and came across a number of similar issues that people had solved with a repeat/until style loop checking some internal state.

So I implemented this testing iTunes' "player state" (see below), and it worked fine (from within Script Editor anyway).

However; when compiling and invoking the script through Java - I get some very strange results - you'll see from the code that I have a lot of debug in there to see what's going on. I've seen situations where I trigger the script from Java that seem to take a random time to complete and also seem to be influenced by whether iTunes is activated or not.. but then subsequent invocations work fine. 

There seems to be little predictability to the behavior. With reference to the script at the bottom of this post, please see the example invocations below:

When run from within Script Editor:

06:10:56 about to tell iTunes
06:10:56 about to open file
06:10:56 issued open file
06:10:56 about to get playerState
06:10:56 got playerState:stopped
06:10:56 is stopped
06:10:56 about to delay:stopped
06:10:57 done delay:stopped
06:10:57 playerState now:playing
06:10:57 about to stop

... when run from within Java:

2008-02-28 06:14:27,491 [child-of-btpool0-1] INFO  executing script
2008-02-28 06:14:39,982 [child-of-btpool0-1] INFO  [result=
06:14:27 about to tell iTunes
06:14:27 about to open file
06:14:27 issued open file
06:14:27 about to get playerState
06:14:27 got playerState:stopped
06:14:27 is stopped
06:14:27 about to delay:stopped
06:14:34 done delay:stopped
06:14:36 playerState now:playing
06:14:37 about to stop ]

As far as I can see (and through searches on the Internet), the Java I'm using to invoke the AppleScript shouldn't be a problem:

        LOG.info("about to execute [script=" + script + "]");

        NSAppleScript myScript =
                new NSAppleScript(script);

        NSMutableDictionary errors =
                new NSMutableDictionary();

        LOG.info("executing script");

        NSAppleEventDescriptor results =
                myScript.execute(errors);

        int errorCount = errors.count();
        if (errorCount > 0) {
            throw new AppleScriptErrorException(errors);
        }

        return results.stringValue();

If anyone has any experience of this kind of behavior or has any thoughts on what might be going on, I would greatly appreciate it. If there's any more detail I can give, please let me know.

Thanks in advance,

Zuhayr Khan

AppleScript:

set debug to "
"
set debug to debug & time string of (current date) & " about to tell iTunes
"
tell application "iTunes"
set debug to debug & time string of (current date) & " about to open file
"
open "Macintosh HD:Users:zuhayrkhan:Music:Test.mp3"
set debug to debug & time string of (current date) & " issued open file
"
with timeout of 1 second
set debug to debug & time string of (current date) & " about to get playerState
"
set playerState to player state as string
set debug to debug & time string of (current date) & " got playerState:" & playerState & "
"
if (playerState = "stopped") then
set debug to debug & time string of (current date) & " is stopped
"
repeat until playerState = "playing"
set debug to debug & time string of (current date) & " about to delay:" & playerState & "
"
delay 0.1
set debug to debug & time string of (current date) & " done delay:" & playerState & "
"
set playerState to player state as string
set debug to debug & time string of (current date) & " playerState now:" & playerState & "
"
end repeat
end if
set debug to debug & time string of (current date) & " about to stop "
stop
end timeout
return debug
end tell

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Timing issues with Java executing AppleScript calling iTunes
      • From: Axel Luttgens <email@hidden>
  • Prev by Date: Re: osascript: application isn't running. huh?
  • Next by Date: Re: Timing issues with Java executing AppleScript calling iTunes
  • Previous by thread: Re: linebreak in a shell script; was Re: What's wrong with this call to zip?
  • Next by thread: Re: Timing issues with Java executing AppleScript calling iTunes
  • Index(es):
    • Date
    • Thread