Solved - Modify Trash to work with spaces in path or name?
Posted: Sat 18 Aug 2007, 09:43
Solved - everything seems to work now. I've updated the code here.
This is an update to the Trash ROXapp so that it will work with files that have spaces in their name or path. Please test.
I'll also do a version that shows you an item in the trash when you click on it, instead of offering to restore it, and that has new entries on the context menu to delete the item, and to view the item.---Edit - this new version is in the next post---
--------------------------
Original post
I'm trying to modify the Trash ROXapp so that it will work with files that have spaces in their name or path (This is one thing that really annoys me in Puppy, and I think it would be a great improvement for newbies). It almost all works, but when I try to restore an item with spaces in its name (it is OK with spaces in the path) I get something like this:
I can't figure out where the /1 is coming from - I could just strip it off, as it always seems to be /1, but that doesn't seem very proper. I don't really know Bash, and I'm not much of a programmer, so can anyone fix the code please?
-------------------------
We should probably also add a "delete this item" feature, as at the moment if a user goes into the trash and decides the want to delete the only item in there, they're likely to delete it the way you normally delete things, rather than emptying the trash, in which case the trash icon does not get changed to the empty one.
I've attached a .pet below, or here is the [updated] code:
/usr/local/apps/Trash/AppRun:
/usr/local/apps/Trash/template/temp-AppRun:
This is an update to the Trash ROXapp so that it will work with files that have spaces in their name or path. Please test.
I'll also do a version that shows you an item in the trash when you click on it, instead of offering to restore it, and that has new entries on the context menu to delete the item, and to view the item.---Edit - this new version is in the next post---
--------------------------
Original post
I'm trying to modify the Trash ROXapp so that it will work with files that have spaces in their name or path (This is one thing that really annoys me in Puppy, and I think it would be a great improvement for newbies). It almost all works, but when I try to restore an item with spaces in its name (it is OK with spaces in the path) I get something like this:
Code: Select all
mv: cannot stat `/root/.Trash/2 3-3565/1': No such file or directory
-------------------------
We should probably also add a "delete this item" feature, as at the moment if a user goes into the trash and decides the want to delete the only item in there, they're likely to delete it the way you normally delete things, rather than emptying the trash, in which case the trash icon does not get changed to the empty one.
I've attached a .pet below, or here is the [updated] code:
/usr/local/apps/Trash/AppRun:
Code: Select all
#!/bin/bash
# July 10, 2005 - Dan Van Wormer - Puppy Linux
# Create the >Trash directory if it does not already exist.
[ -d /root/.Trash ] || mkdir /root/.Trash
# Determine the path to this application.
APPDIR=`dirname "$0"`
cd "$APPDIR"
APPDIR="`pwd`"
# Play the trash sound
function PlaySound
{
# See if /dev/dsp is in use. If it is then do not play the sound file.
fuser /dev/dsp
if [ $? -eq 0 ];then #=0 if found.
exit
else
play $APPDIR/systemmsg.wav
fi
exit
}
function EmptyIcon
{
cp -f "$APPDIR/trashcan_empty.png" "$APPDIR/.DirIcon"
cd ${HOME}
rox -x $APPDIR
}
function FullIcon
{
cp -f "$APPDIR/trashcan_full.png" "$APPDIR/.DirIcon"
cd ${HOME}
rox -x $APPDIR
}
# Find the name of the file.
function notAForwardSlash
{
fSlash=/
if [[ $1 != $fSlash ]]
then
return 0
else
return 1
fi
}
function getFileName
{
STRING="$1"
LENGTH=${#STRING}
for ((n=0;n <= $LENGTH; n++))
do
CHAR=${STRING:$n:1}
if notAForwardSlash $CHAR
then
FileName=$FileName$CHAR
else
FileName=""
fi
done
}
function MoveToTrash
{
getFileName "$m"
# Create a directory for the trashed item.
mkdir "/root/.Trash/$FileName-$$"
# move the item to the Trash directory.
mv "$m" "/root/.Trash/$FileName-$$"
# Copy the icon for the trashed item.
cp -f "$APPDIR/template/trash.png" "/root/.Trash/$FileName-$$/.DirIcon"
# Copy the AppInfo file over.
cp -f "$APPDIR/template/AppInfo.xml" "/root/.Trash/$FileName-$$/AppInfo.xml"
# Write the new AppRun file with.
gawk -v var="PATH" -v name="$m" -v trash="$APPDIR" '{sub(var, name)}
{sub("TRASH", trash)}
{sub("TRASH", trash); print}' $APPDIR/template/temp-AppRun > "/root/.Trash/$FileName-$$/AppRun"
# Make it executable.
chmod 777 "/root/.Trash/$FileName-$$/AppRun"
}
function RemoveOldRoxIcon
{
while grep ">$m<" /root/Choices/ROX-Filer/PuppyPin
do
number=`grep -m 1 ">$m<" /root/Choices/ROX-Filer/PuppyPin`
# Write the new PuppyPin file with the trashed item removed.
gawk -v var="$number" '{sub(var, ""); print}' /root/Choices/ROX-Filer/PuppyPin > /root/Choices/ROX-Filer/PuppyPin1
# Rename the new PuppyPin file
mv /root/Choices/ROX-Filer/PuppyPin1 /root/Choices/ROX-Filer/PuppyPin
# Restart the pinboard.
rox -p /root/Choices/ROX-Filer/PuppyPin
done
}
# If they chose the "Look in the Trash" option.
if [ "$1" = "-look" ]
then
exec rox /root/.Trash
# If they chose the "Empty the Trash" option.
elif [ "$1" = "-empty" ]
then
# See what is in the trash.
stuff=`ls /root/.Trash`
# Write the confirmation message.
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Delete:21,Cancel" -center -geometry 300x300 -title "Confirm Delete" "$stuff"
# If they chose to cancel.
[ $? -eq 21 ] || exit
# If they chose to delete.
rm -r /root/.Trash/*
EmptyIcon
PlaySound
# If they chose to "Quickly Empty the Trash".
elif [ "$1" = "-empty-quick" ]
then
rm -r /root/.Trash/*
EmptyIcon
PlaySound
else
# Check to see if they clicked on the application or sent a file or directory to be deleted.
test -sd "$@"
if [ "$?" = "0" ]
then
# If they just clicked on the icon.
exec rox /root/.Trash
else
# If they sent something to the trash can.
for m in "$@"
do
# Test to see if the item is a symbolic link.
if [ -h "$m" ]
then
# Write the confirmation message.
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Yes:21,No" -center -geometry 550x90 -title "Confirm Action" "The item $m is a Symbolic Link.
This will move the Symbolic Link to the Trash and leave the original item alone.
Do you want to continue?"
# If they chose to cancel.
[ $? -eq 21 ] || exit
fi
match=`grep ">$m<" /root/Choices/ROX-Filer/PuppyPin`
# Test $match to see if the item had a link to it on the pinboard.
test "$match"
if [ "$?" = "0" ]
then
# Write the confirmation message.
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Yes:21,No" -center -geometry 560x90 -title "Confirm Action" "There is one or more Desktop shortcuts pointing to $m
This will move the original item to the Trash and remove the shortcut(s) from the Desktop.
Do you want to continue?"
# If they chose to cancel.
[ $? -eq 21 ] || exit
while check=`grep ">$m<" /root/Choices/ROX-Filer/PuppyPin`
do
OldNumber=`grep -c ">$m<" /root/Choices/ROX-Filer/PuppyPin`
rox --RPC << EOF
<?xml version="1.0"?>
<env:Envelope xmlns:env="http://www.w3.org/2001/12/soap-envelope">
<env:Body xmlns="http://rox.sourceforge.net/SOAP/ROX-Filer">
<PinboardRemove>
<Path>$m</Path>
</PinboardRemove>
</env:Body>
</env:Envelope>
EOF
# Check to see if the entry was removed.
NewNumber=`grep -c ">$m<" /root/Choices/ROX-Filer/PuppyPin`
if [ $OldNumber = $NewNumber ]
then
# If SOAP did not work then remove the icon manualy.
RemoveOldRoxIcon "$match"
fi
done
fi
MoveToTrash "$m"
done
FullIcon
PlaySound
fi
fi
fi
Code: Select all
#!/bin/bash
# March 26, 2006 - Dan Van Wormer - Puppy Linux
# Determine the path to this application.
APPDIR=`dirname "$0"`
cd "$APPDIR"
APPDIR="`pwd`"
# Find the name of the file.
function notAForwardSlash
{
fSlash=/
if [[ $1 != $fSlash ]]
then
return 0
else
return 1
fi
}
function getFileName
{
STRING="$1"
LENGTH=${#STRING}
for ((n=0;n <= $LENGTH; n++))
do
CHAR=${STRING:$n:1}
if notAForwardSlash $CHAR
then
FileName=$FileName$CHAR
else
FileName=""
fi
done
}
# Get the name of the item in the Trash.
getFileName "PATH"
function RestoreItem
{
# Restore the item.
mv "$APPDIR/$FileName" "PATH"
# See what is in the trash.
stuff=`ls /root/.Trash`
getFileName "$APPDIR"
if [ "$FileName" = "$stuff" ]
then
cp -f "TRASH/trashcan_empty.png" "TRASH/.DirIcon"
rox -x TRASH
fi
# Delete this file and directory.
rm -fr "$APPDIR"
}
# If they chose the "Restore" option.
if [ "$1" = "-restore" ]
then
# See if there is already a file or folder of the same name in the same location.
if [ -e "PATH" ]
then
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Yes:21,No" -center -geometry 550x90 -title "Error" "An item named PATH already exists.
Do you want to overwite it?"
# If they chose to cancel.
[ $? -eq 21 ] || exit
fi
# Restore the item.
RestoreItem
else
# Check to see if they clicked on the application or dropped a file or directory on the icon.
test -sd "$@"
if [ "$?" = "0" ]
then
# If they just clicked on the icon.
# Write the confirmation message.
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Yes:21,No" -center -geometry 550x90 -title "Confirm Action" "Do you want to restore the item PATH?"
# If they chose to cancel.
[ $? -eq 21 ] || exit
# See if there is already a file or folder of the same name in the same location.
if [ -e "PATH" ]
then
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Yes:21,No" -center -geometry 550x90 -title "Error" "An item named PATH already exists.
Do you want to overwite it?"
# If they chose to cancel.
[ $? -eq 21 ] || exit
fi
# Restore the item.
RestoreItem
# If they chose the see-path option.
elif [ "$1" = "-see-path" ]
then
# Write the message.
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Okay" -center -geometry 360x90 -title "Original File Location" "PATH"
else
# If they dropped something to the icon.
# Write the error message.
MSG=`which gxmessage` || MSG=xmessage
$MSG -buttons "Okay" -center -geometry 360x90 -title "Error" "You can not drop items onto things that are in the Trash."
fi
fi