Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for do shell script to finish

I'm trying to create an app in Xcode using Applescript to convert PDF's.

The problem is that I need Xcode/AppleScript to wait for each 'do shell script' command to finish processing before it starts the next.

Is there a way of detecting that each script has finished?

e.g.

tell application "System Events"
        do shell script "/opt/local/bin/convert task1.tif output1.pdf"
    end tell

tell application "System Events"
        do shell script "/opt/local/bin/convert task2.tif output2.pdf"
    end tell

Thanks, Anthony

like image 695
user2862680 Avatar asked Nov 22 '13 13:11

user2862680


People also ask

Does a shell script wait for command to finish?

The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.

What does wait do in bash script?

await is a new operator used to wait for a promise to resolve or reject. It can only be used inside an async function. Promise. all returns an array with the resolved values once all the passed-in promises have resolved.

How do I wait for a file in the shell script?

We use process substitution, <(inotifywait –m –q –e create –-format '%f' ${directory}), to feed the output of inotifywait. Other files might be created in the watched directory while inotifywait waits for the file passed as the input. So, we use read in a while loop to continue waiting for the input file.

How do I use shell wait?

wait is typically used in shell scripts that spawn child processes that execute in parallel. To illustrate how the command works, create the following script: #!/bin/bash sleep 30 & process_id=$! echo "PID: $process_id" wait $process_id echo "Exit status: $?"


1 Answers

First, remove your "do shell script" lines from the "System Events" blocks of code. System Events is not needed for the "do shell script" command.

Next, you can force applescript to not wait for a shell command to finish and also get the PID (process id) of the command back in applescript. For example, run this command.

do shell script "/bin/sleep 5"

You will see that it is a basic 5 second delay command and applescript will wait those 5 seconds before returning control to the script.

Now run this command.

do shell script "/bin/sleep 5 > /dev/null 2>&1 & echo $!"

You will see that now applescript doesn't wait 5 seconds and you also get the PID of the running sleep command. Notice the stuff we added to the end of the original command to make that happen.

We can use this PID. We can make a repeat loop and check if the PID is still running using the "ps" command. When the sleep command finishes the PID will no longer exist and we can use that as a trigger to know when the sleep command has finished.

set thePID to do shell script "/bin/sleep 5 > /dev/null 2>&1 & echo $!"

repeat
    do shell script "ps ax | grep " & thePID & " | grep -v grep | awk '{ print $1 }'"
    if result is "" then exit repeat
    delay 0.2
end repeat

return "the process is complete!"

So that should give you the tools to accomplish your goal. Good luck.

like image 112
regulus6633 Avatar answered Oct 16 '22 23:10

regulus6633