Image 01


Norman Carver
ps2pdf service menu

Dolphin Service Menus by clhong 5 comments

While ps2pdf with one argument works as desired on the command line, it fails for me in a service menu (KDE 3.4.2).
The sed-based versions work fine, but neither one appears to be quite correct. The first problem is that the substitute command takes a regexp as its first argument. This means that the "." in the pattern means to match any single char--not to literally match a dot. Try running this on a file with a name like and see what happens! To fix this, you need to escape the dot like this:
sed -e 's/\.ps/.pdf/'
The second problem is that the "g" option means to apply the substituation globally, so multiple substitutions may take place--probably not what you want.The final possible problem is that correcting the "." will still not necessarily limit this to being applied to an extension which is what you might want. This can be accomplished by using a "$" in the regexp to denote that the .ps must be at the end of the filename:
(need to protect the $ inside of double quotes, but \ didn't work for some reason, though doubling the $'s did--not sure why).
Now I wanted something more complex to happen with filenames, so that a final .ps would get changed to .pdf, but also: (1) if there were no extension a .pdf would be added and (2) if the .ps "extension" was not final it would get changed to .pdf (e.g., would become To accomplish this I ended up with:
Exec=ps2pdf %f "`echo %f | sed -e 's/\.ps$$/.pdf/p' -e '/\.pdf$$/ d' -e 's/\.ps\./.pdf./p' -e '/\.pdf\./d' -e 's/$$/.pdf/'`"
(Translation: if .ps is at the end of the filename, replace it with .pdf, if .pdf was put at end then done, else look for first .ps. in filename and replace with .pdf., if .pdf. inserted then done, else add .pdf to end of filename. If there is a simpler way to do this with sed I didn't see it!) - Jul 14 2006