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




On Oct 17, 2007, at 5:40 PM, Doug Zwick wrote:


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 think the draining thing might depend on ordering as well. If a process does not deadlock without two threads it probably never will. I have simple Runtime exec code and more complicated where the deadlock is a concern. The simple can usually just be inserted as a method in a the class...


e.g.

	public boolean isExecutable(String execpath) {
		String[] rtargs = new String[] { "ls","-al",execpath };
		String lsout = rtexec(rtargs);
		String executable = lsout.substring(3,4);
		return executable.equals("x");
	}
	
	public boolean setExecutable(String executable) {
		String[] rtargs = new String[] { "chmod","555",executable };
		String chmodout = rtexec(rtargs);
		return isExecutable(executable);
	}
	
	private String rtexec(String[] args) {
		try {
			StringBuffer execout = new StringBuffer();
			Process proc = Runtime.getRuntime().exec(args);
			proc.waitFor();
			InputStream inout = proc.getInputStream();
			InputStream inerr = proc.getErrorStream();
			byte []buffer = new byte[256];
			while (true) {
				int stderrLen = inerr.read(buffer, 0, buffer.length);
				if (stderrLen > 0) {
					execout.append(new String(buffer,0,stderrLen));					
//					out.write(buffer, 0, stderrLen);
				}
				int stdoutLen = inout.read(buffer, 0, buffer.length);
				if (stdoutLen > 0) {
//					out.write(buffer, 0, stdoutLen);
					execout.append(new String(buffer,0,stdoutLen));
				}
				if (stderrLen < 0 && stdoutLen < 0)
					break;
			}	
			return execout.toString();
		}
		catch(Throwable tossed) { tossed.printStackTrace(); }
		return "-";
	}

You can also get more interactive with Runtime'd osascript if you want. The following is from a java CLI GUI shell. It feeds osascript the input through it's System.in and gets the output from the appropriate stream as well.

osascript
set cd to (random number 100000) mod 52 + 1
return cd
18 Oct 2007 06:29:23,375 INFO - RuntimeExecutable: osascript
18 Oct 2007 06:29:23,379 INFO - -s
18 Oct 2007 06:29:23,379 INFO - s
18 Oct 2007 06:29:23,380 INFO - -e
18 Oct 2007 06:29:23,380 INFO - set cd to (random number 100000) mod 52 + 1
return cd


49

Since the parms get echoed I might be wrong on this feeding the script to osascript through System.in. It probably accumulates them and sends the entire script as a parameter. However, I think I have entered scripts into Terminal using osascript's system in so that should work.
The 49 is the returned result, so this is not completely async anyhow.
Also java alone, even if the process is async, should be able to wait as long as you might want and check for the existence of files.


Mike Hall        hallmike at att dot net
http://www.geocities.com/mik3hall
http://sourceforge.net/projects/macnative



_______________________________________________
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
References: 
 >Re: how can i know from the java code that an applescript completed to run? - urgent (From: Doug Zwick <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.