YAD - Tips

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#21 Post by slavvo67 »

I just started using YAD, too. Here's one to choose an individual file.

filechoice1=$(yad --geometry700x400 --file --filename --width=600)

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#22 Post by mikeb »

--file-filter="Pburn Files|*.pba *.pbn" --filename="/path/"

shows only certain file types and starts in the /path directory...really handy

mike

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#23 Post by kjdixo »

Screenshots as bookmarks
Use YAD to implement an easy gui interface for 'CutyCapt'

http://cutycapt.sourceforge.net/

CutyCapt is a small cross-platform command-line utility to capture WebKit's rendering of a web page into a variety of vector and bitmap formats, including SVG, PDF, PS, PNG, JPEG, TIFF, GIF, and BMP.

I installed the following in Quirky Puppy Tahr 6.05
1. cutycapt
2. yad
Created a Pictures folder (writeable to) in root.
The following code will trigger the cutycapt program.

Code: Select all

#!/bin/bash

dialog=$(yad --title "Cuty Capt" --form --field="Paste address" --field="Title")
address=$(echo $dialog | awk 'BEGIN {FS="|" }{print $1}')
title=$(echo $dialog | awk 'BEGIN {FS="|" }{print $2}')
domain=$(echo $address | sed 's/www.//g' | awk 'BEGIN {FS="://" }{print $2}' | awk 'BEGIN {FS="." }{print $1}')

echo $address
echo $domain
echo $title
cutycapt --url="$address" --max-wait=5000 --out=/root/Pictures/"$domain"-"$title".png
Paste a web page url (not https - it does not seem to work in cutycapt) into the first field.
Write some memory jogger text into the second field (like a bookmark title).
Hit ok or enter and wait 5 seconds for a screenshot to appear in the Pictures folder.
It will be named [domain]-[title].png where domain is the part of the url between '://' and the first '.'
Running in console mode for the first few times echos the variables and helps you see what is happening.
Attachments
cc.jpg
(17.8 KiB) Downloaded 2553 times

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#24 Post by kjdixo »

Some additions to the previous bit of code.
1. The web page capture .png gets written to the Pictures folder then it is displayed in a YAD window, with its filename as the window title.
2. Images in the Pictures folder are time-stamped for chronological listing.

Code: Select all

#!/bin/bash

dialog=$(yad --title "Cuty Capt" --form --field="Paste address" --field="Title")
address=$(echo $dialog | awk 'BEGIN {FS="|" }{print $1}')
title=$(echo $dialog | awk 'BEGIN {FS="|" }{print $2}')
domain=$(echo $address | sed 's/www.//g' | awk 'BEGIN {FS="://" }{print $2}' | awk 'BEGIN {FS="." }{print $1}')
timestamp() {
date +"%Y-%m-%d_%H:%M:%S"
}
time=$(echo $(timestamp))
echo $address
echo $domain
echo $time
echo $title
cutycapt --url="$address" --max-wait=5000 --out=/root/Pictures/"$time"-"$domain"-"$title".png

yad --image /root/Pictures/"$time"-"$domain"-"$title".png --title /root/Pictures/"$time"-"$domain"-"$title".png
Note that sed is used to remove 'www.'

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#25 Post by kjdixo »

PDF format is better for saving large web page screen-shots.
Very often web pages are very long and the .png file will be thousands of pixels in height (file size very large).
I have found that using cutycapt to save as .pdf is much better when working with large web pages.
The pdf format chops the screen-shot into page sized chunks and the pdf viewer (Evince) displays a list of thumbnails on one side.

Code: Select all

#!/bin/bash

dialog=$(yad --title "Cuty Capt" --form --field="Paste address" --field="Title")
address=$(echo $dialog | awk 'BEGIN {FS="|" }{print $1}')
title=$(echo $dialog | awk 'BEGIN {FS="|" }{print $2}')
domain=$(echo $address | sed 's/www.//g' | awk 'BEGIN {FS="://" }{print $2}' | awk 'BEGIN {FS="." }{print $1}')
timestamp() {
date +"%Y-%m-%d_%H:%M:%S"
}
time=$(echo $(timestamp))
echo $address
echo $domain
echo $time
echo $title
cutycapt --url="$address" --max-wait=5000 --out=/root/Pictures/"$time"-"$domain"-"$title".pdf
Saving as .pdf is very quick, stable and more user friendly than .png

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#26 Post by kjdixo »

So far we have a PDF static copy of a web address, timestamped and with a descriptive filename.
Saving a web page as a pdf, alows direct copy and paste of text, not so easy with a png file.
It would also be useful to include somewhere in our saved static pdf copy of the web page, the url of the particular website and make it copyable and pasteable into a browser address bar if required.
(I am using Quirky Puppy Tahr 6.05)
1. Install pdftk
2. Replace previous bash script with this:

Code: Select all

#!/bin/bash

dialog=$(yad --title "Cuty Capt" --form --field="Paste address" --field="Title")
address=$(echo $dialog | awk 'BEGIN {FS="|" }{print $1}')
title=$(echo $dialog | awk 'BEGIN {FS="|" }{print $2}')
domain=$(echo $address | sed 's/www.//g' | awk 'BEGIN {FS="://" }{print $2}' | awk 'BEGIN {FS="." }{print $1}')
timestamp() {
date +"%Y-%m-%d_%H:%M:%S"
}
time=$(echo $(timestamp))
echo $address
echo $domain
echo $time
echo $title
cutycapt --url="$address" --max-wait=5000 --out=/root/Pictures/temp.pdf
pdftk /root/Pictures/temp.pdf dump_data > /root/Pictures/in.txt
cd /root/Pictures
sed -i '1i InfoValue: '$address'' in.txt
sed -i '1i InfoKey: Title' in.txt
sed -i '1i InfoBegin' in.txt
pdftk /root/Pictures/temp.pdf update_info in.txt output /root/Pictures/"$time"-"$domain"-"$title".pdf
Summary
cutycapt saves the web page to a temporary temp.pdf for pdftk to work with.
cutycapt saves the pdf metadata but does not include the metadata 'InfoKey: Title'.

pdftk gets a metadata dump_data and saves it to in.txt.
sed prepends 'InfoKey: Title' to in.txt.
pdftk does an update_info, using in.txt, to the output timestamped pdf file.

Now in evince the website url appears as the window title.
In evince click > file > properties and see that the website url is listed next to Title:
The evince properties dialog contents are copyable and pasteable, so you can grab the url.

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#27 Post by kjdixo »

This might be a useful alternative to the simpler 'yad --color' dialog.

Code: Select all

#!/bin/bash
dialog=$(yad --title "Select Colors" --form --field=Color1:CLR --field=Color2:CLR)
color1=$(echo $dialog | awk 'BEGIN {FS="|" }{print $1}')
color2=$(echo $dialog | awk 'BEGIN {FS="|" }{print $2}')

red1=${color1:1:2}
green1=${color1:5:2}
blue1=${color1:9:2}
color1="#"$red1$green1$blue1

red2=${color2:1:2}
green2=${color2:5:2}
blue2=${color2:9:2}
color2="#"$red2$green2$blue2
echo $color1
echo $color2
The returned variables on lines 3 and 4, $color1 and $color2 are 12 character strings preceded by a #.
I needed to extract from the 12 character string
red (1st and 2nd characters)
green (5th and 6th characters)
blue (9th and 10th characters)
and then remake the strings (concatenate the variables $red, $green, $blue) and prepend a #.
Run in console mode the first few times to observe the outputs $color1 and $color2.
Attachments
colors.jpg
(18.86 KiB) Downloaded 2436 times

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#28 Post by kjdixo »

Language Learning - Audio Flashcards with YAD
Demonstrates use of combo-box and mp3 audio files.

http://mpg123.de/

mpg123 is a real time MPEG 1.0/2.0/2.5 audio player/decoder for layers 1,2 and 3 (most commonly MPEG 1.0 layer 3 aka MP3).

I installed the following in Quirky Puppy Tahr 6.05
1. mpg123
2. yad

Created an Audio folder (writeable to) in root.
Saved 1.mp3 to 8.mp3 (weather audio files) and weather.txt in /root/Audio/weather/
Saved 1.mp3 to 8.mp3 (colors audio files) and colors.txt in /root/Audio/colors/

The following code will trigger mpg123.
It uses mpg123-alsa (automatically installed in 1. above).

Code: Select all

#!/bin/sh
# language text files stored with corresponding single word audio files

# save 1.mp3 to 8.mp3 (weather audio files) and weather.txt in /root/Audio/weather/ 
# save 1.mp3 to 8.mp3 (colors audio files) and colors.txt in /root/Audio/colors/

dialog=$(yad --width=400 --title="category" --form --field="":CB 'weather!colors' --button=gtk-ok:0)
category=$(echo $dialog | awk 'BEGIN {FS="|" }{print $1}')
echo $category

#Load weather.txt or colors.txt file lines into a bash array

OLD_IFS=$IFS
IFS=$'\n'
let line_counter=0
for line in $(cat "/root/Audio/$category/$category.txt"); do
let line_counter=$(($line_counter+1))

portuguese=$(echo $line | awk 'BEGIN {FS="<pt>" }{print $2}' | awk 'BEGIN {FS="<en>" }{print $1}')
english=$(echo $line | awk 'BEGIN {FS="<en>" }{print $2}' | awk 'BEGIN {FS="<st>" }{print $1}')

echo $category" "$portuguese" "$english
dialog=$(yad --button=LISTEN:0 --title $category --width=300 --text " "$portuguese"  "$english" ")

mpg123-alsa /root/Audio/$category/$line_counter".mp3"

done
IFS=$OLD_IFS
weather.txt

Code: Select all

<pt>ventoso<en>windy<st>
<pt>quente<en>warm<st>
<pt>ensolarado<en>sunny<st>
<pt>nevando<en>snowy<st>
<pt>chuvoso<en>rainy<st>
<pt>quente<en>hot<st>
<pt>frio<en>cold<st>
<pt>nublado<en>cloudy<st>
colors.txt

Code: Select all

<pt>preto<en>black<st>
<pt>azul<en>blue<st>
<pt>castanho<en>brown<st>
<pt>verde<en>green<st>
<pt>cinzento<en>grey<st>
<pt>vermelho<en>red<st>
<pt>branco<en>white<st>
<pt>amarelo<en>yellow<st>
I hope this is self-explanatory.
Attachments
category.jpg
(15.78 KiB) Downloaded 2392 times
weather.jpg
(12.38 KiB) Downloaded 2364 times

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#29 Post by kjdixo »

Automatically picks up folder names from Audio directory and then loads them to the combo box.
Folders of txt files and corresponding mp3 files can be added without modifying YAD combo box code.
Folder names must be single words without spaces (we can complicate it with spaces another time).

Code: Select all

#!/bin/sh
# language text files stored with corresponding single word audio files

# for example
# save 1.mp3 to 8.mp3 (weather audio files) and weather.txt in /root/Audio/weather/ 
# save 1.mp3 to 8.mp3 (colors audio files) and colors.txt in /root/Audio/colors/

cd /root/Audio
combo=$(for f in *; do [ -d "$f" ] && echo $f; done) 
combo=$(echo $combo | sed 's/ /!/g')

echo $combo

dialog=$(yad --width=400 --title="category" --form --field="":CB $combo --button=gtk-ok:0)
category=$(echo $dialog | awk 'BEGIN {FS="|" }{print $1}')
echo $category

#Load weather.txt or colors.txt file lines into a bash array

OLD_IFS=$IFS
IFS=$'\n'
let line_counter=0
for line in $(cat "/root/Audio/$category/$category.txt"); do
let line_counter=$(($line_counter+1))

portuguese=$(echo $line | awk 'BEGIN {FS="<pt>" }{print $2}' | awk 'BEGIN {FS="<en>" }{print $1}')
english=$(echo $line | awk 'BEGIN {FS="<en>" }{print $2}' | awk 'BEGIN {FS="<st>" }{print $1}')

echo $category" "$portuguese" "$english
dialog=$(yad --button=LISTEN:0 --title $category --width=300 --text " "$portuguese"  "$english" ")

mpg123-alsa /root/Audio/$category/$line_counter".mp3"

done
IFS=$OLD_IFS
Last edited by kjdixo on Tue 31 Mar 2015, 21:43, edited 1 time in total.

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#30 Post by slavvo67 »

Does anyone know how to get the standard ok and cancel buttons to work properly? I currently coded a zero in the pull down box instead, to cancel. Not a good way home....

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#31 Post by kjdixo »

Try this ... execute in console mode or copy and paste to a terminal to check the outputs

Code: Select all

yad --title="Test" --text="Test buttons" --form --field="1" --field="2" --field="3" --field="4" --button="gtk-ok:0" --button="gtk-cancel:1"

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#32 Post by slavvo67 »

Seems to work but the true test will be in a program I'm writing. - Thanks.

Slavvo67

User avatar
mikeb
Posts: 11297
Joined: Thu 23 Nov 2006, 13:56

#33 Post by mikeb »

I applied that yad folder chooser to pburn and it works a treat because you can specify the start directory and can specify the size...gtkdialogs choosers tend to be large.

I yadded pburn last year for an excersise... was quite pleased with the result.

Don't ask...its not like the pburn you know and wait for to boot :D...I forked from version one to get on the fly audio burning.

Mike

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#34 Post by slavvo67 »

The choose directory option seems to go through all the subs, as well. I found that a bit strange. For example, listing all pdf files after choosing a directory will list all the pdf's in all the subs, as well. Is it just a user issue again? LOL


slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#36 Post by slavvo67 »

What I was looking for in my above posts was a way to have the OK and Exit buttons actually work in a script. So for those non-pro's like myself, here's a partial to show how I did it.

choice2=$(yad --title='Title1' --text='Travel Items' --form --field='Show:CB' '1.Airline Tickets!2.Cars' --button="Exit:1" --button="OK:0")
ret=$?
if [[ $ret -eq 1 ]]; then
exit 0 # This exits if you hit the exit instead of OK button.
fi
numb777=${choice2:0:2}
# The above takes the first 2 characters like # 1. and allows it to # be a choice in an if .. then statement.
# For example...
if [ $numb777 = '1.' ]
then
echo "Perhaps you should go to Priceline.com"
fi

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#37 Post by kjdixo »

Thanks.
An excellent example.

It is frustrating and time consuming when there are not many examples available.
Your snippet will be very useful.

Here is YAD being used to selectively display language verb tables in an openbox menu.
This method could be used to display any images.
Clicking on a menu item opens the required image in a yad window.
Verb tables are complex and often need to be referred to.
Open and close and compare any number of images. It is nice and fast.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<openbox_menu xmlns="http://openbox.org/3.4/menu">
<menu id="root-menu" label="MENU 5">
<item label="" icon="/usr/share/icons/Tango/32x32/actions/go-home.png"><action name="Execute"><command>sh -c "cp /root/.config/openbox/menu1.xml /root/.config/openbox/menu.xml && openbox --reconfigure;xdotool mousemove_relative --polar 280 80; xdotool click 3;xdotool mousemove_relative  --polar 100 80"</command></action></item>
<item label="XFE" icon="/usr/share/icons/Tango/32x32/apps/file-manager.png"><action name="Execute"><command>xfe</command></action></item>
<item label="dar" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=dar__to-give --image /root/Verbs/ar/dar__to-give.png</command></action></item>
<item label="deitar" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=deitar__to-lay_to-put-(down)_to-put-to-bed_to-throw --image /root/Verbs/ar/deitar__to-lay_to-put-(down)_to-put-to-bed_to-throw.png</command></action></item>
<item label="deixar" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=deixar__to-let_allow_to-leave-(behind) --image /root/Verbs/ar/deixar__to-let_allow_to-leave-(behind).png</command></action></item>
<item label="dizer" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=dizer__to-say_tell --image /root/Verbs/er/dizer__to-say_tell.png</command></action></item>
<item label="estar" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=estar__to-be_temporary --image /root/Verbs/ar/estar__to-be_temporary.png</command></action></item>
<item label="falar" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=falar__to-speak --image /root/Verbs/ar/falar__to-speak.png</command></action></item>
<item label="haver" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=haver__to-be_impersonal__to-have_auxilliary --image /root/Verbs/er/haver__to-be_impersonal__to-have_auxilliary.png</command></action></item>
<item label="ir" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=ir__to-go --image /root/Verbs/ir/ir__to-go.png</command></action></item>
<item label="pedir" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=pedir__to-ask-for_to-beg --image /root/Verbs/ir/pedir__to-ask-for_to-beg.png</command></action></item>
<item label="poder" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=poder__to-be-able_can --image /root/Verbs/er/poder__to-be-able_can.png</command></action></item>
<item label="ser" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=ser__to-be_permanent --image /root/Verbs/er/ser__to-be_permanent.png</command></action></item>
<item label="ter" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=ter__to-have --image /root/Verbs/er/ter__to-have.png</command></action></item>
<item label="trazer" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=trazer__to-bring --image /root/Verbs/er/trazer__to-bring.png</command></action></item>
<item label="ver" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=ver__to-see --image /root/Verbs/er/ver__to-see.png</command></action></item>
<item label="vestir" icon="/root/.portugal.png"><action name="Execute"><command>yad --button='gtk-quit:1' --title=vestir__to-dress_wear_put-on --image /root/Verbs/ir/vestir__to-dress_wear_put-on.png</command></action></item>
</menu>
</openbox_menu>
The 'right click multiple menu' is described fully on the lxde forum . . .
xdotool is not an essential requirement, it only enhances operation of the menu.
http://forum.lxde.org/viewtopic.php?f=24&t=31525
Attachments
yad-obmenu.png
(52.48 KiB) Downloaded 1989 times

slavvo67
Posts: 1610
Joined: Sat 13 Oct 2012, 02:07
Location: The other Mr. 305

#38 Post by slavvo67 »

Hi kjdixo:

I was just starting to explore the icons so thank you for that. It looks like a lot of work / writing, though. That is the issue with adding a graphic interface, I guess. I'll let you know if I find any shortcuts.

Best,

Slavvo67

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#39 Post by kjdixo »

Really happy ... got the above 'yad openbox right-click menu - verb tables' and my earlier post's 'Language Learning - Audio Flashcards with YAD' all working on an old ASUS EeePC 2G Surf (the first model with 2GB storage).
I began by installing LXPUP 14:10 and then installed xfe (removed rox and pcmanfm) and did many more minimalist tweaks (installed earlier pmount - from quirky tahr 6.05 and tweaked pmount code to replace 'rox' with 'xfe').
I have left lxde installed for experimentation purposes and I run an openbox session (with feh [wallpaper] and stalonetray) - my choice.
So now I have a very portable, highly configurable 'language laboratory'.
The eeepc has a small screen with speakers placed either side of the screen.
I tweaked xrandr to give me 2000px x 2000px panning mode, so the small screen is not so much of a problem.
Yad is great for speed and minimalism because it will display images and ... play audio (using mpg123) without the need for big programs that take 'vital and agonising milliseconds' to startup.

@slavvo67 "It looks like a lot of work / writing, though"
10% inspiration 90% perspiration . . . as the saying goes.

kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#40 Post by kjdixo »

Very slightly different from previous combo box examples

Code: Select all

#! /bin/bash
title=$(echo "Select")

combo()
{  
choice=$(yad --title="$title" --text='Travel Items' --form --field='Show:CB' '1.Airline Tickets!2.Cars' --button="Exit:1" --button="OK:0")

ret=$?
if [[ $ret -ge 1 ]]; then
exit 0
fi

choice=$(echo $choice | awk 'BEGIN {FS="|" }{print $1}')
echo $choice
title=$(echo "$choice")
}

while true ; do
combo
done

Post Reply