You are not logged in.

#1 2021-12-22 14:34:04

sleekmason
zoom
Registered: 2018-05-22
Posts: 875
Website

[Solved]Convert script From Zenity to Yad

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

#2 2021-12-22 21:19:51

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,464
Website

Re: [Solved]Convert script From Zenity to Yad

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

#3 2021-12-22 21:38:40

sleekmason
zoom
Registered: 2018-05-22
Posts: 875
Website

Re: [Solved]Convert script From Zenity to Yad

brontosaurusrex wrote:

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

#4 2021-12-22 21:42:56

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,464
Website

Re: [Solved]Convert script From Zenity to Yad

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

#5 2021-12-22 21:47:35

sleekmason
zoom
Registered: 2018-05-22
Posts: 875
Website

Re: [Solved]Convert script From Zenity to Yad

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

#6 2021-12-22 21:53:07

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,464
Website

Re: [Solved]Convert script From Zenity to Yad

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

#7 2021-12-22 21:58:20

sleekmason
zoom
Registered: 2018-05-22
Posts: 875
Website

Re: [Solved]Convert script From Zenity to Yad

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

#8 2021-12-22 22:01:10

sleekmason
zoom
Registered: 2018-05-22
Posts: 875
Website

Re: [Solved]Convert script From Zenity to Yad

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

#9 2021-12-22 22:02:53

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,464
Website

Re: [Solved]Convert script From Zenity to Yad

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

#10 2021-12-22 22:12:23

sleekmason
zoom
Registered: 2018-05-22
Posts: 875
Website

Re: [Solved]Convert script From Zenity to Yad

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

#11 2021-12-22 22:12:55

brontosaurusrex
Middle Office
Registered: 2015-09-29
Posts: 2,464
Website

Re: [Solved]Convert script From Zenity to Yad

Great! (And ignore my previous xargs post).

Last edited by brontosaurusrex (2021-12-22 22:13:42)

Offline

#12 2021-12-22 22:15:02

sleekmason
zoom
Registered: 2018-05-22
Posts: 875
Website

Re: [Solved]Convert script From Zenity to Yad

brontosaurusrex wrote:

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)"

Both work . .  any preference?

Offline

#13 2021-12-22 22:15:31

sleekmason
zoom
Registered: 2018-05-22
Posts: 875
Website

Re: [Solved]Convert script From Zenity to Yad

brontosaurusrex wrote:

Great! (And ignore my previous xargs post).

Ha!  smile

Offline

Board footer

Powered by FluxBB