Anna-Lischen
Anna-Lischen

Reputation: 878

How to send a command using AppleScript to terminal one by one and save the output, which is not writable to file anywhere?

So, I have a problem. I have downloaded a program from the web. And it's a command line app. I have written a code, which generated some n-k commands to the app. I have written them into an output file. I can write an app in Python, but it freezes on some of the commands. I have tested them manually and seems like there are two issues:

  1. Commands must be run one-by-one;
  2. Some of the commands give an output like bla-bla-bla, this thing is not written into an output file. So, if I run a command ./app -p /file1 -o /file2 -s -a smth- > /fileOutput.txt The fileOutput.txt is empty, though in the terminal, there's is this bla-bla-bla message, stating, that something is wrong. If the command gives bla-bla-bla the app may freeze for a while.

Here is what I want to do:

  1. CD into folder, the containing app;
  2. For command in fileWithCommands perform command and start the next, only when the previous finishes;
  3. If the command gives message, containing bla-bla-bla (cause it may look like file1 bla-bla-bla), write the command and this strange output into file badOutputs.txt.

Have never done applescript before. However, this's what I've done so far:

set theFile to "/Users/MeUser/Desktop/firstCommand"
set fileHandle to open for access theFile
set arrayCommand to paragraphs of (read fileHandle)
#I have found the previous code here: http://alvinalexander.com/mac-os-x/applescript-read-file-into-list-array-examples
close access fileHandle
tell application "Terminal"
    activate
    do script "cd /Users/MeUser/Desktop/anApp/"
    repeat with command in arrayCommand
        do script command
    end repeat
end tell

Though there's a problem, if in one window the commands make up a huge queue. Without window 1 cd and the command are in different windows. And I am still unable to save the output.

UPDATE

Did with accordance to @Mark Setchell's recommendations. So now I have such code:

set theFile to "/Users/meUser/Desktop/firstCommand"
set fileHandle to open for access theFile
set arrayCommand to paragraphs of (read fileHandle)
close access fileHandle
repeat with command in arrayCommand
    do shell script "cd /Users/meUser/Desktop/App/; " & command
end repeat

To the command I have added the following:

2>&1 /Users/meUser/Desktop/errorOut.txt

However, the apple script says that a mistake of the app is the mistake of the script. I.e.: file corrupted, app fails. I want it to write into error file where has it failed and move to the next command, while the script just fails.

Upvotes: 0

Views: 565

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207718

Maybe not a complete solution, but more than a comment and easier to format this way...

First Issue

Your command-line app which writes on the Terminal may be writing to stderr rather than stdout. Try redirecting stderr to the same place as stdout by using

./app -p ... > /FileOutput.txt 2>&1

Second Issue

You cannot do:

do shell script cd somewhere
do shell script do_something

because each do shell script will execute in a separate, unrelated process. So your first process will start - in the default directory like all processes - and correctly change directory and then exit. Then your second process will start - in the default directory like all processes - and try to run your command. Rather than that, you can do this:

do shell script "cd somewhere; do_something"

which starts a single process which changes directory and then runs your command line program there.

Issue Three

Why do you want to send your commands to Terminal anyway? Does the user need to see something in Terminal - seems unlikely because you want to capture the output, don't you? Can't you just run your commands using do shell script?

Issue Four

If you want to keep your normal output separate from your error output, you can do:

./app ... params ... > OutputFile.txt 2> errors.txt

Suggestion 1

You can retain all the errors from all the scripts and accumulate them in a single file like this:

./app .. params .. >> results.txt 2>&1

That may enable you to deal with errors separately later.

Suggestion 2

You can capture the output of your shell script into an Applescript variable, say ScriptOutput, like this, then you can parse it:

set ScriptOutput to do shell script "..."

Suggestion 3

If errors caused by your script are stopping your loop, you can enclose them in a try block like this so they are handled and everything continues:

try
   do shell script "..."
on error errMsg
   display dialog "ERROR: " & errMsg
end try

Upvotes: 1

Related Questions