bash script won't run command when getting input from a drag and dropped file on a .desktop file

Viewed 162

I am trying to create a simple drag and drop method to extract pages from a multi-page pdf file. I have a .desktop file that should run a bash script when a file dropped on it. If I run the script using ./extractpages.sh using a manual path for $thefile, it runs fine, logs the variables, and runs the command. You end up with a separate pdf for each page that is in the multi-page pdf in the same directory. But when I drag and drop a file onto the "Separate pdf pages.desktop" file, It is receiving the path for the drag and dropped file, because the variables are echoed into the log file correctly. It just doesn't run the final command. Or at least I don't think it is.

Another question: How do I check for errors when the bash script runs? One thing I notice is the in the log file, there are '(single quotes) around some of the items, and only a ' at the beginning of some and none around some. I thought this might be the problem, so I have entered a manual ' in the command string. Still no go. FWIW I am using Linux Mint 20 and Mate.

.desktop file:

#!/usr/bin/env xdg-open

[Desktop Entry]
Encoding=UTF-8
Name=Test
Comment=Drag a multipage pdf to seperate it into single pages.
Exec=mate-terminal -e "/home/spoolserver/Desktop/pdfsepfiles/extractpages.sh \"%u\""
#Exec=mate-terminal -e /home/spoolserver/Desktop/pdfsepfiles/extractpages.sh %U
Icon=/home/spoolserver/Desktop/pdfsepfiles/old/pdfsep1.svg
Type=Application
Terminal=true
Name[en_US]=Separate pdf pages.desktop

bash script:

#!/bin/bash
thefile="$1"
#thefile=/home/spoolserver/Desktop/pdfsepfiles/PIO11082021A.pdf

thename="$(basename "$thefile")" #not excluding the extension
thebasename="${thename%.*}" #excluding the extension
thepath=$(dirname "$thefile")

thenewname="$thebasename"'%02d.pdf'

echo "thefile  is $thefile" >> /home/spoolserver/Desktop/pdfsepfiles/test.log
echo "$1" >> /home/spoolserver/Desktop/pdfsepfiles/test.log
echo "thepath  is $thepath" >> /home/spoolserver/Desktop/pdfsepfiles/test.log
echo "thenewname= $thenewname" >> /home/spoolserver/Desktop/pdfsepfiles/test.log
echo "The command is pdfseparate $thefile $thepath/$thenewname'" >> /home/spoolserver/Desktop/pdfsepfiles/test.log

echo "-------------------------" >> /home/spoolserver/Desktop/pdfsepfiles/test.log

pdfseparate "$thefile" "$thepath"/"$thenewname'"

I have all of the variables echoed so that I can try and figure out why the command at the end doesn't run. Here is the content of the log file:

thefile  is '/home/spoolserver/Desktop/pdfsepfiles/PIO1102021A.pdf'
'/home/spoolserver/Desktop/pdfsepfiles/PIO1102021A.pdf'
thepath  is '/home/spoolserver/Desktop/pdfsepfiles
thenewname= PIO1102021A%02d.pdf
The command is pdfseparate '/home/spoolserver/Desktop/pdfsepfiles/PIO1102021A.pdf' '/home/spoolserver/Desktop/pdfsepfiles/PIO1102021A%02d.pdf'
-------------------------

EDIT: I found an answer to one of my questions. How do I check for errors when the bash script runs? If you add 2>> path/to/log/file It will log the errors that the command is producing. Those errors are:

I/O Error: Couldn't open file '"/home/ftpusers/Pioneer/PIO11182021B.pdf"': No such file or directory.
Syntax Error: Could not extract page(s) from damaged file ('"/home/ftpusers/Pioneer/PIO11182021B.pdf"')

The path is correct. The permissions are correct. I can run the command manually and it works. Does it have to do with the syntax that expanding the variables is producing?

EDIT:Looks like I solved this. If I remove the \"%u\" from the end of the exec line in the .desktop file, it works. For some reason this bit of text is entering single or double quotes to the path of the dragged and dropped file.

0 Answers
Related