You are not logged in.
Howdy all,
I have adjusted a script I found in order to scale an image based on width instead of % for use in a Thunar custom action. Works great with Zenity but . .
I would like to change the script to use Yad instead of Zenity. Simply replacing one for the other in the script doesn't work, and seems to cause issues for the -radiolist options. Guessing this is pretty simple, but the answer has alluded me for hours/days now and I need help.
Here is the script I would like to change to use yad:
#!/bin/bash
DEST="./Resized"
WIDTH=200
HEIGHT=327
QUALITY=100
# check required software
CHECK=("convert" "zenity")
for i in "${CHECK[@]}"
do
if ! command -v $i >/dev/null 2>&1 ; then
echo "Error: '$i' not found (install the appropriate package)" 1>&2
exit 1
fi
done
resize=$(zenity --width=$WIDTH --height=$HEIGHT --list \
--text "\n Copy And Scale Image \n Based On Final Width" \
--radiolist --column "Select " --column "New Size" \
FALSE "600" \
FALSE "800" \
FALSE "1366" \
FALSE "1440" \
FALSE "1600" \
FALSE "1920" \
FALSE "2560");
# check if "Cancel" button
if ! [[ $resize ]];
then
exit 1
fi
mkdir -p $DEST/$resize
for file
do
if [ ! -e $file ]
then
continue
fi
to_name="$DEST/$resize/"$(echo $file | cut -f1 -d.)".jpg"
# convert -resize $resize% -quality $QUALITY "${file}" "${to_name}"
convert -geometry "$resize" -quality $QUALITY "${file}" "${to_name}"
done
exit 0
Called in thunar custom actions with "%N"
This is a pretty cool custom action to resize your images:)
Last edited by sleekmason (2021-12-22 23:44:39)
Offline
With some 'cut' cheating
resize="$(yad --width="$WIDTH" --height="$HEIGHT" --list \
--text "\n Copy And Scale Image \n Based On Final Width" \
--radiolist --column "Select" --column "New Size" --separator=" " \
FALSE "600" \
FALSE "800" \
FALSE "1366" \
FALSE "1440" \
FALSE "1600" \
FALSE "1920" \
FALSE "2560")"
resize="$(echo "$resize" | cut -d' ' -f2-)"
Offline
With some 'cut' cheating
resize="$(yad --width="$WIDTH" --height="$HEIGHT" --list \ --text "\n Copy And Scale Image \n Based On Final Width" \ --radiolist --column "Select" --column "New Size" --separator=" " \ FALSE "600" \ FALSE "800" \ FALSE "1366" \ FALSE "1440" \ FALSE "1600" \ FALSE "1920" \ FALSE "2560")" resize="$(echo "$resize" | cut -d' ' -f2-)"
Still a no go here. Creates the correct folders but no image copied.
Offline
With set -x
set -x
resize="$(yad --width="$WIDTH" --height="$HEIGHT" --list \
--text "\n Copy And Scale Image \n Based On Final Width" \
--radiolist --column "Select" --column "New Size" --separator=" " \
FALSE "600" \
FALSE "800" \
FALSE "1366" \
FALSE "1440" \
FALSE "1600" \
FALSE "1920" \
FALSE "2560")"
resize="$(echo "$resize" | cut -d' ' -f2-)"
run from terminal and see what errors may popup?
Offline
Still no copied image.
sleekmason@ai:~$ resize
++ yad --width=200 --height=350 --list --text '\n Copy And Scale Image \n Based On Final Width' --radiolist --column Select --column 'New Size' '--separator= ' FALSE 600 FALSE 800 FALSE 1366 FALSE 1440 FALSE 1600 FALSE 1920 FALSE 2560
+ resize='TRUE 1440 '
++ echo 'TRUE 1440 '
++ cut '-d ' -f2-
+ resize='1440 '
+ [[ -n 1440 ]]
+ mkdir -p ./Resized/1440
+ exit 0
and full script again with changes in case I screwed it up:)
#!/bin/bash
DEST="./Resized"
WIDTH=200
HEIGHT=350
QUALITY=100
# check required software
CHECK=("convert" "yad")
for i in "${CHECK[@]}"
do
if ! command -v $i >/dev/null 2>&1 ; then
echo "Error: '$i' not found (install the appropriate package)" 1>&2
exit 1
fi
done
set -x
resize="$(yad --width="$WIDTH" --height="$HEIGHT" --list \
--text "\n Copy And Scale Image \n Based On Final Width" \
--radiolist --column "Select" --column "New Size" --separator=" " \
FALSE "600" \
FALSE "800" \
FALSE "1366" \
FALSE "1440" \
FALSE "1600" \
FALSE "1920" \
FALSE "2560")"
resize="$(echo "$resize" | cut -d' ' -f2-)"
# check if "Cancel" button
if ! [[ $resize ]];
then
exit 1
fi
mkdir -p $DEST/$resize
for file
do
if [ ! -e $file ]
then
continue
fi
to_name="$DEST/$resize/"$(echo $file | cut -f1 -d.)".jpg"
# convert -resize $resize% -quality $QUALITY "${file}" "${to_name}"
convert -geometry "$resize" -quality $QUALITY "${file}" "${to_name}"
done
exit 0
Calling with resize %N from thunar.
Last edited by sleekmason (2021-12-22 21:48:18)
Offline
You need to feed it some image, like
resize someimage.png
Other stuff: Script seems poorly quoted, perhaps https://www.shellcheck.net/ and fix all the quotes.
Offline
Sorry about that.
sleekmason@ai:~/Pictures/extras$ resize Canopy.jpg
\++ yad --width=200 --height=350 --center --list --text '\n Copy And Scale Image \n Based On Final Width' --radiolist --column Select --column 'New Size' '--separator= ' FALSE 600 FALSE 800 FALSE 1366 FALSE 1440 FALSE 1600 FALSE 1920 FALSE 2560
+ resize='TRUE 1440 '
++ echo 'TRUE 1440 '
++ cut '-d ' -f2-
+ resize='1440 '
+ [[ -n 1440 ]]
+ mkdir -p ./Resized/1440
+ for file in "$@"
+ '[' '!' -e Canopy.jpg ']'
++ echo Canopy.jpg
++ cut -f1 -d.
+ to_name='./Resized/1440 /Canopy.jpg'
+ convert -geometry '1440 ' -quality 100 Canopy.jpg './Resized/1440 /Canopy.jpg'
convert-im6.q16: unable to open image `./Resized/1440 /Canopy.jpg': No such file or directory @ error/blob.c/OpenBlob/2924.
+ exit 0
sleekmason@ai:~/Pictures/extras$ \
Yeah, seems everything I get off the web needs cleaning up.
Offline
What shellscript said:
$ shellcheck myscript
Line 15:
convert-im6.q16: unable to open image `./Resized/1440 /Canopy.jpg': No such file or directory @ error/blob.c/OpenBlob/2924.
^-- SC1009 (info): The mentioned syntax error was in this simple command.
^-- SC1073 (error): Couldn't parse this backtick expansion. Fix to allow more checks.
Line 18:
^-- SC1072 (error): Fix any mentioned problems and try again.
Offline
You are getting an extra space, which I don't.
Not sure, but xargs should remove leading/trailing spaces, so replace
resize="$(echo $resize | cut -d' ' -f2-)"
with
resize="$(echo $resize | cut -d' ' -f2- | xargs)"
Last edited by brontosaurusrex (2021-12-22 22:12:38)
Offline
Fixed all the issues in shellcheck and it works now! Thank you so much:)
#!/bin/bash
DEST="./Resized"
WIDTH=200
HEIGHT=340
QUALITY=100
# check required software
CHECK=("convert" "yad")
for i in "${CHECK[@]}"
do
if ! command -v "$i" >/dev/null 2>&1 ; then
echo "Error: '$i' not found (install the appropriate package)" 1>&2
exit 1
fi
done
set -x
resize="$(yad --title "Resize" --width="$WIDTH" --height="$HEIGHT" --center --list \
--text "\n Copy And Scale Image \n Based On Final Width. \n" --text-align=center \
--radiolist --column "Select" --column "New Size" --separator=" " \
FALSE "600" \
FALSE "800" \
FALSE "1366" \
FALSE "1440" \
FALSE "1600" \
FALSE "1920" \
FALSE "2560")"
resize="$(echo "$resize" | cut -d' ' -f2-)"
# check if "Cancel" button
if ! [[ $resize ]];
then
exit 1
fi
mkdir -p "$DEST/$resize"
for file
do
if [ ! -e "$file" ]
then
continue
fi
to_name="$DEST/$resize/$(echo "$file" | cut -f1 -d.).jpg"
# convert -resize $resize% -quality $QUALITY "${file}" "${to_name}"
convert -geometry "$resize" -quality $QUALITY "${file}" "${to_name}"
done
exit 0
Last edited by sleekmason (2021-12-22 23:43:18)
Offline
Great! (And ignore my previous xargs post).
Last edited by brontosaurusrex (2021-12-22 22:13:42)
Offline
You are getting an extra space, which I don't.
Not sure, but xargs should remove leading/trailing spaces, so replaceresize="$(echo $resize | cut -d' ' -f2-)"
with
resize="$(echo $resize | cut -d' ' -f2- | xargs)"
Both work . . any preference?
Offline