Waiting for one perl script to finish before running another - Applescript

Hi all

I am running two terminal scripts from an Applescript like this:

--tell application "Terminal"
--do script perlscript 1
--end tell
--tell application "Terminal"
--do script perlscript 2
--end tell

Problem is perlscript 1 takes a long time so perlscript 2 is running before perlscript 1 has finished!

How do I get perlscript 2 to WAIT until perlscript1 has finished running?



G5 Mac OS X (10.4.8)

Posted on Feb 16, 2007 11:01 AM

Reply
12 replies

Feb 16, 2007 4:20 PM in response to Michael Conniff

Michael thanks for the suggestion but in fact that is exactly what I had before I realised this problem was occurring so I then tried to separate the two steps by ending the tell block after the first step.

You try it for yourself - have one perl script taking a longer time to run (something like printing out every number from 1 to 1000000) and the other one taking a very short time to run (like a helloworld) - and then call them one after the other from an applescript, something like:

tell application "Terminal"
do script "
cd /Users/bis/Desktop/ArchiveFolder
perl longscript.pl"
do script "
cd /Users/bis/Desktop/ArchiveFolder
perl shortscript.pl"
end tell

Now you would have thought it would finish running longscript.pl before running shortscript.pl in the same terminal window right?

Wrong.

It starts running longscript.pl in one window, then it opens another window and runs shortscript.pl before longscript.pl has finished running!

Well essentially what I have is an Applescript which

1) copies some text and xml files to certain locations
2) calls perlscript 1 to tidy up some of the formatting in the files
3) calls perlscript 2 to convert some of the tidied up files into other formats (such as csv) and
4) sends off files of different formats to different destinations using ftp etc.

1) and 4) work fine but 2) and 3) are what is causing this problem (or, rather, 3 finishes running while 2 is still running). I need the applescript as the "wrapper" to be able to call these other scripts and routines. All I really want to do is to say in applescript:

Finish step 2) BEFORE running step 3).

I have tried quitting the terminal, closing windows, etc. after step 2 but it always seems to run step 3 before finishing step 2.

So I end up with a situation where the wrapper Applescript has finished running but step 2) is still continuing to run in the terminal!

Feb 16, 2007 5:10 PM in response to hacktivist_learner

Well I don't know much about perl either, so that makes me uniquely qualified 🙂

This is what I would do. Make both your perl scripts regular shell scripts with a first line of #!/usr/bin/perl, and make sure they have the execute bit set. Then forget about "tell" and just do:

do shell script "cd /Users/bis/Desktop/ArchiveFolder; longscript.pl; shortscript.pl"

You may have to redirect any output with a "2>&1" to get all the output you would get in Terminal.

You could do something fancy like touch alldone at the end of script 1, then have script 2 test for the existence of file "alldone", but the plain sequencing above should do it.

Other than that, you may find more help in the Applescript forums.

Feb 16, 2007 5:24 PM in response to Michael Conniff

There might be a reason to "tell Terminal", in which case you could create a shell wrapper script that executed the two Perl scripts in sequence. So your AppleScript would be something like:

tell application "Terminal"
do script "/Users/yourname/perlwrapper.sh"
end tell

And then shell script /Users/yourname/perlwrapper.sh would contain:

#!/bin/bash
perl /Users/yourname/longscript.pl
perl /Users/yourname/shortscript.pl
exit

You'd have to "chmod +x perlwrapper.sh" to make it executable before running.

But the AppleScript forum might be the place to ask how to "tell Terminal" to do two things in sequence.

Feb 16, 2007 5:52 PM in response to hacktivist_learner

Hi hacktivist_learner,
I had no idea that "do script" ran the scripts asynchronously and the choice surprises me greatly. I don't use the command, preferring "do shell script." Because "do shell script" can save the output of the command, it must run synchronously. Thus, you could put them in a pair of "do shell script" commands.

If you have a compelling reason for using "do script", you could put both Perl script invocations in the same "do script" command, like so:

do script "cd /Users/bis/Desktop/ArchiveFolder; perl longscript.pl; cd /Users/bis/Desktop/ArchiveFolder; perl shortscript.pl"

Of course you can put them on different lines as you did and still put them in the same "do script" command. If after following one of these suggestions the Perl scripts still run at the same time then the problem could be that the first Perl script returns before it finishes its work, spawning a subprocess.
--
Gary
~~~~
The 357.73 Theory:
Auditors always reject expense accounts
with a bottom line divisible by 5.

Feb 16, 2007 6:06 PM in response to Ken Nellis

Thank you very much it works 🙂 I was thinking of joining the two perl scripts into one but that would have been a lot harder and less elegant. But I will take your advice and ask the Applescript forum about how to tell terminal to run scripts in sequence. I guess, since my applescript is passing parameters from the applescript to the perlscripts, what i really want to know is how to pass parameters back from the perl script to the applescript, once i know this i can wait till those parameters are passed before running the next step in the sequence.

hope that makes sense thanks again 🙂

G5 Mac OS X (10.4.8)

Feb 17, 2007 6:54 AM in response to hacktivist_learner

Hello,

I think you might just need something like the following:

do shell script "
perlscript 1
perlscript 2
"

Maybe it's bad form, but I've found that you can leave out the tell application "Terminal" block. I've never used it and all my do shell scripts work fine. In fact the use of two separate tell blocks, might have been the source of your problem. But that's just a guess.

--
Cole



15 PB Mac OS X (10.3.9)

Feb 18, 2007 2:12 AM in response to Cole Tierney

In fact the use of two separate tell blocks, might have been the source of your problem. But that's just a guess.

You have hit the nail on the head Cole! When I use ONE tell block as in

tell application "Terminal"
do script "cd /Users/bis/Desktop/ArchiveFolder; perl format3.pl;
cd /Users/bis/Desktop/ArchiveFolder; perl format2.pl"
end tell


it works fine but when I use TWO tell blocks as in

tell application "Terminal"
do script "cd /Users/bis/Desktop/ArchiveFolder; perl format3.pl; "
end tell
tell application "Terminal"
do script "cd /Users/bis/Desktop/ArchiveFolder; perl format2.pl"
end tell


the same old problem rears its asynchronous head!

G5 Mac OS X (10.4.8)

Feb 18, 2007 12:31 PM in response to Michael Conniff

Hi Michael - it is true you also suggested using only one tell block and I am sorry I was not able to act on your suggestion. I thought you were suggesting I should use something like:

tell application "Terminal"
do script "
cd /Users/bis/Desktop/ArchiveFolder;perl longscript.pl"
do script "
cd /Users/bis/Desktop/ArchiveFolder;perl shortscript.pl"
end tell


which does not work (i.e. the short script runs before the long script has finished running).

Had I known that you were suggesting something along the lines of:

tell application "Terminal"
do script "cd /Users/bis/Desktop/ArchiveFolder; perl format3.pl;
cd /Users/bis/Desktop/ArchiveFolder; perl format2.pl"
end tell


which does work (the short script waits for the long one to finish before running) then of course you should have been credited with the solution to my problem.

So put it down to a newbie inability to understand the subtleties of your argument! Had it not been for your initial willingness to help out and identify the contours of the problem, none of the other responses and solutions might have been forthcoming so thank you 🙂

G5 Mac OS X (10.4.8)

G5 Mac OS X (10.4.8)

G5 Mac OS X (10.4.8)

G5 Mac OS X (10.4.8)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Waiting for one perl script to finish before running another - Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.