Cannot write a string with double quotes by SetPreference:

Hello,

I am unable to write a string with double quotes as a value by SetPreference:
Example:

(aud-do-command "SetPreference" :Name "FileFormats\/ExternalProgramExportCommand" :Value "ffmpeg -i - \"%f.wav\"")

This is not working. Instead this is written into audacity.cfg:

ExternalProgramExportCommand=ffmpeg -i - %f.wav

As you can see, the double quotes are missing.

Can you help me with this please?

The “SetPreference:” command gets me every time - the correct syntax is not obvious (a documented example would be nice, but as far as I’m aware there isn’t one yet).

Here’s how I approach it:

  1. Look up in “audacity.cfg” the setting that I want to use.
    In this case I have:
[FileFormats]
...
ExternalProgramExportCommand=lame - "%f"
  1. Test the “Name” using “Extra menu > Scriptables 1 > GetPreference”.
    In this case, when I enter:
FileFormats/ExternalProgramExportCommand

a message box pops up displaying the current value:

lame - "%f"

If I’ve entered an invalid “Name”, then there’s no result.
Note that the correct command has a forward slash between “FileFormats” and “ExternalProgramExportCommand”


3. Copy that “Name” into a text editor (such as NotePad++), put quotes around it:

"FileFormats/ExternalProgramExportCommand"
  1. Now we need to wrap it in AUD-DO, but note that to use double-quote characters within the string, we must “escape” the quotes.
    This won’t work:
(aud-do "GetPreference: Name="FileFormats/ExternalProgramExportCommand"")

because the initial quote (start of a string literal) is closed before the “F”:

"GetPreference: Name="

To treat the double-quote before the “F” as "just another character, we escape it with a backslash (and the same for the double-quote just before the end):

(aud-do "GetPreference: Name=\"FileFormats/ExternalProgramExportCommand\"")
  1. Now test it in the Nyquist Prompt by printing it to the debug window (click the “Debug” button to run):
(print (aud-do "GetPreference: Name=\"FileFormats/ExternalProgramExportCommand\""))

and in the debug window, Nyquist prints:

("lame - "%f"" . T)

Now that we’ve worked out and checked that the “Name” works, we can test the “SetPreference:” command.
Remember to escape double quotes that appear within a string.

(aud-do-command "SetPreference:"
    :Name "FileFormats/ExternalProgramExportCommand"
    :Value "ffmpeg -i - \"%f.wav\"")

""  ;Empty string as return value to avoid complaints about returning a list.

I think I have very similar approach. Also I got to exactly the same code as your last one. But the problem is that it removes the double quotes, although they are escaped.
Does the last code you posted work for you?

Yes.

What you posted is different from what I posted.

My code (in one line):

(aud-do-command "SetPreference:" :Name "FileFormats/ExternalProgramExportCommand" :Value "ffmpeg -i - \"%f.wav\"")

Your code:

(aud-do-command "SetPreference" :Name "FileFormats\/ExternalProgramExportCommand" :Value "ffmpeg -i - \"%f.wav\"")

You have “/” between “FileFormats” and “ExternalProgramExportCommand”.

I forgot to mention that I have removed the escape character. I do not understand what am doing wrong… I run this in the prompt:

(aud-do-command "SetPreference:" :Name "FileFormats/ExternalProgramExportCommand" :Value "ffmpeg -i - \"%f.wav\"")
""

and I get this in the audacity.cfg:

ExternalProgramExportCommand=ffmpeg -i - %f.wav

Is it possible that it does not work because I am using Audacity 2.4.2 and you are using the latest? Or is it because I am on Windows and you on Linux?

I misunderstood what you meant.

Yes the double quotes around “%f.wav” are stripped out by Audacity when the value is written into audacity.cfg, but (at least on Linux) that does not matter. The export (with ffmpeg) still works correctly.

After running my code snippet in the Nyquist Prompt, the audacity.cfg file contains:

[FileFormats]
...
ExternalProgramExportCommand=ffmpeg -i - %f.wav

and when I then export using “external encoder”, the result is the same whether those double quotes are present or not.

Thank you. For a simple command, this is not a problem as you explained. But if I use powershell to run multiple commands and some of them contain the double quotes, then it becomes a problem. I can write manually into audacity.cfg > ExternalProgramExportCommand what I want and I can run it with no problem, but I do not know how to write the same thing into audacity.cfg with Nyquist plugin so it does not strip double quotes.

You confirmed that this is happening by design, so I am thinking if there is a workaround. I did not come with anything yet…

Looking through my “audacity.cfg”, the only places that I see double quotes is in relation to external program export, and in those cases it seems that the quotes are optional. If you can provide an actual example of where that is a problem, I’ll look into it, but so far it seems to be a non-issue.