nocheck
is the only way in Praat to do error handling, limited though it may be. If you want to use the GUI programmatically from Python (or any other such), the best way to do error handling is to use nocheck
and then catch errors by looking for the side effects of those commands.
If you are opening a Sound
, you can do assert numberOfSelected("Sound")
or something like that (with more or less elegant tests). If you are writing something to disk, you can use fileReadable()
to see if the file was created.
Alternatively, if you are not actually using the GUI, you can bypass sendpraat
entirely and use Praat through the console (with versions before 6.0 a different binary called praatcon
was needed for Windows, but more recent versions use the same program with the --run
option).
You won't be able to pass commands directly to it, but you can encapsulate those commands into scripts and then just do subprocess.call('praat --run path/to/my/script.praat arguments')
or something like that. Then you might be able to catch errors in that script (= your commands) using Python, or implement the same manual error checking as above.
Update: an example
Here's an example (in Perl, and in Linux, but you get the idea):
#!/usr/bin/env perl
use Capture::Tiny ':all';
use Try::Tiny;
try {
($stdout, $stderr, $exit) = capture {
system( 'praat', '--run', '~/stdout.praat' );
}
}
catch {
chomp;
warn "It died: $_";
};
print "STDOUT:\n$stdout\n";
print "STDERR:\n$stderr";
And the contents of stdout.praat
:
abc$ = "abcde"
num$ = "0123456789"
writeInfoLine: abc$
assert selected("Sound") ; Fail
writeInfoLine: abc$, num$ ; Won't run
The output:
user@linux:~$ perl stdout.pl
STDOUT:
abcde
STDERR:
Error: No Sound selected.
Formula not run.
Script line 4 not performed or completed:
« assert selected("Sound") ; Fail »
Script “/home/user/stdout.praat” not completed.
Praat: command file “/home/user/stdout.praat” not completed.
Update: try / catch in Praat
Since I wrote this answer, I've managed to implement a very rudimentary version of a try/catch procedure for Praat. With it, a script like
include path/to/try.proc
writeInfoLine: "Before fail"
call try
... abc$ = "abcde" \n
... num$ = "0123456789" \n
... Create Sound as pure tone: "tone", \n
... ... 1, 0, 0.4, 44100, 440, 0.2, 0.01, 0.01 \n
... assert selected("TextGrid") ; Fail \n
... Remove ; Won't run \n
if try.catch
appendInfoLine: "Failed!"
endif
will execute without crashing, leaving behind the Sound that was created because the line where it is removed did not run.
The procedure is available in the utils plugin distributed through CPrAN, and is implemented here.