[SOLVED] Yad and "--dclick-action"

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Koentje
Level 4
Level 4
Posts: 472
Joined: Tue Jan 04, 2022 6:23 pm

[SOLVED] Yad and "--dclick-action"

Post by Koentje »

I made a small script that lists all the installed packages on my system and shows the package information. At the moment you have to select a package and click on the button. But i want it to be possible to double click on a package name to open the info dialog. It should be possible with the --dclick-action flag, but i can't get it to work..

Code: Select all

#!/bin/bash
################################################################
#                                                              #
#  Small script that lists all installed packages with option  #
#  to show package info and link to website. Only tested on    #
#  Mint 19 & 20.   Depends on: apt, dpkg, yad and a browser    #
#                                                              #
#  Made by Koentje  (lowrida007@gmail.com         version 1.0  #
#                                                              #
################################################################

tmpfile="/tmp/yad-show-package.tmp"
main_width="1900"
main_height="800"
dialog_width="1050"
dialog_height="500"
dialog_font="Ubuntu Mono 12"


###############################################################################################################
#############################  CHANGES AFTER THIS LINE ARE AT OWN RISK  #######################################
###############################################################################################################

arch=`dpkg --print-architecture`


function info_dialog () {
 package=$(echo "$input" | awk -F "|" '{print $2}')
 sudo apt show "$package" > "$tmpfile"
 url=$(cat "$tmpfile" | grep "Homepage:" | awk '{print $2}')

 cat $tmpfile | yad --text-info --margins=6 --back=#181818\
 --title=" Show package  $package "\
 --center --fixed --on-top --wrap\
 --tail --always-print-result\
 --fontname="$dialog_font"\
 --text-align=left\
 --width="$dialog_width" --height="$dialog_height"\
 --buttons-layout=spread\
 --button="OK"\
 --button=" Website ":44

 case $? in
   44)		# Open url
    echo "$?: Open $url"
    xdg-open $url
    info_dialog
   ;;
   *)
    echo "Exit nr: $?"
   ;;
 esac

main
}


function main () {
 input=$(dpkg -l | sort -fbn | tail -n +4 | grep -F "ii" | awk -F " "  '{ printf $1"\n" $2"\n" $3"\n" $4"\n"; $1=$2=$3=$4=""; printf substr($0,5)"\n" }' | yad --listening\
 --list --width="$main_width" --height="$main_height"\
 --title=" Show installed packages - ($arch)  "\
 --window-icon=utilities-system-monitor\
 --center --vscoll-policy=always\
 --window-icon=application\
 --text-align=left\
 --button=" Package Info  "!help-about!" Shows more info about package ":200\
 --button="gtk-close"!gtk-close!" Bye.. ":202\
 --buttons-layout=spread\
 --dclick-action='info_dialog'\
 --add-preview\
 --column-width=10 --grid-lines=both --expand-column=100 --no-markup\
 --column="Inst:TEXT"\
 --column="Package name:TEXT"\
 --column="Version number:TEXT"\
 --column="Platform:TEXT"\
 --column="Short description:TEXT") # '

 case $? in
   200)		# apt show package
    echo "$?: Open info dialog"
    info_dialog
   ;;
   202)		# Cancel
    echo "Program halted."
    exit
   ;;
   252)		# Escape exit
    echo "Escaped."
    exit
   ;;
   *)
    echo "Exit nr: $1"
   ;;
 esac
}

main
Last edited by Koentje on Mon Jan 23, 2023 2:26 pm, edited 1 time in total.
vimes666
Level 5
Level 5
Posts: 933
Joined: Tue Jan 19, 2016 6:08 pm

Re: Yad and "--dclick-action"

Post by vimes666 »

You are calling 'info_dialog' from a yad dialog. The yad dialog is a sub-process of your script and as such it has its own environment. It therefore cannot see the function in your script.
You can repair that by exporting the 'info_dialog' function.
You can find an example of its use here: https://github.com/v1cont/yad/wiki/Man-pages-browser
If you think the issue is solved, edit your original post and add the word solved to the title.
Koentje
Level 4
Level 4
Posts: 472
Joined: Tue Jan 04, 2022 6:23 pm

Re: Yad and "--dclick-action"

Post by Koentje »

I have tried that, but can't get it to work. It is indeed an environment issue..
vimes666
Level 5
Level 5
Posts: 933
Joined: Tue Jan 19, 2016 6:08 pm

Re: Yad and "--dclick-action"

Post by vimes666 »

I was on my way to an appointment when I answered this so it was a bit too hasty.
In addition to adding export -f info_dialog right after the function you should change this line:
--dclick-action='info_dialog'\
into

Code: Select all

--dclick-action="bash -c 'info_dialog'"\
However then you will run into some more errors as you will see when you run it from a terminal.
There is a typo in the width construct and you will have to provide some values to the function. The %s parameter in the example I mentioned will do that.
If you think the issue is solved, edit your original post and add the word solved to the title.
Koentje
Level 4
Level 4
Posts: 472
Joined: Tue Jan 04, 2022 6:23 pm

Re: Yad and "--dclick-action"

Post by Koentje »

I know i tried it this way. I could solve the width and height issue by replacing the strings with actual values.. But it seems that the $input string is empty, so all the cat commands output an error.
1000
Level 5
Level 5
Posts: 879
Joined: Wed Jul 29, 2020 2:14 am

Re: Yad and "--dclick-action"

Post by 1000 »

Code: Select all

#!/bin/bash
################################################################
#                                                              #
#  Small script that lists all installed packages with option  #
#  to show package info and link to website. Only tested on    #
#  ... .   Depends on: apt, dpkg, yad and a browser    #
#                                                              #
#  Made by Koentje  (lowrida007@gmail.com         version 1.1  #
#                                                              #
################################################################

tmpfile="/tmp/yad-show-package.tmp"
main_width="1900"
main_height="800"
dialog_width="1050"
dialog_height="500"
dialog_font="Ubuntu Mono 12"


###############################################################################################################
#############################  CHANGES AFTER THIS LINE ARE AT OWN RISK  #######################################
###############################################################################################################

arch=`dpkg --print-architecture`


function info_dialog () {
 package=$(echo "$input" | awk -F "|" '{print $2}')
# sudo apt show "$package" > "$tmpfile"
# sudo seems redundant because the command works without sudo
 apt show "$package" > "$tmpfile"
 
if [[ -z "$tmpfile" ]] ; then
    Message="info about package not available"
    Error_dialog
else
     #url=$(cat "$tmpfile" | grep "Homepage:" | awk '{print $2}')
     url=$(grep "Homepage:" "$tmpfile" | awk '{print $2}')

    # cat $tmpfile | yad --text-info --margins=6 --back=#181818\
    # black text on a black background is invisible
    cat $tmpfile | yad --text-info --margins=6 \
     --title=" Show package  $package "\
     --center --fixed --on-top --wrap\
     --tail --always-print-result\
     --fontname="$dialog_font"\
     --text-align=left\
     --width="$dialog_width" --height="$dialog_height"\
     --buttons-layout=spread\
     --button="OK"\
     --button=" Website ":44

     case $? in
        44)		# Open url
            echo "$?: Open $url"
                if [[ -z "$url" ]] ; then
                    Message="url not available"
                    Error_dialog
                else
                    xdg-open $url
                fi
            info_dialog
            ;;
        *)
            echo "Exit nr: $?"
            ;;
     esac
fi
 

main
}


function Error_dialog () {
    yad --title="simple form" --text="Error: $Message" --button="OK":1  --image=gtk-dialog-error ; Status="$?"
}


function main () {
 input=$(dpkg -l | sort -fbn | tail -n +4 | grep -F "ii" | awk -F " "  '{ printf $1"\n" $2"\n" $3"\n" $4"\n"; $1=$2=$3=$4=""; printf substr($0,5)"\n" }' | yad --listening\
 --list --width="$main_width" --height="$main_height"\
 --title=" Show installed packages - ($arch)  "\
 --window-icon=utilities-system-monitor\
 --center --vscoll-policy=always\
 --window-icon=application\
 --text-align=left\
 --button=" Package Info  "!help-about!" Shows more info about package ":200\
 --button="gtk-close"!gtk-close!" Bye.. ":202\
 --buttons-layout=spread\
 --dclick-action='info_dialog'\
 --add-preview\
 --column-width=10 --grid-lines=both --expand-column=100 --no-markup\
 --column="Inst:TEXT"\
 --column="Package name:TEXT"\
 --column="Version number:TEXT"\
 --column="Platform:TEXT"\
 --column="Short description:TEXT") # '

 case $? in
   200)		# apt show package
    echo "$?: Open info dialog"
    info_dialog
   ;;
   202)		# Cancel
    echo "Program halted."
    exit
   ;;
   252)		# Escape exit
    echo "Escaped."
    exit
   ;;
   *)
    echo "Exit nr: $1"
   ;;
 esac
}

main
vimes666
Level 5
Level 5
Posts: 933
Joined: Tue Jan 19, 2016 6:08 pm

Re: Yad and "--dclick-action"

Post by vimes666 »

But it seems that the $input string is empty, so all the cat commands output an error.
That is because of the same environment problem. $input is defined in main(), main() executes yad. yad gets its own environment. Now when you execute info_dialog() from $input will not be in its environment.
You can solve that by exporting $input as well.
Did you look up wat the %s parameter does in a dclick-action?
I didnt look any further at your code (yet) nor @1000's suggested changes though I am getting curious how the original script looked like. Did it contain yad originally?
If you think the issue is solved, edit your original post and add the word solved to the title.
1000
Level 5
Level 5
Posts: 879
Joined: Wed Jul 29, 2020 2:14 am

Re: Yad and "--dclick-action"

Post by 1000 »

You can use to compare for example

Code: Select all

 reset ; diff --color=always -y -W 100 installed.old installed.new 
or with better color

Code: Select all

reset ; colordiff -y -W 100 installed.old installed
with GUI
https://www.tecmint.com/best-linux-file ... omparison/


Of course, this is not the best tool
because part looks like this

Code: Select all

main                                            main
                                              > }
                                              >
                                              >
                                              > function Error_dialog () {
                                              >     yad --title="simple form" --text="Error: 
}                                               }
it should look like this

Code: Select all

main                                            main
}                                               }
                                              >
                                              >
                                              > function Error_dialog () {
                                              >     yad --title="simple form" --text="Error: 
                                              > }
but i don't know a better tool.
vimes666
Level 5
Level 5
Posts: 933
Joined: Tue Jan 19, 2016 6:08 pm

Re: Yad and "--dclick-action"

Post by vimes666 »

Anyway, I had nothing to do so I looked at the code and tried to get it running.
Well that was good fun, I ran into a load of things even after I had fixed it :)
Here is the result and it works in the way I think it was intended to work. Try it out and have a look what has been changed (although it won't win a beauty contest).

Code: Select all

#!/bin/bash
################################################################
#                                                              #
#  Small script that lists all installed packages with option  #
#  to show package info and link to website. Only tested on    #
#  Mint 19 & 20.   Depends on: apt, dpkg, yad and a browser    #
#                                                              #
#  Made by Koentje  (lowrida007@gmail.com         version 1.0  #
#                                                              #
################################################################

main_width="1900"
main_height="800"

###############################################################################################################
#############################  CHANGES AFTER THIS LINE ARE AT OWN RISK  #######################################
###############################################################################################################

arch=`dpkg --print-architecture`

function open_file {
   xdg-open "$url" || notify-send "No website found" "Url: $url"
}
export -f open_file

function info_dialog () {
 tmpfile="/tmp/yad-show-package.tmp"
 dialog_width="1050"
 dialog_height="500"
 dialog_font="Ubuntu Mono 12"
 apt-cache show "$2" > "$tmpfile"         # use apt-cache show in scripts. apt show is unstable in scripts 
 export url=$(cat "$tmpfile" | grep "Homepage:" | awk '{print $2}')

 cat $tmpfile | yad --text-info --margins=6 --back=#181818\
 --title=" Show package  $package "\
 --center --fixed --wrap\
 --tail --always-print-result\
 --fontname="$dialog_font"\
 --text-align=left\
 --width="$dialog_width" --height="$dialog_height"\
 --buttons-layout=spread\
 --button="OK" \
 --button="Website:bash -c open_file"             # you cannot open the url directly here because yad cannot handle the : in the url
}
export -f info_dialog

function main () {
 export input
 input=$(dpkg -l | sort -fbn | tail -n +4 | grep -F "ii" | awk -F " "  '{ printf $1"\n" $2"\n" $3"\n" $4"\n"; $1=$2=$3=$4=""; printf substr($0,5)"\n" }' | yad --list\
 --width="$main_width" --height="$main_height"\
 --title=" Show installed packages - ($arch)  "\
 --separator=' '\
 --window-icon=utilities-system-monitor\
 --center --vscoll-policy=always\
 --window-icon=application\
 --text-align=left\
 --button=" Package Info  "!help-about!" Shows more info about package ":200\
 --button="gtk-close"!gtk-close!" Bye.. ":202\
 --buttons-layout=spread\
 --dclick-action="bash -c 'info_dialog %s'"\
 --add-preview\
 --column-width=10 --grid-lines=both --expand-column=100 --no-markup\
 --column="Inst:TEXT"\
 --column="Package name:TEXT"\
 --column="Version number:TEXT"\
 --column="Platform:TEXT"\
 --column="Short description:TEXT")

 case $? in
   200)		# apt show package
    echo "$?: Open info dialog"
    info_dialog $input
   ;;
   202)		# Cancel
    echo "Program halted."
    exit
   ;;
   252)		# Escape exit
    echo "Escaped."
    exit
   ;;
   *)
    echo "Exit nr: $1"
   ;;
 esac
}

main
Have fun.
If you think the issue is solved, edit your original post and add the word solved to the title.
Koentje
Level 4
Level 4
Posts: 472
Joined: Tue Jan 04, 2022 6:23 pm

Re: Yad and "--dclick-action"

Post by Koentje »

vimes666 wrote:
Mon Jan 23, 2023 7:27 am
That is because of the same environment problem. $input is defined in main(), main() executes yad. yad gets its own environment. Now when you execute info_dialog() from $input will not be in its environment.
I understand that every function has it's own environment, so does yad in main(). But when i single click on a program and then the button "Package Info" (going to info_dialog function) it also switches to another environment, but $input has all the data i need. But when i use --dclick-action= $input is empty?

I will try your script and report my findings.. ;)
Koentje
Level 4
Level 4
Posts: 472
Joined: Tue Jan 04, 2022 6:23 pm

Re: Yad and "--dclick-action"

Post by Koentje »

Great!! :D Works flawless! Now i finally can remove the "Package Info" button. Oh and i also removed the 'no url' notification.. i don't like notifications. I made the website button variable. No url, no button! ;)

Thanks!

Code: Select all

#!/bin/bash
#####################################################################
#                                                                   #
#  Small script that lists all installed packages. Double click on  #
#  a package shows package info and a link to the package homepage. #
#  Only tested on Mint 19 & 20.                                     #
#  Depends on: apt, dpkg, yad and an internet browser               #
#                                                                   #
#  Made by Koentje  (lowrida007@gmail.com                           #
#                                                     version 1.3   #
#####################################################################

arch=`dpkg --print-architecture`


function open_file {
   xdg-open "$url"
}
export -f open_file


function info_dialog () {
 tmpfile="/tmp/yad-show-package.tmp"
 dialog_width="1050"
 dialog_height="500"
 dialog_font="Ubuntu Mono 12"
 apt-cache show "$2" > "$tmpfile"
 export url=$(cat "$tmpfile" | grep "Homepage:" | awk '{print $2}')
  if ! [ "$url" = "" ]; then buttonhomepage="--button=Website:bash -c open_file"; fi   # Now button is only visible when a homepage is found

 cat $tmpfile | yad --text-info --margins=6 --back=#181818\
 --title=" Show package  $package "\
 --center --fixed --wrap\
 --tail --always-print-result\
 --fontname="$dialog_font"\
 --text-align=left\
 --width="$dialog_width" --height="$dialog_height"\
 --buttons-layout=spread\
 --button="OK" \
 "$buttonhomepage"
}
export -f info_dialog


function main () {
 main_width="1900"
 main_height="800"
 export input
 input=$(dpkg -l | sort -fbn | tail -n +4 | grep -F "ii" | awk -F " "  '{ printf $1"\n" $2"\n" $3"\n" $4"\n"; $1=$2=$3=$4=""; printf substr($0,5)"\n" }' | yad --list\
 --width="$main_width" --height="$main_height"\
 --title=" Show installed packages - ($arch)  "\
 --separator=' '\
 --window-icon=utilities-system-monitor\
 --center --vscoll-policy=always\
 --window-icon=application\
 --text-align=left\
 --button="gtk-close"!gtk-close!" Bye.. ":202\
 --buttons-layout=spread\
 --dclick-action="bash -c 'info_dialog %s'"\
 --add-preview\
 --column-width=10 --grid-lines=both --expand-column=100 --no-markup\
 --column="Inst:TEXT"\
 --column="Package name:TEXT"\
 --column="Version number:TEXT"\
 --column="Platform:TEXT"\
 --column="Short description:TEXT") # '

 case $? in
   202)		# Cancel
    echo "Program halted."
    exit
   ;;
   252)		# Escape exit
    echo "Escaped."
    exit
   ;;
   *)
    echo "Exit nr: $1"
   ;;
 esac
}

main
vimes666
Level 5
Level 5
Posts: 933
Joined: Tue Jan 19, 2016 6:08 pm

Re: [SOLVED] Yad and "--dclick-action"

Post by vimes666 »

Oh and i also removed the 'no url' notification
It is a bit strange when you click a button and nothing happens, but its your script, do what you like with it :)
I like the script btw, it is useful.
However if you want to make things like this in bash and yad, you have to realize that it can be a pain in the lower regions, mostly at moments you think you are almost done. I have been through quite a lot of delimeters hells with it and the subprocess (subshell) aspect can make things very complex and hard to get ones head around.
And yad has a lot of quirks. I once spent a day to get a text in a label with some spaces in it and I failed, mockingly writing some code to replace them with underscores to work around it.
There is documentation but it is very basic. For the nifty stuff you have to deal with examples on the web that oftentimes don't work to begin with.
Maybe there are less quirks in the latest version of yad but if you want to make your script public you have to deal with the old repo version to keep the install requirements simple and distro compatible.
There is also a yad thread in google groups that is active with the yad maker himself aswering questions. I advise you to check it out: https://groups.google.com/g/yad-common
Maybe you should also watch this video: https://www.youtube.com/watch?v=dKTyRgBsMtE
If you think the issue is solved, edit your original post and add the word solved to the title.
Koentje
Level 4
Level 4
Posts: 472
Joined: Tue Jan 04, 2022 6:23 pm

Re: [SOLVED] Yad and "--dclick-action"

Post by Koentje »

vimes666 wrote:
Mon Jan 23, 2023 2:39 pm
Oh and i also removed the 'no url' notification
It is a bit strange when you click a button and nothing happens, but its your script, do what you like with it :)
Koentje wrote: No url, no button! ;)

if ! [ "$url" = "" ]; then buttonhomepage="--button=Website:bash -c open_file"; fi

I like the script btw, it is useful.
Thanks. It was something i was missing in Mint, a simple overview of installed software.
1000
Level 5
Level 5
Posts: 879
Joined: Wed Jul 29, 2020 2:14 am

Re: [SOLVED] Yad and "--dclick-action"

Post by 1000 »

This part
if ! [ "$url" = "" ]; then buttonhomepage="--button=Website:bash -c open_file"; fi # Now button is only visible when a homepage is found

cat $tmpfile | yad --text-info --margins=6 --back=#b0b0b0\
--title=" Show package $package "\
--center --fixed --wrap\
--tail --always-print-result\
--fontname="$dialog_font"\
--text-align=left\
--width="$dialog_width" --height="$dialog_height"\
--buttons-layout=spread\
--button="OK" \
"$buttonhomepage"
It is beautiful :mrgreen:
But I changed the background color to grey. Because I can't see a black font on a black background.

Maybe you can add also something from flatpack , snap
something like for example https://github.com/tele1/LinuxScripts/b ... alled.bash
But i am not sure if commands are correct.

For example firefox is from snap
apt-cache show firefox = working

dpkg -l | grep firefox = is empty because is from snap

flatpack = I don't know / I don't use.
Koentje
Level 4
Level 4
Posts: 472
Joined: Tue Jan 04, 2022 6:23 pm

Re: [SOLVED] Yad and "--dclick-action"

Post by Koentje »

1000 wrote:
Mon Jan 23, 2023 3:38 pm
This part
..."$buttonhomepage"...
It is beautiful :mrgreen:
Thank you. I didn't like the extra popups or notifications and i also didn't like that the button didn't do anything when no url was found. So to keep it simple.. no url, no button! :wink:

But I changed the background color to grey. Because I can't see a black font on a black background.
Ok, maybe that is theme related, because i have white text color standard.

Maybe you can add also something from flatpack , snap
I don't use flatpak or snap, so i can't do any testing with that.. if someone wants to add this to the script, be my guest!
Koentje
Level 4
Level 4
Posts: 472
Joined: Tue Jan 04, 2022 6:23 pm

Re: [SOLVED] Yad and "--dclick-action"

Post by Koentje »

I noticed that some packages have more homepages in their info. And almost all of the first ones are the right urls. So i made a slight change..

Change this line: export url=$(cat "$tmpfile" | grep "Homepage:" | awk '{print $2}')
Into this line: export url=$(cat "$tmpfile" | grep "Homepage:" | head -n1 | awk '{print $2}')

Now it picks the first one..
1000
Level 5
Level 5
Posts: 879
Joined: Wed Jul 29, 2020 2:14 am

Re: [SOLVED] Yad and "--dclick-action"

Post by 1000 »

I tried check

Code: Select all

#!/bin/bash
while read -r LINE ; do

    ListWeb=$(apt-cache show "$LINE" | grep ^"Homepage:")
    CountWeb=$(wc -l <<< "$ListWeb")

    if [[ "$CountWeb" -gt 1 ]] ; then
        echo "$LINE = $CountWeb"
    fi

done <<< $(dpkg -l | sort -fbn | tail -n +4 | grep -F "ii" | awk -F " "  '{ printf $2 "\n" }')
example output

Code: Select all

$ bash homepage 
accountsservice = 2
alsa-ucm-conf = 2
apparmor = 2
apport = 3
apport-gtk = 3
bind9-dnsutils = 2
bind9-host = 2

Code: Select all

$ apt-cache show apport-gtk | grep -i "package\|version\|home"
Package: apport-gtk
Version: 2.20.11-0ubuntu82.3
Homepage: https://wiki.ubuntu.com/Apport
Package: apport-gtk
Version: 2.20.11-0ubuntu82.1
Homepage: https://wiki.ubuntu.com/Apport
Package: apport-gtk
Version: 2.20.11-0ubuntu82
Homepage: https://wiki.ubuntu.com/Apport
So, that's true.
Koentje
Level 4
Level 4
Posts: 472
Joined: Tue Jan 04, 2022 6:23 pm

Re: [SOLVED] Yad and "--dclick-action"

Post by Koentje »

Do you have anacron installed? For some reason the double click does nothing on that one?! Strange.. i have no clue why?
1000
Level 5
Level 5
Posts: 879
Joined: Wed Jul 29, 2020 2:14 am

Re: [SOLVED] Yad and "--dclick-action"

Post by 1000 »

I can confirm problem for now only.
vimes666 heeelp ! :mrgreen:
vimes666
Level 5
Level 5
Posts: 933
Joined: Tue Jan 19, 2016 6:08 pm

Re: [SOLVED] Yad and "--dclick-action"

Post by vimes666 »

It has an apostrophe in the description, that is the cause. Delimeter hell as I said earlier..
If you think the issue is solved, edit your original post and add the word solved to the title.
Post Reply

Return to “Scripts & Bash”