how to open praat from a praatscript - praat

I would like to allow a praatscript to open the the praat object line. This would allow me to open praat and then automatically allow the script to load objects into the object window.
for example the script
run_something.praat 1.wav 1.TextGrid 0.1 0.2
could open praat and then open the editor to 0.1 and 0.2 in the audio file "1.wav" < this is easy for me to do
I just can't get praat open beforehand to insert what I need.
right now my script looks like this:
form Info
text Sound
text Textgrid
real Start
real End
endform
if sound$ != "" and textgrid$ != ""
Read from file: sound$
Read Strings from raw text file: textgrid$
#read_lab()
selectObject: 1, 3
View & Edit
editor = 3
editor: editor
Select: start , end
Zoom to selection
endeditor
endif
of course it will tell me that View and Edit does not work because the GUI is not open. I cannot use environments because it has to work on windows and linux

You can start Praat using the --open option to ... open the files you want. This will start Praat in GUI mode, with the specified files (and/or scripts) open:
praat --open sound.wav annotation.TextGrid script.praat
But if you want to issue commands to that instance of Praat programmatically (= without you clicking on anything), you'll likely have to use sendpraat, which should be usable in all the platforms where Praat runs.
Note that the compilation instructions in that page are a bit outdated for Linux at least: I was able to compile on my machine with
gcc -std=gnu99 -o sendpraat -DSTAND_ALONE -DUNIX \
`pkg-config --cflags gtk+-2.0 glib-2.0` \
sendpraat.c \
`pkg-config --libs gtk+-2.0 glib-2.0`
With sendpraat available, you'll have to start a GUI instance of Praat however you prefer, and then, in a separate statement, send the execution of your script:
sendpraat 0 praat \
'runScript: "path/to/your/script", "1.wav", "1.TextGrid", 0.1, 0.2'
Instructions for using sendpraat are available on the Praat manual. The first argument is a timeout in seconds (=die if action is not completed by then), then the name of the program, and then a list of commands to run sequentially.
As far as I know, there is no straightforward way to run a single script that will, in one step, open a GUI version of Praat and make that instance of Praat execute a command. But I might be wrong.

For those who are struggling with sendpraat :
try:
sendpraat praat 'execute "pathtoyourscript"'

Related

Reuse Vim terminal buffer

When working on Bash scripts in Vim 8, I often execute the current buffer in a split terminal with
:terminal %
but the second time I run this command, a new buffer is opened rather than reusing the previous one, resulting in multiple open terminals unless I explicitly close the first one.
Is there a way to have Vim always reuse the same buffer for the :terminal command? A one-liner that I can drop into my .vimrc is preferred.
:help :terminal says:
Open a new terminal window.
and there's AFAIK no way to tell :terminal to reuse an existing window.
You might be able to build your own alternative to :terminal with :help term_start() and the term_opencmd option, though.
If you're willing to go a bit further, you could actually use a combination of tmux with send-keys, vim and the file path rather than the buffer itself.
First you create a tmux session that's preferably running in the same directory as the script will be run:
tmux new-session -s DEBUGSESSION
Then you edit your file called, say, my_script.sh which already needs to have the executable flag.
vim my_script.sh
Then from inside vim, you send to the tmux session the keys to run the file:
:!tmux send-keys -t DEBUGSESSION "%:pCtrl+VCtrl+M"
Yes, you could probably also send the buffer instead of the file but if you have a large file I don't know about the performance.
And yes, this limits you to the whole file (as opposed to the buffer, where you can select a number of lines).
But I still think it could be useful.
Building on my earlier answer, the following commands try to reuse the same terminal every time. They open a new window if the old one does not exist, and they fail if a command is still running in the old window. The window retains its geometry from command to command:
command! -nargs=1 RunInTerminal silent call s:RunInTerminal(<q-args>)
command! -nargs=? MakeInTerminal silent call s:MakeInTerminal(<q-args>)
function s:RunInTerminal(cmd)
let l:options = {'term_name': '!' .. a:cmd}
const l:orig_winid = win_getid()
if win_gotoid(s:term_winid) == 1
let l:options['curwin'] = 1
endif
call term_start([&g:shell, &g:shellcmdflag, a:cmd], l:options)
let s:term_winid = win_getid()
call win_gotoid(l:orig_winid)
endfunction
if !exists('s:term_winid')
let s:term_winid = 0
endif
function s:MakeInTerminal(args)
const l:cmd = expandcmd(&makeprg) .. (a:args == '' ? '' : ' ' .. a:args)
call s:RunInTerminal(l:cmd)
endfunction

emacs macro/function for doing "gcc main.c && ./a.out" does not automatically display output buffer

I'm trying to perform a one-step compile and run operation in emacs. I tried recording a macro using C-(, then M-!, then "gcc main.c && ./a.out", RET, then C-).
However, when I execute this macro with C-x e, the *Shell Command Output* buffer doesn't automatically open if I happen to not have it on my screen at the moment (even though it is in the buffer list, and the output does correctly appear in that buffer). I only see (Type e to repeat macro) on the bottom of my page, which has appeared after the output that I wanted to see, so is in a sense blocking it.
This is a minor annoyance; I would prefer that the Shell Command Output pop up automatically, the way it does when I manually use the shell-command command M-!, rather than requiring me to switch to that buffer manually using C-x b.
I also tried evaluating (call-process "/pathtofile/a.out" ), but that has a similar issue: I need to provide a buffername to output to, and even if I do, the output doesn't automatically get displayed; I have to manually switch to that new buffer. Additionally, call-process appends the output to that buffer, as opposed to refreshing.
How can I easily get the shell command output to show up automatically without manually performing the M-! command?
Update: I found out that recording a macro using M-x compile, then replacing the command with gcc main.c && ./a.out does automatically display the compilation result buffer when the macro is invoked using C-x e. If anyone has any insight to why the previous examples don't automatically display their output, I would welcome any answers.
As an alternative, you can specify compile-command as a buffer-local variable. For example, to compile, run with output to compilation buffer, removing the executable aftwerard, you can add
/* -*- compile-command: "gcc -std=gnu11 file-name.c && a.out && rm a.out" -*- */
to the first line of your file (everything between -*- are buffer local variables delimited by ;). This variable is initialized when the buffer is visited. Then running compile will use this command. This can be a useful to specify other buffer local variables like indentation, etc. in any file/mode (where comments on the first line are acceptable).
It also often makes sense to define compile-command in mode hooks, eg. your c-mode-hook, so your compile commands are generic across major modes.
I solved my problem using an elisp function instead of a macro. Putting the following in ~/.emacs:
(defun c-gcc-and-run ()
"Saves current buffer, runs gcc, and runs ./a.out if compile is successful."
(interactive)
(save-buffer)
(compile (concat "gcc " (buffer-file-name) " && ./a.out")))
(add-hook 'c-mode-hook '(lambda () (local-set-key "\C-c\C-f" 'c-gcc-and-run))
Every time c-mode is opened for the first time, the hook automatically binds \C-c\C-f to c-gcc-and-run, which I defined, and which uses the compile elisp function to perform the desired commands.

How to give interactive inputs in a single line for .exe windows

I am trying to automate one process which involves giving interactive input to a .exe utility. It expects the user-input for each step. I wanted to give all those values at single time while giving that command only. For eg: ./test.exe 8\n1\n0 etc. I have tried multiple ways to give input to the 'test.exe' utility like 8\n1\n0 | ./test.exe and 8,1,2 | ./test.exe. None of these worked. Any help how to pass these options 8,1,2 to the interactive utility test.exe in single line so that it will be helpful for my automation
There is no set way to automate a 3rd party program with Powershell. Not all utilities even offer the ability to do so.
I'd look in the utility documentation for any switches.
Try the following to see if you can get any built in help: test.exe -h, test.exe /h, test.exe /?, test.exe -?
use the sysinternals strings utility to try and find anything that look like command line switches inside the exe that you can take advantage of.
https://technet.microsoft.com/en-us/sysinternals/strings.aspx?f=255&MSPPError=-2147217396
The answer depends entirely on how your executable works.
If the executable reads from standard input, you can redirect input to it as follows (PowerShell):
PS C:\> 8,1,2 | .\test.exe
This won't work if the executable doesn't read from standard input or if it clears the console input buffer.
The executable may also let you provide command-line arguments that specify the needed input, but this depends on the executable.

How can I use script to create a typescript of my terminal session?

I should compile and run my file and record it with script in a typescript. But when I try it I get unexpected results in the file. Am I doing something wrong?
$ script
Script started, file is typescript
developer#developer-VirtualBox:~/Desktop/kth/os/smallshell/oslab$ gcc -Wall -O4 -ansi -pedantic miniShell.c
developer#developer-VirtualBox:~/Desktop/kth/os/smallshell/oslab$ exit
exit
Script done, file is typescript
developer#developer-VirtualBox:~/Desktop/kth/os/smallshell/oslab$ more typescript
Script started on Mon 18 May 2015 06:38:38 CEST
developer#de
-ansi -pedantic miniShell.cp/kth/os/smallshell/oslab$ gcc -Wall -O4
developer#de
veloper-VirtualBox:~/Desktop/kth/os/smallshell/oslab$ exit
exit
Script done on Mon 18 May 2015 06:38:58 CEST
developer#developer-VirtualBox:~/Desktop/kth/os/smallshell/oslab$
script records all characters sent to your terminal. That includes all kinds of terminal control sequences (such as cursor movement, colors, etc).
By default, less (and other pages) do not work well with these control characters. Use the -R option of less to allow the program to render the file as it was originally sent. There are some limitations, since even this presumes that the file is not generated in fullscreen mode. To handle that, your best choice is to slowly cat the file to a terminal with the same size as that on which the file was generated. For that, I use a program slowcat. Others use a -t option to script which records timing information (but that is not available on all versions of script — essentially, Linux-specific).
Alternatively, you can use a program or script to remove these control sequences, and get something comparable (without video highlights and colors) to that which less -R would show. Some discussion of how to do this is found in Can I programmatically “burn in” ANSI control codes to a file using unix utils?.

(Tcl?) Script for running modelsim with testbench as parameter from shell

I want to make a script, which can be executed from shell like:
./myscript -test1 or tclsh myscript.tcl -test1
I want it to open ModelSim, compile units, load a desired testbench, run simulation. Name of the test would be a parameter. I've already made macro files (.do) containing modelsim commands to compile & simulate desired units (+adding signals to waveform). I'm asking because scripting isn't my area of expertise.
So here's my questions:
How to ,,tell'' Modelsim (at startup) to do the commands in specified file?
Is TCL the language i'm looking for // is it doable in TCL? If so, which commands should i make familiar with?
Or maybe shell script is sufficient and i should look for specific Modelsim commands in reference manual?
Thanks for you time!
EDIT: Posting little example i've made for everyone to use. Usage: ./foo.tcl testname
#!/usr/bin/tclsh
# params
set testname [lindex $argv 0]
set testlist {test1 test2 test3}
# run vsim test $testname
if { [ lsearch $testlist $testname ] >= 0 } {
puts "Test found. Executing..."
open "|vsim -do $testname "
} else { puts "Test not found on the list!" }
You can launch vsim with arbitrary commands using the -do <arg> command line option. The argument can either be a filename of a .do file containing arbitrary Tcl code or a string of Tcl commands ("run -all; quit" is useful for non-interactive command line mode with -c).
Tcl is a full featured scripting language. It can handle any automation task you need to accomplish. Ultimately you cannot escape Tcl with Modelsim since almost everything runs through it internally. I would recommend you piece together what you need in a .do file and run that using the -do option.
If you create a .tcl script (.do files can run QuestaSim/ModelSim commands and tcl commands), you can do everything you want to do, include running other .do/.tcl files. For example:
ModelSim/QuestaSim Command Line:
just like what you are used to...
$: do MyDoFile.do
...instead use a Tcl file, which could call out your .do files:
$: source ./MyDirectory/MyTCLScript.tcl
Within that MyTCLScript.tcl you can have literally the following:
Within MyTCLScript.tcl:
...
#Use tabs for aliases
source ./MyDirectory/OtherDirectory/OtherTCLScript.tcl
MyAlias1
MyAlias2
do ./MyDoFile.do
...
Finally, to let you use commands to run single testbenches and the sort, I suggest looking at Tcl documentation on aliases. Here is an example though:
Within OtherTCLScript.tcl:
...
alias MyAlias1 {
eval <command><command flags>
}
alias MyAlias2 {
eval <command><command flags>
}
...
Sources:
1. Experience
2. Questa SIM User's Manual

Resources