AS Studio: Get control back after starting a shell command?

Hi everyone,

I have written an AS Studio GUI wrapper for a C command-line tool that processes a video file.

If I use “do shell script” in my AS Studio code, it of course hangs until the shell script finishes.

If I use the construct


tell application "Terminal" to do script theShellScript

Then my AS Studio code gets control back immediately and I could then do a lot of things the GUI needs to do, such as look for a Cancel button being pressed, dim the other buttons that are not meaningful during processing, etc.

The only non-elegance to this approach is that the Terminal.app opens a window.

Is there a recommended way to get this functionality (essentially forking off another process to run the command-line tool) without using “do script” to Terminal.app? For example, any generic “app launchers” in BSD?

I just need some app to be able to do a Tell statement to. “do shell script” doesn’t work for this purpose since it waits for the shell script to finish.

Thanks for any advice anyone can give.

Happy Holidays

Johnny

Take a look at Technical note TN2065: do shell script in Applescript http://developer.apple.com/technotes/tn2002/tn2065.html#TNTAG1. Look for

.

Thank you so much! That works beautifully! Since my AS Studio app isn’t hung up, I can enable a “Cancel” button which the user can use to kill the background process and quit the main app.

Now I would like to see if there is a way for my AS Studio app to know when the binary has finished with the file.

I have the source to my binary that does the decoding of the file - it is straight C, so if there is a way for it to signal my AS app that would be the most elegant solution.

Barring that, I see various ways to poll various aspects of the binary’s state (output file size unchanging, PID exists in ps -aux, etc.) but it appears that “On Idle” does not work in an AS Studio app. to do periodic polling.

Is there some code I could add to the end of my C app to have it somehow signal my AS Studio process that it is finished? I would like to be able to turn off the progress bar and notify the user in the GUI that the file is finished processing.

Thank you again for the excellent suggestion.

Johnny

‘on idle’ should work for you - I use it for the same purposes all the time. Don’t forget to include ‘return ’ at the end of the ‘on idle’ handler to specify the polling time.

Yes, I had thought that on idle did not work in a Studio app, but it does.

What I wound up doing is using the on idle handler to do a

if "tivodecode" is not in (do shell script "ps -acx") then

construct. This returns TRUE when the background process completes. The idle handler polls every 3 seconds.

I also had to do a

tell application "Finder"
		update theFinderOutputFilePath
		set theOutputFileSize to size of theFinderOutputFilePath
	end tell

To make the progress indicator be accurate, as apparently the “size of” Finder property/command only works on the updated size, so the Finder “update” command was needed.

I’d still like to know what system command or Cocoa method to call in my C app that would be able to send a signal or an Apple Event to my Applescript Studio app, informing it that it was finished with the conversion. That would be the most elegant solution.