Change PDF export filename

When I export a score or part as a PDF, it chooses a filename that I don’t want. The main problem is that the name contains a space, which is absolutely forbidden throughout my music catalog (because I use bash, which requires enormous effort to constantly escape spaces). For now I have been manually changing the filename every time, but this is very tedious and also error prone. Can I please change the export filename so that it never (ever ever ever ever ever) includes a space?

Thanks for this request, Byron. I agree that it would be useful to be able to customise the filenames for exported graphics, and we will do our best to support this in the future.

In the meantime, I don’t know what platform(s) you use, but perhaps a tool that monitors a folder for changes to files and automatically renames them according to your preferences might be useful, e.g. something like this?

I’m on a Mac, and in print mode I can choose to open OS X print dialog box. From there I can save file as pdf with whatever name and location I like. Use the “Print…”-button in bottom left corner.

I’m not sure why it takes “enormous effort” just to put some quotes round the file names in the bash scripts…

But as a work round, you could write another script that replaces all the blanks in the file names with hyphens, or underscores, or whatever you want. Even if that is “an effort,” you only have to do it once, and that since that script only does one thing, you will probably never have to change it even if you keep changing the way the rest of your catalog system works.

No, it is not easy to escape spaces. Consider for example:

for f in $(ls scores/trombone/bach/*.pdf); do # totally broken script

The “for” loop in bash splits everything at spaces. Sure, I can put in some “IFS=” trick here, and then some other trick in another place, and then every time it does X, do another trick, and then…

Now you have suggested to put in hyphens, but how will the filesystem understand that?

for f in $(ls scores/trombone/bach/*.pdf | tr ' ' '-'); do ls $f; done 
> No such file or directory
> No such file or directory
> No such file or directory
> No such file or directory
> No such file or directory
> No such file or directory
> No such file or directory
> No such file or directory

So the one simple solution is to prohibit spaces in the filenames. That way the OS can use a space as a delimiter, which logically it should! Ancient Greek scrolls were written without spaces, and that made them very hard to read. So people started delimiting written words with spaces. Operating systems are people too, and they need spaces just like we do. For the OS, a filename is a word; therefore–by definition of a space–there can be no spaces in filenames (speaking from the perspective of the OS). That’s why it all gets so complicated–putting a space in a filename goes against the very definition and core meaning of a space.

Bash is pretty awful for handling files with spaces in. It’s much easier to do in Python, Ruby or something else. Try the attached script (note: it won’t change anything on your file system unless you uncomment the last line, and please try it on a copy of your data directory first!)
process.zip (347 Bytes)

import os
import sys
import glob


if len(sys.argv)!=2:
  print("Usage: process.py <path>")
  exit(1)

os.chdir(sys.argv[1])

for file in glob.glob("**/*pdf"):
  newName=file.replace(" ","_")
  print(file + " -> " + newName)
  # uncomment this line to rename
  #os.rename(file, newName)

Doesn’t

for f in $(ls -Q scores/trombone/bach/*.pdf); do

put the quotes into the file name list automatically? http://man7.org/linux/man-pages/man1/ls.1.html

But I agree with PaulWalmsley, personally I wouldn’t use bash for anything complicated!

Interesting - I hadn’t come across the -Q switch before, I’ll try to remember that. Sometimes I use find/xargs with the ‘-0’ (zero) option:

find . -name “*.pdf” -print0 | xargs -0 “echo file is {}”

One of the best pieces of Unix advice I ever got (back in the 1980s!) was “make some time to re-read the man pages every 6 months - especially for the commands that you already know everything about:wink: