Novell Home

HowTo: Create a GUI Zipping Utility with the shell and Runtime Revolution

Novell Cool Solutions: Feature
By Stomfi

Digg This - Slashdot This

Posted: 22 Oct 2004
 

StomfiLearning to use Linux at Home and Work
Welcome to my ongoing series of HowTo articles designed to help Linux newbies get comfortable with Linux. Before trying any of these HowTos, take a few minutes to study the prerequisites so you can hit the ground running.
--Stomfi

Your work on the Poems database in the HowTo for that project will be used here. Use that HowTo to discover how to create windows, fields and buttons, and how to create shell scripts.

This is a picture of the finished utility.

The yellow field is a list of files and folders in the current directory. When zip menu opens it is set to the user's HOME directory. This is called the defaultFolder in RunRev

You will see that folders are marked with a / at the end. This is the result of running the shell command “ls -p”

The mouse is used to click on a line in the yellow field. If the line is a folder, another shell script changes to that directory and returns its name so it can be set as the defaultFolder.

If it is a file, its name is placed in the white field and the buttons will work to perform the named action.

The next pictures show the way to set up the files field so that items in the list can be individually highlighted.


The last tab on the properties pop up is used.


Notice that only the List Behavior is switched on. Also that Horizontal Grid is switched on.

The size and position of this field can also be locked in the basic tab.

The scripts for the card, the files field and the buttons are listed next. Right click on a blank area of the card to edit the card script. Double click on the field or buttons to get the properties pop up and select the script tab. Do not forget to click the Apply button to save your script.

This is the script for the card.

on openCard 
   #Look for files and directories in
the users HOME 
   set the defaultFolder to $HOME 
   #Use the shell command ls -p to
put a / after directories 
   #and put the results into the
files field 
   put the shell of ("ls -p")
into field "files" 
 end openCard 

This is the script for the “files” field.

on mouseDown 
   #We don't want anything here to
start with 
   put empty into field "ZipFld" 
   #Whenever we click a line it goes
into a holding variable 
   put the hilitedText of me into
ThisFile 
   #This shell script return a D if
the file is a directory 
   put the shell of ($HOME &
"/bin/newlist.sh" && ThisFile) into ThisFileType 
   #If it is a directory we have to
move there 
   if ThisFileType = "D" 
   then 
     put the shell of ("cd "
&& ThisFile &"; pwd") into NewDir 
     #This is the folder that runrev
uses to look for files 
     set the defaultFolder to NewDir 
     #Run this shell on the
defaultFolder 
 
     put the shell of ("ls -p")
into field "files" 
     #Add a message for the user to
navigate backwards 
     put return & "Click
Line Above to Go Back" after field "files" 
   else 
     #Not a directory so it is a file
which we can work on 
     put ThisFile into field "ZipFld" 
   end if 
 end mouseDown 

This is the script for the ZipOnly button.

on mouseUp 
 #Check to see if there is a file to
Zip 
 if field "ZipFld" <>
empty 
 then 
 #Select a folder to put it in 
 answer folder "Please choose a
zipping folder" 
 put it into ZipFldr 
 put field "ZipFld" into
ZipFile 
 #Tell the zip shell script about the
zip file and folder 
 
 put ($HOME & "/bin/zipped.sh"
&& ZipFile && ZipFldr) into Zipped 
 replace return with empty in Zipped 
 #Run the shell 
 put the shell of Zipped into ThisZip 
 #Refresh the file list 
 put the shell of ("ls -p")
into field "files" 
 end if 
 end mouseUp 

This is the script for the Unzip button.

on mouseUp 
 if field "ZipFld" <>
empty 
 then 
 answer folder "Please choose an
unzipping folder" 
 put it into UnZipFldr 
 put field "ZipFld" into
ZipFile 
 put ($HOME & "/bin/unzipped.sh"
&& ZipFile && UnZipFldr) into Zipped 
 replace return with empty in Zipped 
 #Run the unzip shell inside a
terminal window 
 #To gather needed user responses 
 put ("xterm -e" &&
Zipped) into ThisZip 
 #Use the runrev launch command 
 launch ThisZip 
 #Refresh file list 
 put the shell of ("ls -p")
into field "files" 
 end if 
 end mouseUp 

This is the script for the Zip + Encrypt button.

on mouseUp 
 #This is for encrytion and zipping 
 #see the zip and unzip scripts for
comments on how this works 
 
 if field "ZipFld" <>
empty 
 then 
 answer folder "Please Choose a
zipping folder" 
 put it into ZipFldr 
 put field "ZipFld" into
ZipFile 
 put ($HOME & "/bin/zippy.sh"
&& ZipFile && ZipFldr) into Zipped 
 replace return with empty in Zipped 
 put ("xterm -e" &&
Zipped) into Zippy 
 replace return with empty in Zippy 
 launch Zippy 
 end if 
 put the shell of ("ls -p")
into field "files" 
 end mouseUp 

Next the shell scripts are shown. These go in the user's HOME/bin directory. Use the shell command chmod +x FileName to make them executable. Replace FileName with the name of your shell script.

This is the shell script newlist.sh

#!/bin/bash #newlist.sh name if [ -d $1 ]; then echo "D" fi This is the shell script called zipped.sh
#!/bin/bash 
 #zipped.sh filename zip_directory 
 # 
 #strip name from extension. This
echoes the filename and pipes it to awk. The Field separator 
 
 #is set to a dot by FS=”.”
The pipe symbol is a vertical broken line on the keyboard 
 
 FN=`echo $1 | awk 'BEGIN { FS="."
}{print $1}'` 
 #zip without directory into target
directory 
 zip -D $2/$FN $1 

This is the shell script called unzipped.sh

#!/bin/bash 
 #unzipped.sh filename directory 
 #strip extension 
 FE=`echo $1|awk 'BEGIN { FS="."
}{print $2}'` 
 if [ $FE != "zip" ] 
 then 
 echo "not a zip file" 
 exit 
 fi 
 #save current directory 
 CDIR=`pwd` 
 #change to $2 directory 
 cd $2 
 unzip $CDIR/$1 
 cd $CDIR 

This is zippy.sh

#!/bin/bash 
 #zippy.sh filename directory 
 #strip name from extension 
 FN=`echo $1|awk 'BEGIN { FS="."
}{print $1}'` 
 #zip without directory path into
target 
 zip -D -e $2/$FN $1


The last picture shows the layout of the RunRev application. After you have saved the file. Build the application as previously shown and put the resulting runtime executable into your HOME/bin directory. Place a shortcut on your desktop and use it to zip, zip and encrypt and unzip files. This is a good way to send files over the Internet.

This application demonstrates that given the right tools and a bit of training, ordinary users can build their own utilities. One of the big advantages of Linux is that these tools are either free or very cheap. There are lots more advantages which these HowTos will explore. I especially like the tools which get your computer to work for you without you having to be there. I bet Bill and his bums on seats brigade hate those ones.

So where did all those colours come from?






  1. Make sure you are in pointer tool mode – Tools – Pointer Tool. Double click on any field of button or the card.
  2. Click on Colors in the bar.
  3. Change the Background Color.
  4. If you want to change the colour of your text, change the Foreground Color.
  5. If it disappears make sure you haven't hidden it under the background colour.

Have some fun but make sure you can see the important bits. You can do other things with the Color pop up.

In another HowTo we shall add directory zipping functionality to this utility. If you would like to figure it out for yourself, here are a few clues.

Obviously we need another question after if ThisFileType = "D" to find out if we want to zip the directory in this case we put the directory name without the trailing / in the zipping field. We also need to set a flag so that when we press a zipping button it knows we are doing a directory. You need to set a global variable so that the other scripts know about it, eg global ADIR whicch you put in each script before you use it. eg put “D” into ADIR for the setting, and if ADIR = “D” then use dirzzip.sh or dirzippy.sh else the ordinary file zip shell. Before the “end if” put “F” into ADIR to reset it, otherwise it will keep trying to do directories. The line for zipping is the same for each original file one but includes -r . Don't worry if this is too much. I will explain all. I'll also include some checks for the unzip button so it does nothing if the file isn't a zip file.

I hope you have as much fun playing with this as I did creating it for you.

For more information about Runtime Revolution visit http://www.novell.com/coolsolutions/feature/1863.html


Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions. www.webwiseone.com

© 2014 Novell