nocheck 是 Praat 中进行错误处理的唯一方法,尽管它可能是有限的。如果您想通过 Python(或任何其他方式)以编程方式使用 GUI,进行错误处理的最佳方法是使用 nocheck,然后通过查找这些命令的副作用来捕获错误。
如果您要打开 Sound,您可以执行 assert numberOfSelected("Sound") 或类似的操作(或多或少优雅的测试)。如果您正在向磁盘写入内容,您可以使用fileReadable() 查看文件是否已创建。
或者,如果您实际上使用 GUI,您可以完全绕过 sendpraat 并通过控制台使用 Praat(对于 6.0 之前的版本,Windows 需要一个名为 praatcon 的不同二进制文件,但更新的版本使用带有--run 选项的相同程序。
您将无法直接将命令传递给它,但您可以将这些命令封装到脚本中,然后执行subprocess.call('praat --run path/to/my/script.praat arguments') 或类似的操作。然后,您可能能够使用 Python 捕获该脚本(=您的命令)中的错误,或者实现与上述相同的手动错误检查。
更新:一个例子
这是一个例子(在 Perl 和 Linux 中,但你明白了):
#!/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";
还有stdout.praat的内容:
abc$ = "abcde"
num$ = "0123456789"
writeInfoLine: abc$
assert selected("Sound") ; Fail
writeInfoLine: abc$, num$ ; Won't run
输出:
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.
更新:Praat 中的 try / catch
自从我写了这个答案,我已经设法实现了一个非常基本的a try/catch procedure for Praat 版本。有了它,像
这样的脚本
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
将在不崩溃的情况下执行,留下由于删除它的行没有运行而创建的声音。
该过程在通过CPrAN分发的utils插件中可用,并在here实现。