YAD - Tips

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#201 Post by MochiMoppel »

I'm using smokey01's yad-0.36.2.
step wrote:the forum seems to have added a space character at the end of each line in MochiMoppel's script. Make sure to delete such characters or yad will print an error message "No column titles specified for List dialog."
It's not the forum, it's your browser. Some browsers (Opera!) add spaces to the end of each line when you copy text from the browser window. An old and well known (but often forgotten :wink: ) bug.

Not a yad issue, but a naming annoyance: Many icons in the directory /usr/local/lib/X11/mini-icons are named mini-something.xpm or mini.something.xpm. When sorting by name they all start with mini. Not very useful. I prefer to strip the mini- or mini. from the name. RIght after the find command I add the line

Code: Select all

FOUND=${FOUND//${SEP}mini[-.]/$SEP}
(note that I edited the original code. Now it's SEP instead of IFS - though the latter would work). As a result it's easier to find similar icons, e.g. all home icons:
Attachments
eliminated_mini.png
(36.15 KiB) Downloaded 1862 times
Last edited by MochiMoppel on Mon 16 May 2016, 08:16, edited 1 time in total.

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#202 Post by step »

@smokey01, Got it, thanks. It does fix the issue.

@MochiMoppel, you're right, it's Opera not the forum. I'm appalled that the new Opera, which is based on Chromium, still has that bug. Bug or feature? Featured-bug compatibility?

It's a nice script. On Fatdog710 gxmessage and exiv2 aren't native. So I changed one line to replace both gxmessage and exiv2 with runexiftool, which is included in my :D exiftool package for Fatdog64 (in contrib repo, needs perl package or devx)

Code: Select all

function showinfo   { source $TMPFILE ; runexiftool "$IMGPATH" 2>/dev/null;} ; export -f showinfo
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#203 Post by MochiMoppel »

So --list is pretty powerful, but still it seems impossible to refresh the list while the dialog is running.

This trick can be achieved with the --form option and the special @ character, which makes it possible to send output to a field instead of stdout. For the demo I use a field of type TXT (multiline text) and turn it into a poor man's list.

The demo copies files selected in ROX into a target directory. To see this in action, open a ROX window and run the demo. If you had selected files already, they will appear in the multiline field, if not, do it now and push the "Refresh" button (do not drag&drop the files!). The field should update. Make a different selection and press "Refresh" again. If all goes well the field will replace the old contents with a list of the selected files. Choose a target directory and press "Copy" if you like. The listed files/directories will be copied to the target. Of course you can delete files from the list to exclude them from copying.

Code: Select all

#!/bin/sh
function multiline { 
  X=$(xclip -o)
  [[ ${X::1} = / ]] && echo  "${1}${X// \//\\n/}" || echo -e "\n\tSelect files in ROX, then push Refresh button"
} ; export -f multiline 

yad --width=500 --form  \
--field="Source files":TXT                      "$(multiline)" \
--field="Target directory":CDIR                 "/archive" \
--field="Refresh file list!gtk-refresh":FBTN    '@bash -c "multiline 1:"' \
--field="Copy to target!gtk-copy":FBTN          'bash  -c "F=%1; F=${F//\\\n/|}; IFS=\|; rsync -a $F %2"'
Attachments
yad_multilinetext.png
(35.95 KiB) Downloaded 1786 times

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

yad drop list collector revisited

#204 Post by step »

This conversation started here:
stemsee wrote:Here is a yad drag'n'drop pane that sits on your desktop and adds the path of everything you drop there to /root/list.txt...
smokey01 wrote:stemsee, nice. I prefer not to background it and keep the decorations.
Have a look at this?

Code: Select all

yad --geometry=100x100 --on-top --text="Drag-n-drop directory or files here \nThey will be added to ref list" --no-buttons --skip-taskbar --dnd --cmd echo $1 | sed 's/^.......//' >> /root/references.txt
stemsee wrote:That would have been a better solution. i was trying to find the expression to get rid of 'file://' yesterday!

However for some reason nothing is getting added to the file /root/references.txt with your code as it is! sed 's/^' isn't that to insert?
smokey01 wrote:Once you close the dnd gui the files will be written to your file.
Well I found a way to update the list file in "real time" without the need to close the drag-and-drop window.

Code: Select all

script -q -c 'yad --geometry=100x100 --on-top --text="Drag-n-drop items here\nThey will be added to ref list\n---" --no-buttons --skip-taskbar --dnd --cmd printf "%s\n" "$1" | sed -e "s,^file://,,"' >> /root/references.txt
How this works is better explained in a SO article - URL's listed further down. I also changed sed to delete "file://" only. Since you can also drop URLs from your browser, the protocol selector, http, ftp, ... is preserved in that case.
It would be nice to keep the drop target and the result list together in a single mini-app. Here it is. It uses the notebook interface. Save the code to a file, don't be tempted to run it directly from the command line.

edit: Read the CAVEAT section in this post
The final script is in this other post.

Code: Select all

#!/bin/sh
# {da,a}sh compatible.

lf=${SHOM:-$HOME}/references.txt
lf=/tmp/references.txt # or even /tmp/references-$$.txt for separate sessions.

# Accepted items: files, folders, URLs from browser. Folders won't be expanded.

# https://unix.stackexchange.com/a/61833
#script -q -c 'yad --geometry=100x100 --on-top --text="Drag-n-drop items here\nThey will be added to ref list\n---" --no-buttons --skip-taskbar --dnd --cmd printf "%s\n" "$1" | sed -e "s,^file://,,"' >> "$lf"

# Don't run these lines from terminal or you may incur bug https://sourceforge.net/p/yad-dialog/tickets/205/
script -q -c 'yad --plug='$$' --tabnum=1 --text="Drag-n-drop items here\nThey will be added to ref list\n---" --dnd --cmd printf "%s\n" "$1" | sed -e "s,^file://,,"' >> "$lf" &
script -q -c 'tail -f "'"$lf"'" | yad --plug='$$' --tabnum=2 --listen --tail --text-info' &
yad --no-buttons --skip-taskbar --notebook --key=$$ --tab="Drop" --tab="List"
Attachments
yad_drop_list.png
(10.39 KiB) Downloaded 1599 times
yad_drop_list.tar.gz
same script as above
(624 Bytes) Downloaded 205 times
Last edited by step on Mon 20 Jun 2016, 21:46, edited 2 times in total.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

stemsee

#205 Post by stemsee »

can you check your script ... it doesn't work here!

Still it will be great to get your improvements. After writing that script I developed the dnd idea to become DnD-Widget which is my desktop file router, which acceps dirs and files and sends them to default apps; media fies, documents, scripts, html, pdf, etc That script also needs a realtime updater in several places. Maybe you could implement your code there too ... that woud be great!

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

'script' and yad CAVEAT

#206 Post by step »

stemsee wrote:can you check your script ... it doesn't work here!
It works here. Can you tell me which puplet and show terminal output (although I suppose you had none). Can you please test just the commented line that starts with "#script" too? That way we can make sure your puplet can run 'script' correctly.

CAVEAT
However, I just found out a caveat with 'script' itself.
man script wrote: BUGS
script places everything in the log file, including linefeeds and backspaces. This is not what the naive user expects.
So I went looking for possible impacts of this bug and sadly I found one. 'Script' ends each line with \r\n, that means that all lines in /tmp/references.txt end with \r. So you have to keep that in mind when you're re-using the real-time output list, you need to strip the \r before you can use the filepath. Gotcha: your text editor may not actually display the \r, but it's there! You 'cat -v' to check the file.

Note that this issue can't be fixed by simply piping 'script' into 'sed' to delete the \r character. If we did, we would undo the 'script' magic of unbuffering the whole command, and we'd be back to square one. There are ways around this, but I suspect none of them revolves around using 'script' anymore. I need to think about it.

Meanwhile, if you can strip \r in your downstream processing, the 'script' unbuffered solution should work for you just fine.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

solution for the 'script' command CAVEAT

#207 Post by step »

I found a way around the whole unbuffered issue. For starters, fix your yad options! --dnd --cmd isn't valid.
man yad wrote: Drag-and-Drop box options
--command=CMD
Run command when data received. Data strings pass to command as an agrument. By default data just prints to stdout.
So it's --dnd --command. If you want to run a shell builtin, like echo, then use

Code: Select all

--command "bash -c 'echo \$0"
but better use printf instead of echo so you're not exposed to issues with file names that start with '-e'. And of course $0 should be quoted.

Then we can get rid of the buffering issue by using bash shell modifiers to delete "file://".

Finally there's the whole shell escape character ordeal... In summary, the one-liner that works for me is:

Code: Select all

yad --geometry=100x100 --on-top --text="Drag-n-drop items here\nThey will be added to ref list\n---" --no-buttons --skip-taskbar --dnd --command "bash -c 'printf %s\\\\n "\${0//file:\/\//}"'" >> /tmp/reference.txt
Adapting to the notebook script is left as an exercise for the reader :) (sorry, I'm running out of time).
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

yad drop list collector revisited

#208 Post by step »

This is the final version of the drop list collector tabbed dialog. It solves open issues, and it by-passes caveats.

Code: Select all

#!/bin/sh
# {da,a}sh compatible.

lf=${SHOM:-$HOME}/references.txt
lf=/tmp/references.txt # or even /tmp/references-$$.txt for separate sessions.

# Accepted items: files, folders, URLs from browser. Folders won't be expanded.
# Dropped icon GROUP output order may not match ROX-Filer's icon order.

# One-liner version
#yad --geometry=100x100 --on-top --text="Drag-n-drop items here\nThey will be added to ref list\n---" --no-buttons --skip-taskbar --dnd --command "bash -c 'printf %s\\\\n \"\${0//file:\/\//}\"'" >> "$lf"

# Two-tab version
# Don't run these lines from terminal or you may incur bug
# https://sourceforge.net/p/yad-dialog/tickets/205/
yad --plug=$$ --tabnum=1 \
  --text="Drag-n-drop items here\nThey will be added to ref list\n---" \
  --dnd --command "bash -c 'printf %s\\\\n \"\${0//file:\/\//}\"'" \
  >> "$lf" &

tail --pid=$$ -f "$lf" |
awk '{fflush();print;fflush()}' |
yad --plug=$$ --tabnum=2 --listen --tail --text-info &

yad --no-buttons --skip-taskbar --notebook \
  --key=$$ --tab="Drop" --tab="List"
Attachments
yad_drop.tar.gz
(689 Bytes) Downloaded 226 times
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

stemsee

#209 Post by stemsee »

The gui pops up but nothing gets added to references.tx, in fact that file doesn't get created! Maybe it needs to be in existence to start with.

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#210 Post by step »

Open terminal in the folder where you placed yad_drop.sh, run
sh -x ./had_drop.sh
Copy entire output into your reply.
Tell me on which puplet you ran the command.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

stemsee

#211 Post by stemsee »

Dpup wheezy

Code: Select all

sh -x ./yad_drop.sh 
+ lf=/root/references.txt
+ yad --plug=6714 --tabnum=1 '--text=Drag-n-drop items here\nThey will be added to ref list\n---' --dnd --command 'bash -c '\''printf %s\\n "${0//file:\/\//}"'\'''
+ awk '{fflush();print;fflush()}'
+ yad  --no-buttons --undecorated --skip-taskbar --notebook --key=6714 --tab=Drop --tab=List
+ tail --pid=6714 -f /root/references.txt
+ yad --plug=6714 --tabnum=2 --tail --text-info
tail: unrecognized option '--pid=6714'
BusyBox v1.21.0 (2013-02-18 15:57:06 WST) multi-call binary.

Usage: tail [OPTIONS] [FILE]...

Print last 10 lines of each FILE (or stdin) to stdout.
With more than one FILE, precede each with a filename header.

	-f		Print data as file grows
	-s SECONDS	Wait SECONDS between reads with -f
	-n N[kbm]	Print last N lines
	-c N[kbm]	Print last N bytes
	-q		Never print headers
	-v		Always print headers

N may be suffixed by k (x1024), b (x512), or m (x1024^2).
If N starts with a '+', output begins with the Nth item from the start
of each file, not from the end.

got it

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#212 Post by step »

You need to install the Gnu version of 'tail'. If you can't then you can still delete option --pid from tail. However, if you do, when yad terminates the entire upstram pipeline (tail + awk) is left running in the background. You can terminate those processes by using ps+grep, or pgrep or pidoff to find their process ids and kill them. It isn't very clean and it's prone to error.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

stemsee

#213 Post by stemsee »

Yep! That works :-)

This is great! Your code will be useful elsewhere too!

Cheers!

My tweaked version

Code: Select all

#!/bin/sh
# {da,a}sh compatible.

lf=$HOME/references.txt

yad --plug=$$ --tabnum=1 --text="Drag-n-drop items here\nThey will be added to ref list\n---" --dnd --command "bash -c 'printf %s\\\\n \"\${0//file:\/\//}\"'" >> "$lf" &
tail -f "$lf" | awk '{fflush();print;fflush()}' | yad --plug=$$ --tabnum=2 --tail --editable --text-info &
yad --geometry=300x200+50+500 --on-top --no-buttons --undecorated --skip-taskbar --notebook --key=$$ --tab="Drop" --tab="List"
This will work excellently for D'n'D-Widget desktop file router as each list will be able to have its own editable list tab!
Attachments
capture13611.png
(5.04 KiB) Downloaded 1497 times
capture18177.png
(4.42 KiB) Downloaded 1487 times
capture13110.png
(2.73 KiB) Downloaded 1476 times

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#214 Post by MochiMoppel »

step wrote:You need to install the Gnu version of 'tail'. If you can't then you can still delete option --pid from tail. However, if you do, when yad terminates the entire upstram pipeline (tail + awk) is left running in the background. You can terminate those processes by using ps+grep, or pgrep or pidoff to find their process ids and kill them. It isn't very clean and it's prone to error.
I think it would be simpler without tail+awk, avoiding some pitfalls. Here is a variant that uses tee. The List tab would always open empty and list only the dropped files of the current session and not those from previous session, but this might even be an advantage.

This script should run without problems from the command line. I kept your script largely unchanged. I only removed the (now useless) --listen option. I didn't understand what you mean by "better use printf instead of echo so you're not exposed to issues with file names that start with '-e'.". I see no problems with echo. Would add to simplification.

Code: Select all

#!/bin/bash
lf=/tmp/references.txt

yad --plug=$$ --tabnum=1 \
--text="Drag-n-drop items here\nThey will be added to ref list\n---" \
--dnd --command "bash -c 'printf %s\\\\n "\${0//file:\/\//}"'" | tee -a "$lf" | \
yad --plug=$$ --tabnum=2 --tail --text-info & 

yad --no-buttons --skip-taskbar --notebook \
--key=$$ --tab="Drop" --tab="List"

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#215 Post by step »

@MochiMoppel,

using tee in that fashion is an excellent idea. Possible improvement: tee $1 instead of tee -a so the user can choose to restart /tmp/references.txt from scratch or to append to the existing file.

As regards my comment about -e, it's a corner case regarding a file named "-e" (or other valid echo options):

Code: Select all

# echo "-e"

# printf %s\\n "-e"
-e
#
but in hindsight, it should never happen because yad outputs absolute paths.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#216 Post by MochiMoppel »

step wrote:@MochiMoppel,
using tee in that fashion is an excellent idea.
Thanks, but I have an even better idea: no tee. No external utilities needed - apart from yad, of course.
I also prefer paned instead of notebook. Switching back and forth in a notebook between the Drop tab and the List tab can be a nuisance. Here a version using paned:

Code: Select all

#!/bin/bash
function dostuff {
    STUFF="${0//file:\/\//}"
    printf %s\\n "$STUFF" >> /tmp/references.txt
    printf %s\\n "$STUFF"
}; export -f dostuff

yad --dnd       --plug=$$ --tabnum=1  --command "bash -c dostuff"  --text="\n\nDrop\nhere" --text-align=center| \
yad --text-info --plug=$$ --tabnum=2  --tail  & 
yad --paned     --key=$$  --orient=hor --splitter=100 --no-buttons --width=300
Attachments
yad_paned.png
(3.66 KiB) Downloaded 1459 times

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#217 Post by step »

Cool! Great to punt one more process.
Good to have the paned option, but tabs work well for me, too. In the tabbed version if you hover the loaded mouse pointer over the drop tab label for a couple of seconds, yad auto-switches the tab. You can drop on "Drop" to append to /tmp/references.txt and to "List", or you can drop on "List" to just append to "List" and leave /tmp/references.txt untouched.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

stemsee

#218 Post by stemsee »

Mochimopel and step

How would it be possible to alter the /tmp/references.txt from the --text-info pane once --editable option is assigend? That is I can edit the text in the yad pane and I then want that edit reflected in the actual file /tmp/references.txt.

cheers
stemsee

stemsee

#219 Post by stemsee »

A simple way is to add a 2nd dnd pane to copy edited text from the list pane to. Then in tab 4 update to run function to replace references to text. But list is not updated.

Code: Select all

#!/bin/sh 
function editor () {
	cp -f /tmp/ref /root/references.txt
	cat /tmp/ref
}
export -f editor
lf=$HOME/references.txt 
yad --plug=$$ --tabnum=1 --text="Drag-n-drop items here\nThey will be added to ref list\n---" --dnd --command "bash -c 'printf %s\\\\n \"\${0//file:\/\//}\"'" >> "$lf" & 
tail -f "$lf" | awk '{fflush();print;fflush()}' | yad --plug=$$ --tabnum=2 --editable --listen --tail --text-info &
yad --plug=$$ --tabnum=3 --text="Drag-n-drop edited refs here\nThey will replace ref list\n---" --dnd --command "bash -c 'printf %s\\\\n \"\${0//file:\/\//}\"'" > /tmp/ref & 
yad --plug=$$ --tabnum=4 --form --field="update:fbtn" "bash -c editor" &
yad --no-buttons --skip-taskbar --notebook --key=$$ --tab="Drop" --tab="List" --tab="Edited DnD" --tab="Result"

stemsee

#220 Post by stemsee »

Here is my double dnd/list gui. Only problem is closing the first parent gui, I don't know how to get the pid, even when it is the last backgrounded process.

Code: Select all

#!/bin/sh 
# refdnd reference or what-have-you list builder/editor
#by stemsee, step and mochimopel 2016
lf=$HOME/refs1.txt
lf2=$HOME/refs2.txt

function editor () {
lf=$HOME/refs1.txt
lf2=$HOME/refs2.txt
[[ "$lf2" ]] && mv "$lf2" "$lf"
refdnd
}
export -f editor

function dostuff {
	lf2=$HOME/refs2.txt
    STUFF="${0//file:\/\//}" 
	printf %s\\n "$STUFF" > "$lf2"
    printf %s\\n "$STUFF" 
}
export -f dostuff

function blank () {
lf=$HOME/refs1.txt
lf2=$HOME/refs2.txt
fix=$(date | awk '{print $3,$2,$4}' | tr ' ' '_')
cp "$lf" "$lf"."$fix"
cp "$lf2" "$lf2"."$fix"
rm -f "$lf"
rm -f "$lf2"
touch "$lf"
touch "$lf2"
refdnd
}
export -f blank

yad --plug=$$ --tabnum=1 --text="Drag-n-drop items here\nThey will be added to\n/root/refs1.txt list\n---" --dnd --command "bash -c 'printf %s\\\\n \"\${0//file:\/\//}\"'" >> "$lf" &
tail -f "$lf" | awk '{fflush();print;fflush()}' | yad --plug=$$ --tabnum=2 --editable --listen --tail --text-info &
yad --plug=$$ --tabnum=3 --text="Drag-n-drop edits from refs1\nOr refs2 / any source\nThey will create /root/refs2.txt\n---" --dnd --command "bash -c dostuff" > "$lf2" & 
yad --plug=$$ --tabnum=5 --form --field="Replace refs1 with refs2:fbtn" "bash -c editor" --field="Save/'Start New' refs:fbtn" "bash -c blank" --field="Save Ref lists by date in /root:btn" &
tail -f "$lf2" | awk '{fflush();print;fflush()}' | yad --plug=$$ --tabnum=4 --editable --listen --tail --text-info &
yad --window-icon=/usr/share/pixmaps/leafpad.png --geometry=80x80+80+80 --no-buttons --undecorated --on-top --notebook --key=$$ --title="Double Reference Lists in /root. Just DragnDrop text/files/dirs/WebAddresses to One of Two DnD tabs" --tab="DnD1" --tab="refs1" --tab-pos=bottom --tab="DnD2" --tab="refs2" --tab="Save"
Last edited by stemsee on Mon 27 Jun 2016, 10:39, edited 1 time in total.

Post Reply