How to wait for a shell script to complete?

I have a function I’d like to run, but only after a shell script runs. This shell command has no set time, and I also don’t want to freeze other processes during this wait. Anyone know how to do this?

do shell script "somecommand"
-- Something Here
my function()

I thought do shell script always ran syncronously.

Try to make sure your script returns something to stdout and assign the result of the do shell script to a variable.

E.g.

set x to do shell script “ls”

this is a bit dirty but you could do it

tell application "Terminal"
	do script "sh"
	repeat until window 1 is not busy
		delay 4
	end repeat
end tell

I have considered that, but this command is inside an actual application, and I don’t want the terminal to launch.

The command is “open -a /Applications/iTunes.app ~/Backup/Music/*”.

So I guess a more appropriate question is to find out how to determine wether iTunes is busy or not.

Tip: query iTunes for something like the ID of window 1, in a repeat loop like that.

As far as I have seen, busy is not an attribute of iTunes :confused:

Ohhhh… I see. The shell script terminates because open terminates as soon as it hands off to launch services.

Sometimes in a script I’ll check if an application is in the process list, and then try to hit it with an event like getting it’s version or something. If that times out, I figure it’s hung, kill it, and restart.

I never really worry about whether it’s finished restarting though, because it always seemed that applescript took care of that for me. For example,

do shell script “open /Applications/iTunes.app”
tell application “iTunes”
set x to version
end tell

Results in…

tell current application
do shell script “open /Applications/iTunes.app”
“”
end tell
tell application “iTunes”
get version
current application
get version
“6.0.1”
end tell

So it seems like applescript keeps hitting iTunes until it gets back an answer. (In this case, it took two tries.)

If you have to wait for iTunes to complete startup before taking some action, perhaps you could just hit it with something like the set x to version command which shouldn’t let the script continue unless iTunes sends back an answer or the event times out?

this is maybe a weird way around it but would an option of call in an applescript from a shell script be a possibility ?
#!/bin/bash

command && osascript -e ’ tell application “iTunes” to do something ’