GetInfo from python script

I use audacity 2.3.1 alpha with mode_pipe enabled and playing with pipe_test.py
I’ve looked into scripting docs, maybe using GetInfo I can obtain the following information:
original file imported in audacity, with full path
track name
labels
Can someone give me an example for the commands I must use?
(the text I must send on the pipe)


Thanks

There’s no direct way to obtain the file name and path of the last imported file from Audacity.
If you are using Python with mod-script-pipe, then you can use Python to do the import, so the exact file name and path is known to Python.

To get track info (including track names) in JSON format:

GetInfo: Type=Tracks

To get labels in JSON format:

GetInfo: Type=Labels

Example output:

[ 
  [ 1,
    [ 
      [ 23.8822, 23.8822, "Hello" ],
      [ 87.568, 108.308, "World" ] ] ] ]

thanks, steve,
it works perfect

and the last unknowns for me:
Is there a way I get can the label time already formatted like the selection times appear on the bottom of the window?
Can I get the start and end of a selection ?


regards

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)

thanks a lot for all your help

Regards,
Eduard