3

I want to create a thunar custom action that lets me right-click a PDF and then select "split PDF" to split it into individual pages. The command to do this is:

$ pdfseparate mypdf.pdf mypdf-%d.pdf 

Which will create a new PDF for each page, called mypdf-1.pdf, mypdf-2.pdf, etc. %d is replaced by the pdfseparate command with a page number as per the man page:

SYNOPSIS pdfseparate [options] PDF-file PDF-page-pattern 
PDF-page-pattern should contain %d (or any variant respecting printf format), since %d is replaced by the page number. 

Thunar custom actions interpolate any %n as the selected file any %d with the directory the file is in. So if I try to replicate the above command, replacing mypdf.pdf with %n, it looks like this:

pdfseparate "%n" "$(basename "%n" .pdf)-%d.pdf" 

However, then thunar will replace the %d with the file's directory. How can I escape the %d so it will be passed on to pdfseparate verbatim?

pdfseparate does not appear to have an option to use a different variable than %d but it does say it accepts "any variant respecting printf format". I'm not sure what that means... I tried %s and it didn't work.

2
  • 3
    In printf-like tools, you can often use %% for a literal %, so try something like pdfseparate "%n" "$(basename "%n" .pdf)-%%d.pdf", that might escape the % in %d and pass it as is to pdfseparate. Does that work? Commented Jan 18, 2023 at 17:44
  • @terdon Yes, post it as an answer for credit. The "%n"s must be unquoted though or the quotes are interpreted as part of the filename. Thunar must be interpreting the %n literally because I tested it unquoted on files with spaces in the name and it worked. Commented Jan 19, 2023 at 16:52

1 Answer 1

2

In printf-like tools, you can often use %% for a literal %. For example, in the man page of the printf found in the standard C library (see man 3 printf), you will see:

 % A '%' is written. No argument is converted. The complete conversion specification is '%%'. 

So, to escape the % and have it treated as a literal %, just use %%. In your case, that means the command you want to give to thunar is:

pdfseparate %n "$(basename %n .pdf)-%%d.pdf" 
4
  • Good, however, when I tested with the \"s, the output files got quotes in the filenames as in "myfile"-1.pdf. Commented Jan 19, 2023 at 17:07
  • @ki9 hmm. The thing is without the quotes, it will almost certainly break if you try it on a file whose name contains spaces. I don't know exactly how it should be quoted without trying, but some quoting in the basename command will be needed. I'll remove them now, but if you try with a file named something like foo bar.pdf, let me know how it goes! Commented Jan 19, 2023 at 17:17
  • I did test it on a file with spaces and it does work as you have it now, creating files like foo bar-1.pdf. Like I said, I guess thunar is already treating %n like a single parameter as if it were already "%n". Commented Jan 19, 2023 at 17:25
  • @ki9 OK, great. You must be right and it is passing the %n quoted. I thought the %n was used and expanded by pdfseparate but if it isn't, then great! Commented Jan 19, 2023 at 17:27

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.