Talk:Scripting Syntax

From Audacity Wiki
Jump to: navigation, search

Some suggestions for discussion / future implementation: Steve 18Jan18:

Module Installation Location

For testing purposes it would be much more convenient if modules could be installed in "Portable Setting", especially while we have the "built on same day" restriction.

On Linux, the directories for per-user data, modules, plug-ins etc. are split across two directories:

~/.audacity-files/
~/.audacity-data/

While this has some logic (modules are not really "data"), it is highly unusual for an application to not keep it's stuff all together in one folder. Following the common Linux convention, this would be:

~/.audacity/

(this is one element of a broader discussion about file locations)


Persistence of Pipes

  • Currently on Windows, mod-script-pipe creates read and write named pipes when Audacity is launched, and removes them on exit (is that correct?)
  • On Linux, the pipes are created the first time that Audacity runs with the module enabled, and they persist until the computer is rebooted.
In Python, it is not possible to tell if "the other end" of a pipe is connected, so there is no way for a Python script to determine if a pipe can be opened. This is a problem for simple Python scripts (without multi-threading), because if the pipe is not connected at the other end, attempting to open the pipe will block (the script will freeze).
If the "write to server" pipe was deleted when mod-script-pipe exits, then Python could simply look for the existence of the pipe to determine if it is safe to connect. This is not foolproof because the pipe would exist if Audacity crashed, but it would provide a simple solution in normal situations. (Also, Python can easily test if Audacity is running).
The "from pipe server" pipe should ideally not be closed by mod-script-pipe because the script may not have finished reading from it. That pipe could be left for the script to delete on exit.

Scripts to Include in Audacity Git

James suggested (and Steve agrees):

  • pipe-test.py to remain a very short script, and the one people use to see if anything is working at all.
  • pipe-test.pl should be renamed full-pipe-test.pl
  • Full-pipe-test.py: A Python script that exercises most of the available commands.
  • New users of scripting to be encouraged to use Python rather than Perl (no need to duplicate all the python scripts in perl).

James added:

  • Later I will add a script redo-manual-images.py that generates as many of the screen shots in the manual as we can


Passing String Data

Done.png String data (such as track name) should be quoted, so as to support spaces.

Python Style Guide

Our supplied Python scripts should comply with Pep 8 https://www.python.org/dev/peps/pep-0008/

"pipe-test.py" should be renamed "pipe_test.py"

Suggest that "pipe_test.py" is written as a module that can be imported.


Steve 01Feb18:

Window Identification

In an X Window Manager environment, wmctrl may be used to find which windows are open, give a window focus, and so on. If the main Audacity window always contained the word "Audacity", (for example: "Audacity - my saved project" rather than just "my saved project") then it would be easier for a Python script to detect and manipulate the Audacity window.

Bulb icon In the Automation branch, the "GetInfo: Type=Boxes" command gives the coordinates and names of all the windows and subwindows of Audacity. You could get the window by position, or look up its current title

Additional Python Scripting Tools

Scripting commands provided by mod-script-pipe may be combined with other commands. For example:

  • Key presses may be sent from Python as illustrated by pykey
  • Although somewhat lacking in documentation, AutoKey provides a powerful and comprehensive Python scripting interface that may be combined with mod-scrip-pipe commands
  • AutoHotKey provides similar functionality to AutoKey for Windows.
Bulb icon In the Automation branch, all menu commands are now directly supported by scripting, so you can for example start and stop recording without sending key presses and without using ExecMenu.
  • AutoKey/AutoHotKey could still be handy for controlling dialogs and similar where the scripting hasn't yet got an equivalent method.

Example of AutoKey script

This script uses AutoKey functions, Linux shell commands and mod-script-pipe commands. (pipecommand module is a python script that sends a single command to modscriptpipe. It should soon be available in the Audacity source.)

import subprocess
import time
import sys
sys.path.insert(0, 'path to pipecommand module')
import pipecommand

# Get list of open windows
command = 'wmctrl -l'
output = system.exec_command(command, getOutput=True)

if 'Audacity' in output:
    # Audacity is running so activate its window
    # Note: the AutoKey command will fail if there is another application
    # with "Audacity" in its name.
    # window.activate('Audacity', switchDesktop=True)
    command = 'wmctrl -Fa "Audacity"'
    output = system.exec_command(command, getOutput=True)
else:
    # Not running, so open as subprocess
    subprocess.Popen(["/home/steve/Sourcecode/audacity/build/audacity"])

# Wait until window has focus
window.wait_for_focus("Audacity", timeOut=20)

# "R" to start recording
keyboard.send_key('r')

# Sleep while Audacity records
time.sleep(5.0)

# Space to stop
keyboard.send_key(' ')

# Allow a little time for waveform to be drawn
time.sleep(0.1)
# "Alt + A" to select all
keyboard.send_keys("<ctrl>+a")

# mod-script-pipe commands
command = 'Normalize: ApplyGain=1 Level=-1.0 RemoveDcOffset=1 StereoIndependent=r0'
pipecommand.pipecommand(command)
time.sleep(0.1)
command = 'MenuCommand: CommandName=Play'
pipecommand.pipecommand(command)