Amisha Mehta
Amisha Mehta

Reputation: 31

Make Shell script to wait until applescript finishes?

I have a shell script which calls an AppleScript. AppleScript runs some automated tests on a software with a given document.

After the AppleScript finishes its execution I am moving the document to some other folder using mv command in my shell script and then I am moving the next document so that AppleScript can run those tests with this new document. But as soon as AppleScript is called it moves the document to another folder without those tests being run on that document.

How can I give a wait in my shell script so that the document is moved only after the AppleScript has finished executing all the tests?

1. mv $file "/path/to/file" (Moving the document to the execution folder)
2.  osascript /pathto/applescript.app (it will use this execution folder to run its tests)
3.  mv "/path/to/file" /Users/Desktop/tempfolder (moving the document on which the test is completed to a temp folder)

Steps 2 and 3 go one after another without wait, hence the document on which the test is to be run is moved before the test completes.

Upvotes: 2

Views: 1041

Answers (2)

pbell
pbell

Reputation: 3105

As Mayerz said, do shell script command is waiting for end of instruction to move to next AppleScript command. In any case, you can also use the "considering application response" bloc. Here is an example with a move in Finder :

set A to ("myVolume:Users:Me:Documents:sample.mov") as alias
set B to ("Users:me:Desktop:") as alias
tell application "Finder"
    considering application responses
        move A to B
    end considering
end tell
display dialog "done"

Similar example with do shell script:

set A to ("myVolume:Users:Me:Documents:sample.mov") as alias
set B to path to desktop
set AU to POSIX path of A
set BU to (POSIX path of B) & "test2.mov"
tell application "Finder"
    considering application responses
        do shell script "mv " & (quoted form of AU) & " " & (quoted form of BU)
    end considering
end tell
display dialog "done"

Upvotes: 0

user1585121
user1585121

Reputation:

Normally, shell commands are executed in-sequence. That is, the next statement is not run until the current one is done executing, whether or not it was successful. So if this is the case you can merely write your script one command after another.

I guess that osascript /pathto/applescript.app run some tasks in a different process, and that osascript returns from its own execution while the process doing "what you want" is still running.

What you could do would be to ps -aux | grep applescript.app then get the pid from that, and do a while loop afterward to see when the program cease. But that seem overengineered and you don't know the execution name of the applescript.app.

If I am wrong somewhere please anybody correct me

Upvotes: 0

Related Questions