YAD - Tips

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
kjdixo
Posts: 153
Joined: Sun 13 Sep 2009, 21:13

#16 Post by kjdixo »

Hi
B.K.Johnsohn wrote
1. What is the default download directory?
2. Can it be changed/ If yes, how?
As it stands my code downloads videos to the root folder.
To find out how to change that, we need to delve into the youtube-dl documentation
https://github.com/rg3/youtube-dl/blob/ ... fic-folder
FAQ
How do I put downloads into a specific folder?

Use the -o to specify an output template, for example -o "/home/user/videos/%(title)s-%(id)s.%(ext)s".
If you want this for all of your downloads, put the option into your configuration file.
https://github.com/rg3/youtube-dl/blob/ ... figuration

For our example:
Make sure you first create a Videos folder (that you can write to) in root

Code: Select all

#!/bin/bash

dialog=$(yad --title "You Tube Download" --form --field="Paste address")

address=$(echo $dialog)

youtube-dl "$address" -o "/root/Videos/%(title)s-%(id)s.%(ext)s" | yad --title "Download" --progress --pulsate
You can easily write extra YAD dialogs to control the parameters and download location.

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

#17 Post by kjdixo »

@ slavvo67
Does anyone have a simple script to choose a directory via pop-up box?
I've been using Zenity but Slacko doesn't have that built in.
see YAD manual 'form options'

http://rpm.pbone.net/index.php3/stat/45 ... /nazwa/yad

This method works

Code: Select all

#!/bin/bash
dialog=$(yad --title "Select Directory" --form --field=Choose:DIR)
echo $dialog
If you run in console mode it will echo the directory path.
Attachments
dir1.jpg
(18.57 KiB) Downloaded 2653 times

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

#18 Post by slavvo67 »

Thank you. I think all recent Puppies have YAD but not all have Zenity (i.e. Slacko), so it's time for me to switch my scripts that use Zenity over to YAD. However, your example adds "|" at the end of the directory, so if I try to cd to $directory1 or whatever variable I choose, it will not work as it will create $directory1 as /mnt/sda2/directoryname|

Instead, $directory1 should reflect: /mnt/sda2/directoryname

You still put me on the right track, with the following example from Smokey01 working:

cd /
directory1=$(yad --file --directory --width=600 --height=400)
cd $directory1

Thanks,

Slavvo67

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

#19 Post by kjdixo »

Thanks for your solution inspired by Smokey01
http://smokey01.com/help/yad-tips-0.0.1.ncd.tar.xz

I missed the "|" because I was in too much hurry to post an answer and did not see it..
Remove the "|" easily with this code.

Code: Select all

#!/bin/bash

dialog=$(yad --title "Select Directory" --form --field=Choose:DIR)
echo $dialog | awk 'BEGIN {FS="|" } { print $1 }'
Inspired by
http://www.linux-magazine.com/Online/Bl ... s-with-YAD
I recommend your dialog example slavvo67.
It is quicker and easier to use.

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

#20 Post by mikeb »

yad --file --directory --width=600 --height=400
ah neat...was looking for that some time ago :)

I must be testing an older yad..it wants --file-selection .....

great thread... :)

mike

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


Post Reply