GetInfo from python script

The command:

GetInfo: Type=Labels

will return something like this:

[ 
  [ 1,
    [ 
      [ 1, 2, "Hello" ],
      [ 5, 6, "World" ] ] ] ]
BatchCommand finished: OK

So the first thing to do is to extract the JSON part of the output string - that is, the text up to the final “]”.
That will give you something like:

[ 
  [ 1,
    [ 
      [ 1, 2, "Hello" ],
      [ 5, 6, "World" ] ] ] ]

which can be parsed by json.loads("<json-string-to-decode>") to give an array in the form:

[[1, [[1, 2, "Hello"],  [5, 6, "World"]]]]

The array contains an array, and the second (n=1) item in the inner array is the label data that we want.
So, if the decoded data is “jdata”, the array of labels is

labels = jdata[0][1]

Putting it all together:

In this example, I’m using pipeclient.py (https://github.com/audacity/audacity/blob/master/scripts/piped-work/pipeclient.py)

import sys
import json
import time

import pipeclient


client = pipeclient.PipeClient()

client.write('GetInfo: Type=Labels')

# Allow a little time for Audacity to return the data:
time.sleep(0.1)
reply = (client.read())
if reply == '':
    sys.exit('No data returned.')

# For robust code, the 'reply' string should be validated,
# but in this example I just assume that it is OK.
jdata = (reply [:reply.rfind(']')+1])

# Parse the jason data string:
data = json.loads(jdata)

# Extract the labels:
try:
    labels = data[0][1]
except IndexError:
    sys.exit('No labels found.')

for label in labels:
    print(label)