Write text file (cross-platform compatible)

The attached plug-in is a simple demonstration plug-in for a rather complex function that writes text files.
The complexity lies in the error checking that (hopefully) makes the function robust on Linux, Mac and Windows.

The plug-in itself is not intended to be “useful” in itself - just a convenient way to test the function.

Main Features:

  • Validates file name (all 3 platforms).
  • Validates the path.
  • Checks that file can be opened for writing.
  • Outputs warning message if specified file cannot be written.
  • Fall-back mechanism if file cannot be written for any reason.

The plug-in is called “Write File” and is a “Generate” type plug-in.

Latest version: writefile.ny

Updated version.
Includes workaround for root directory of a partition on Windows.
writefile.ny (5.92 KB)

Seems to work OK on Mac.

Leaving Valid File Output Folder to “Home Directory” writes to /Users/

Specifying “~/Documents” writes to "Users//Documents.

Specifying “~/Documents/xyzzy” says no such directory, then writes to the default directory.

Tried .csv as a file extension and it was retained.

  • Bill

Thanks Bill,
Could you also try some Mac specific things that you think could give problems, such as using colons or slashes in the filename / path (you will know better than I what is likely to give problems, and whether it does “the right thing” or not).

Trying to write the file name “hour:minute” to a valid folder results in a “not a valid file name” message, then “output” is written to the specified directory.

This is OK, as the OS would translate this to “hour/minute” which would confuse Audacity and other cross-platform apps.

Better might be to substitute underscore for the colon.

– Bill

Thanks Bill.

What happens in other Mac applications if you try to write a file called (for example) “hour:minute.txt” ?

In 10.7.2 I can’t do it. In the File Save As dialog the colon is immediately replaced with a dash.

– Bill

What sort of “dash”? A hyphen “-” ?

If you entered:
~/Library/
as the path, and
hour:minute.txt
what would you (as a Mac user) expect to happen?

Can you have a file called “hour/minute.txt” ? (as opposed to a file called “minute.txt” in (for example) “~/Library/hour”)

Yes, a hyphen “-”. Standard character on the keyboard to the right of the zero.

What would I expect to happen when presented with an interface like in this plug-in?

Since I realize that one cannot use a colon in a file name, then the behaviour is reasonable.

Since I’ve seen what the system does when you try to use a colon in a file name, then I could reasonably expect the colons to be replaced with hyphens.

I can save a file named “hour/minute.txt” from TextEdit.

Hope this helps.

– Bill

One more thing…

If I enter “hour/minute” for the file name
and “~/Documents” ir “Home Folder” as the directory

the plug-in fails with the message “Nyquist did not return audio”.

– Bill

Thanks Bill.
Unfortunately I don’t think that we can allow “/” in the file name as it seems that Audacity/Nyquist won’t allow it (unless trying to output a “:” will produce a “/” in the file name).

If you (and/or any other Mac user) is interested in helping me to find out exactly what we can and can’t do, I can make up a little test plug-in for Mac OS X so that “/”, “:” and possible substitutions can be easily tested.

The simplest solution is to just not allow : or / in file names on Macs, but I’d like to get as close as possible to what Mac users are likely to expect and as I don’t have access to a Mac I need help.

Steve

Tried the new writefile.ny on Windows 7 (both 2.0.0 alpha Unicode and ANSI). I can only write to root of one of my internal drives (D: which is a small partition housing my Win XP installation). All the other drives give “not found” when writing to root (output is written to home directory).

I can write to root of U: which is the external drive and can write to root of a mapped network drive.


Gale

Thanks Gale. I’ve no idea why it is different on some drives to others. It works on the c: and d: drives of a real Vista machine that I borrowed, and it works on the d: drive of my XP virtual machine, but the c: drive shows the error. I think I have a workaround for the problem, but it’s messed up all of the output formatting, so I’ll need to fix that next.