How to create threads with AppleScript ?

Hi,

How do I create a thread in AppleScript ? I am currently executing some java classes via AppleScript, but the problem is that my classes have multiple threads in execution. If I test this with java, everything works just fine, but the problem with AS, is that it is executing the class and is waiting until the execution, i.e the created thread awaken after 10 seconds; so it is impossible to make something else during this time.

So how can I encapsulate every java execution in a Thread with AppleScript ? :?:

Thanks

Chris

I actually tried the following


ignoring application responses
		do shell script "java -classpath path_to_java_class CategoryModifier "" & catname & "" "
	end ignoring

Even with ignoring application responses around does not change the problem : it stills wait the 10 seconds until the java thread has finished to continue :evil: !! Is it really not possible to create a thread or something equivalent that not waits until the function has completed ?? :idea:

Thanks

Christoph

Unfortunately, the ignoring application responses syntax does not affect scripting addition calls, such as “do shell script.” Two questions:

  1. Is it simply a matter of making multiple calls to this command line tool? This can be accomplished by seperating do shell script commands via either a semi-colon or newline, as mentioned in Apple’s technote tn2065:
do shell script "java ...stuff... ; java ...stuff... ; etc ..."
  1. Do you want a crazy solution? :wink:

ignoring application responses
	tell application "my do shell script 1" to run
end
...stuff...
ignoring application responses
	tell application "my do shell script 2" to run
end
...stuff...

where you’ve created script applications that consist of just the “do shell script” command. The calls to “java” will be very, very slow, because of how long it takes to launch a script application, but the main calling script will chug along without a care in the world.

It might be a good idea for the “do shell script” command to implement a “with no response” parameter.

Oh!!! I just realized: Terminal.app is itself an application. I just ran this:


tell application "Terminal"
	
	launch
	
	ignoring application responses
		
		do script "system_profiler"
		do script "man perl"
		do script "man perl"
		do script "man perl"
		
	end ignoring
	
end tell

It opened up 5 windows, (it shoudn’t have opened the first, blank one, since I used “launch” rather than “activate”). In any case, the three man commands very quickly printed to screen in their seperate windows even though the notoriously slow system_profiler command was finished yet.

The one problem is automatically quiting Terminal when it’s done. Since you’re not getting any feedback, you can’t know when its processes are over, and when you call “quit” before Terminal is done doing its thing, a dialog pops up listing the running processes. Looking in the dictionary, I see that Terminal windows have a “busy” boolean. If your main script is a script application, you could save it as a “stay open” script, with this idle handler:


on idle
	
	tell application "Terminal"
		
		repeat with i from 1 to count of windows
			
			if (busy of window i) then return 1 --> go idle for a second
			
		end repeat
		
		-- if we're here, then all windows were finished
		--
		quit
		
	end tell
	
	-- if we're here, then the stay-open script can itself quit
	--
	quit
	
end idle

The above assumes, of course, that all calls made to Terminal are the kind that execute and then finish, unlike my “man” examples above, which wait for interactive commands from the user.

If you’re working with just a compiled script, (perhaps called via the Script Menu), then the idle handler isn’t available to you. Here is an alternative:


-- ... the main part of your script here ...

-- and now you're all done, so:

tell application "Terminal"
	
	repeat with i from 1 to count of windows
		
		repeat
			
			if (busy of window i) then
				
				delay 1 --> go idle for a second
			else
				exit repeat
				
			end if
			
		end repeat
		
	end repeat
	
	-- if we're here, then all windows were finished
	--
	quit
	
end tell

The thing to do is to append “> /dev/null >&1 &” to your command, as it states in the do shell script technote:

So, this should work:

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

This is actually the final solution I finally found, i.e put the process in background and redirect output and errors to /dev/null :smiley: .

Thanks for your replies :slight_smile:

Chris

That is so cool! Thank, Jon. :slight_smile:

I read that technote a bunch of times, yet somehow that bit didn’t stick in my head. I’m off to go read it again…