Skip to content

Instantly share code, notes, and snippets.

@psibre
Last active August 29, 2015 14:10
Show Gist options
  • Save psibre/e9bac46ca995aa85c2d6 to your computer and use it in GitHub Desktop.
Save psibre/e9bac46ca995aa85c2d6 to your computer and use it in GitHub Desktop.
Poll TextGrid editor in Praat externally

Poll Praat TextGrid Editor

Getting the editor info (visible window, selection start and end, etc.) in a Praat script is easy. But say you want to do this continuously to log the state of your editor window into a text file while you're interacting with it. The problem is that Praat doesn't seem to have a sleep or timeout command to pause execution of the script for a given amount of time, then continue without user interaction.

One hypothetical solution might be a Praat script that runs a system command such as sleep (or timeout on Windows), but the Praat UI blocks while the script is running, which is a dealbreaker.

Another solution is to poll from an external thread such as a shell script that can sleep or timeout natively. This approach requires sending the corresponding commands to a running graphical Praat, which can be done using the sendpraat program.

Note that a precompiled executable is only available for Windows; Linux and OSX need to compile from source (but the source itself provides the incantation).

On Linux or OSX, the following pollpraat.sh script can do what we want, i.e., append the info from an open TextGrid editor to a text file named praat.log, once every second, until the shell script is terminated. It must be called with the name of the TextGrid, e.g., ./pollpraat.sh MyTextGrid.

#!/usr/bin/env bash
while true
do
	./sendpraat praat \
	"editor TextGrid $1" \
	"info$ = Editor info" \
	"info$ >> 'shellDirectory$'/praat.log"
	sleep 1
done

For Windows (7 or later), an equivalent pollpraat.cmd script could be,

@echo off
:loop
	sendpraat.exe praat ^
	"editor TextGrid %1" ^
	"info$ = Editor info" ^
	"info$ >> 'shellDirectory$'/praat.log"
	timeout /t 1 /nobreak >nul
goto loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment