KDE: sending files to a script from the browser
Joel Hammer
joel
Mon May 17 11:56:19 PDT 2004
Well, to follow up on my own question.
In windows 2000 scripting, when you use the file explorer to highlight
files or directories, and drop them onto a script, the files are
accessible sequentially in a file system object. This is a pain but
it works, allowing your script to process each file or directory
sequentially. There are limits to how long the input can be, but it
works as you would expect.
With KDE, when you highlight and open files with an application, KDE
stupidly (IMHO) opens multiple versions of the application simultaneously.
So, highlight 50 jpgs and send them to gimp or xv, and you will got 50
instances of gimp or xv. (Even with remote-gimp).
Well, anyway, if you want a bash script to sequentially process those
highlighted files, you seem to be out of luck.
So, I spent two evenings (don't laugh) and hacked a bash solution,
since I write bash scripts, although poorly.
I call this the swarm technique, although there may be a proper name for it.
It works like this:
1. Two or more (many more) identical instances of a bash script are
created by highlighting files with KDE and sending them to an application
(The script). Each script processes a different file.
2. After a decent delay each script looks at the process list with ps
ax. sed and grep, and filters out the identical scripts. The current
script does not filter out is own name. It exclude the file name it is
working on. I suppose checking the process ID would have been better. It
then checks only the first PID in this list of jobs.
3. If a script sees that it has the lowest PID, it finishes
execution. All other scripts will sleep for two seconds before
re-reading the process list.
4. etc.
Now, these scripts will execute in order of their PID's. KDE, amazingly
enuf, doesn't order them as you would expect. So, two strikes on KDE.
Well, this script seems to work. I have included the "swarm" script below, which is
generic. Name it anything you want and it should work, since it uses $0 to
find the other instances of itself.
As an added treat, I have included a function which opens jpeg files
in xv, extracts the comment with the imagemagick utility identify, and
lets you edit the comment in vim. If you want to not change the comment,
just :qa!. Otherwise, make the edits and :wq! If you want to erase the
comment, delete everything in the file (1GdG works fine), except for a
single blank or return. It then uses convert to modify the comment.
You can guess my the frequent occurrence of "root" in this script that I
am running lindows! Some people are shameless. You would likely change
that. I may even change that!
If I could remember the command to capture the process ID of a called
program, I could kill xv more elegantly then with a killall.
This thing needs an abort function that kills the sleeping scripts easily
in case you want to stop editing the files, or whatever.
Any suggestion appreciated.
Joel
#!/bin/bash
# =========Convoluted comment editing function=====================
editcomment()
{
MAGICK_HOME=/usr/bin/ImageMagick-5.5.7
export MAGICK_HOME
echo "This is the current comment in this file" > junkheader.txt
echo "This was the last comment used " > junktail.txt
rm /root/.junkcomment.swp
rm /tmp/junk
mv /root/junkcomment /root/junklastcomment
xv $1 &
konsole -e /usr/bin/vim /root/junkcomment \
-c "r junkheader.txt" \
-c "r ! identify -verbose $1 | \
sed -n \"/comment/,/signature/p\" | \
sed \"s/ *comment: *//\" | \
sed \"s/signature.*/\n/\" " \
-c "r junktail.txt" \
-c "r junklastcomment"
killall xv
[ -s /root/junkcomment ] && {
convert -comment "`cat /root/junkcomment`" $1 /tmp/file
cp /tmp/file $1
}
}
# ============Start of generic swarm script==================
j=0
while [ "$j" -le 1 ]
do
sleep 2
ps ax | grep "/bin/bash $0" | sed "/grep/d" | grep -v "$1" > junk$$
read i < junk$$
k=`echo "$i" | cut -f1 -d" "`
[ -z "$k" ] || [ $$ -le "$k" ] && {
# ===========Put your function here =========================
editcomment $1
# ==========================================================
j=2
}
done
rm junk$$
More information about the Linux-users
mailing list