Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how can i know from the java code that an applescript completed to run? - urgent



Itia Gerbi wrote:

Need some urgent advise here...

I'm trying to invoke an apple script from the java code,

The issue is that the script runs a-synchronically, while I need it to
run synchronically.

I'm using:

Process p = Runtime.getRuntime().exec(i_cmdLine);

retVal = p.waitFor();

the waitFor() returns after the script was started to run,

but it doesn't wait for the for the script to be completed.

I need some indication to know when the apple script completed its work.
(in my case the script is generating some files, so I need to know when
the files where generated)

You will have to post what i_cmdLine contains. When I run the code:

public class TestOSA {
    private static final String[] COMMAND = {
	"osascript",
	"-e",
	"tell application \"Finder\" to return display dialog \"Hello world!\""
    };

    public static void main(String[] args) {
	Runtime rt = Runtime.getRuntime();
	Process p = null;
	try {
	    p = rt.exec (COMMAND);
	    p.waitFor();
	}
	catch (Throwable t) {
	    t.printStackTrace();
	}
	System.err.println ("Done!");
    }
}

it waits for the osascript command to complete.

Also, note that the above is not production-quality code. Whenever one uses Runtime.exec one should always spawn two threads to drain the stout and stderr streams, *especially* when calling Process.waitFor. Failure to do so will result in a deadlock if the process generates more than a couple of hundred bytes of output. I do not see this in your posted code, I assume you removed it for brevity. I also am assuming that your variable i_cmdLine is of type String[], and not just String. Using the scalar String overloading of Runtime.exec can easily get you into trouble when it parses the string differently than what was expected.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.