Xfce Forum

Sub domains
 

You are not logged in.

#1 2017-12-21 09:17:06

Mike_P
Member
Registered: 2017-12-20
Posts: 6

PDF renaming script for Thunar custom actions

Hi!

Here is another script that may be useful. I have to collect lots of PDF files at work. Most of them have filenames like "22354345.pdf" after download and it is very hard to search for the right document from the filemanager. So I made this script that helps to rename these pdf files in a meaningful way without typing. The script extract some lines from the content that may be used as a title and offers them for renaming.
It has a number of dependencies, like Yad (Zenity), convert (xnview image convert tool, optional for preview), pdftotext, detox.
Recommend this script for all xfce-using scientists out there.

#!/bin/bash

OLDIFS=$IFS
IFS=$'\n'

  i="$@"

  type="$(file -b $i)"
  if [ "${type%%,*}" == "PDF document" ]; then
        #echo "$i is a PDF file."
        string=""
       words="0"
       words=$(wc -w $i | cut -d " " -f1 )
       convert -border 10%x10% -bordercolor '#F0F8FF' -density 200 -scale x600 $i[0] /tmp/temp.png

        mtitel=$(pdfinfo $i | grep Title: | sed 's/Title:[ ]*//')
        stitel=$(pdfinfo $i | grep Subject: | sed 's/Subject:[ ]*//')
        ktitel=$(pdfinfo $i | grep Keywords: | sed 's/Keywords:[ ]*//')
        atitel=$(pdfinfo $i | grep Author: | sed 's/Author:[ ]*//')

        pdftotext $i /tmp/pdftext.txt | yad --progress --title="Reading:" --pulsate --width=650 --auto-close
        sed -i '/^[ \t]*$/d' /tmp/pdftext.txt

        ttitel1=$(awk 'NR==1 {print; exit}' /tmp/pdftext.txt)
        ttitel2=$(awk 'NR==2 {print; exit}' /tmp/pdftext.txt)
        ttitel3=$(awk 'NR==3 {print; exit}' /tmp/pdftext.txt)
        ttitel4=$(awk 'NR==4 {print; exit}' /tmp/pdftext.txt)
        ttitel5=$(awk 'NR==5 {print; exit}' /tmp/pdftext.txt)
        ttitel6=$(awk 'NR==6 {print; exit}' /tmp/pdftext.txt)
        ttitel7=$(awk 'NR==7 {print; exit}' /tmp/pdftext.txt)
        ttitel8=$(awk 'NR==8 {print; exit}' /tmp/pdftext.txt)
        ttitel9=$(awk 'NR==9 {print; exit}' /tmp/pdftext.txt)
        ttitel10=$(awk 'NR==10 {print; exit}' /tmp/pdftext.txt)
        ttitel11=$(awk 'NR==11 {print; exit}' /tmp/pdftext.txt)
        ttitel12=$(awk 'NR==12 {print; exit}' /tmp/pdftext.txt)
        ttitel13=$(awk 'NR==13 {print; exit}' /tmp/pdftext.txt)
        ttitel14=$(awk 'NR==14 {print; exit}' /tmp/pdftext.txt)
        ttitel15=$(awk 'NR==15 {print; exit}' /tmp/pdftext.txt)
        ttitel16=$(awk 'NR==16 {print; exit}' /tmp/pdftext.txt)

        rm /tmp/pdftext.txt

string=$(yad --width=1500 --title="PDF-Rename" --text="Please select your PDF title: \nWords: $words" \
--image="/tmp/temp.png" \
--form --date-format="%-d %B %Y" --item-separator="#" \
--separator="|" \
--field="File: ":RO \
--field="Meta-Title: ":RO \
--field="Rename to: ":CBE \
--field="Change PDF metadata title ":CHK \
--button="gtk-ok:0" --button="gtk-close:1" \
"$i" "$mtitel" "$line1#$mtitel#$stitel#$ktitel#$atitel#$ttitel1#$ttitel2#$ttitel3#$ttitel4#$ttitel5#$ttitel6#$ttitel7#$ttitel8#$ttitel9#$ttitel10#$ttitel11#$ttitel12#$ttitel13#$ttitel14#$ttitel15#$ttitel16" "FALSE" )

titles=$(echo $string | cut -d'|' -f3)
chk=$(echo $string | cut -d'|' -f4)
#notify-send $chk

ret=$?
rm "/tmp/temp.png"
[[ $ret -eq 1 ]] && exit 0
[[ $titles -eq "" ]] && exit 0#!/bin/bash

OLDIFS=$IFS
IFS=$'\n'

  i="$@"

  type="$(file -b $i)"
  if [ "${type%%,*}" == "PDF document" ]; then
        #echo "$i is a PDF file."
        string=""
       words="0"
       words=$(wc -w $i | cut -d " " -f1 )
       convert -border 10%x10% -bordercolor '#F0F8FF' -density 200 -scale x600 $i[0] /tmp/temp.png

        mtitel=$(pdfinfo $i | grep Title: | sed 's/Title:[ ]*//')
        stitel=$(pdfinfo $i | grep Subject: | sed 's/Subject:[ ]*//')
        ktitel=$(pdfinfo $i | grep Keywords: | sed 's/Keywords:[ ]*//')
        atitel=$(pdfinfo $i | grep Author: | sed 's/Author:[ ]*//')

        pdftotext $i /tmp/pdftext.txt | yad --progress --title="Reading:" --pulsate --width=650 --auto-close
        sed -i '/^[ \t]*$/d' /tmp/pdftext.txt


        ttitel1=$(awk 'NR==1 {print; exit}' /tmp/pdftext.txt)
        ttitel2=$(awk 'NR==2 {print; exit}' /tmp/pdftext.txt)
        ttitel3=$(awk 'NR==3 {print; exit}' /tmp/pdftext.txt)
        ttitel4=$(awk 'NR==4 {print; exit}' /tmp/pdftext.txt)
        ttitel5=$(awk 'NR==5 {print; exit}' /tmp/pdftext.txt)
        ttitel6=$(awk 'NR==6 {print; exit}' /tmp/pdftext.txt)
        ttitel7=$(awk 'NR==7 {print; exit}' /tmp/pdftext.txt)
        ttitel8=$(awk 'NR==8 {print; exit}' /tmp/pdftext.txt)
        ttitel9=$(awk 'NR==9 {print; exit}' /tmp/pdftext.txt)
        ttitel10=$(awk 'NR==10 {print; exit}' /tmp/pdftext.txt)
        ttitel11=$(awk 'NR==11 {print; exit}' /tmp/pdftext.txt)
        ttitel12=$(awk 'NR==12 {print; exit}' /tmp/pdftext.txt)
        ttitel13=$(awk 'NR==13 {print; exit}' /tmp/pdftext.txt)
        ttitel14=$(awk 'NR==14 {print; exit}' /tmp/pdftext.txt)
        ttitel15=$(awk 'NR==15 {print; exit}' /tmp/pdftext.txt)
        ttitel16=$(awk 'NR==16 {print; exit}' /tmp/pdftext.txt)

        rm /tmp/pdftext.txt

string=$(yad --width=1500 --title="PDF-Rename" --text="Please select your PDF title: \nWords: $words" \
--image="/tmp/temp.png" \
--form --date-format="%-d %B %Y" --item-separator="#" \
--separator="|" \
--field="File: ":RO \
--field="Meta-Title: ":RO \
--field="Rename to: ":CBE \
--field="Change PDF metadata title ":CHK \
--button="gtk-ok:0" --button="gtk-close:1" \
"$i" "$mtitel" "$line1#$mtitel#$stitel#$ktitel#$atitel#$ttitel1#$ttitel2#$ttitel3#$ttitel4#$ttitel5#$ttitel6#$ttitel7#$ttitel8#$ttitel9#$ttitel10#$ttitel11#$ttitel12#$ttitel13#$ttitel14#$ttitel15#$ttitel16" "FALSE" )

titles=$(echo $string | cut -d'|' -f3)
chk=$(echo $string | cut -d'|' -f4)
#notify-send $chk

ret=$?
rm "/tmp/temp.png"
[[ $ret -eq 1 ]] && exit 0
[[ $titles -eq "" ]] && exit 0

nome="${titles// /_}"
name=$(printf $nome | tr -d '|')
size=${#name}

name1=$(printf $name | head -c 80 | tr -d '|')
mv "$i" "$name1.pdf"

	if [ $chk == TRUE ]; then
       		# Rename the file so I can output to the original file name
  		mv "$name1.pdf" "temp.pdf"
  		# Write an instructions file for pdftk
  		echo "InfoKey: Title" > /tmp/pdftemp.txt
 		echo "InfoValue: $titles" >> /tmp/pdftemp.txt
 		# Change the PDF’s metadata, outputting to the original file name
  		pdftk "temp.pdf" update_info /tmp/pdftemp.txt output $name1.pdf
  		# Get rid of the renamed file
  		rm "temp.pdf"
	fi

detox "$name1.pdf"
fi
IFS=$OLDIFS

nome="${titles// /_}"
name=$(printf $nome | tr -d '|')
size=${#name}

name1=$(printf $name | head -c 80 | tr -d '|')
mv "$i" "$name1.pdf"

	if [ $chk == TRUE ]; then
       		# Rename the file so I can output to the original file name
  		mv "$name1.pdf" "temp.pdf"
  		# Write an instructions file for pdftk
  		echo "InfoKey: Title" > /tmp/pdftemp.txt
 		echo "InfoValue: $titles" >> /tmp/pdftemp.txt
 		# Change the PDF’s metadata, outputting to the original file name
  		pdftk "temp.pdf" update_info /tmp/pdftemp.txt output $name1.pdf
  		# Get rid of the renamed file
  		rm "temp.pdf"
	fi

detox "$name1.pdf"
fi
IFS=$OLDIFS

Offline

Board footer

Powered by FluxBB