Posts: 1,520
eriefisher
Joined: 07 Oct 2007
#1
I used to maintain and use a text file to keep track of name and phone numbers. Nothing fancy and no special formatting, just a simple text file I could add to and search as needed. Mind like a sieve you know.

Now the problem. Since I have been playing with zenity I thought I would put a gui to my phonebook. Actually I got everything to work great but I wanted to add a way of removing an entry but I am drawing a blank. I was hoping that OU812, Anti or anyone else could come up with a way to add a remove option?

Here is the script. It needs to be cleaned up but play with it and let me know what you find.

Code: Select all

#!/bin/bash

##  Title: Zen-phonebook
##
##  Purpose: To easily save and search names and phone numbers
##
##  Author: eriefisher@gmail.com
##
##  Dependencies: Zenity and stock Linux tools

touch .zen-phonebook.txt  ## This will look for your phonebook file in your /home directory
                          ## and create it if it doesn't exist. All entries will be saved here.
ans=$(zenity --title"Zen-phonebook" --list --text"What would you like to do" --radiolist --column"Pick" --column"Options" TRUE"Search for a contact" FALSE"Enter new contact" FALSE"Show all contacts" FALSE"Remove contact" --width 300 --height 250)

if ["$ans" ="Search for a contact" ] ; then
   SEARCH=$(zenity --title"Search for" --entry --text"Please enter a name") && cat .zen-phonebook.txt | grep $SEARCH* | zenity --title"Search Results" --text-info --height 200 --width 300
fi

if ["$ans" ="Enter new contact" ] ; then
   NEW=$(zenity --title"New Contact" --entry --text"Please enter name and Number") && echo \"$NEW" >> .zen-phonebook.txt && zenity --title"Saved" --info --text"$NEW\n Has been saved"
fi

if ["$ans" ="Show all contacts" ] ; then
   cat .zen-phonebook.txt | zenity --title"All contacts" --text-info
fi

if ["$ans" ="Remove contact" ] ; then
As you see the end does not finish so take it from there.
Posts: 1,081
OU812
Joined: 29 Sep 2007
#2
These may be of interest


========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://linux.byexamples.com/archives/265/a-complete-zenity-dialog-examples-2/"
linktext was:"http://linux.byexamples.com/archives/26 ... xamples-2/"
====================================
" onclick="window.open(this.href);return false

========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://linux.byexamples.com/archives/259/a-complete-zenity-dialog-examples-1/"
linktext was:"http://linux.byexamples.com/archives/25 ... xamples-1/"
====================================
" onclick="window.open(this.href);return false

========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://manpages.unixforum.co.uk/man-pages/unix/solaris-10-11_06/1/zenity-man-page.html"
linktext was:"http://manpages.unixforum.co.uk/man-pag ... -page.html"
====================================
" onclick="window.open(this.href);return false

I'm afraid this goes a bit beyond my experience with zenity.

john
Posts: 1,520
eriefisher
Joined: 07 Oct 2007
#3
Thanks John, I did not see that last link you posted. I think zenity will work fine but I need to come up with a way to remove a single line from a text file without changing the format of the file. More digging required I guess.
Posts: 200
lagopus
Joined: 15 Oct 2008
#4
Suggestion:

zenity --list to display the content of your file (provided you have one record per line)

I suppose zenity list returns the content of the selected line (not the number of the item in the list, I used zenity but don't have it at hand)

Then I would do a grep -v 'the string returned' phonebook-file redirected to a tmpfile
and overwrite the phonebook with the tmpfile
Posts: 1,520
eriefisher
Joined: 07 Oct 2007
#5
Thanks, I will play with that later.
Posts: 516
oldhoghead
Site Admin
Joined: 01 Oct 2007
#6
eriefisher,

I found this:


========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://www.linux.com/feature/114156?theme=print"
linktext was:"http://www.linux.com/feature/114156?theme=print"
====================================
" onclick="window.open(this.href);return false

Towards the end of the article, It may help, beyond my expertise though

cheers,
oldhoghead
Posts: 1,520
eriefisher
Joined: 07 Oct 2007
#7
Thank you, something like that just might work.
Posts: 1,081
OU812
Joined: 29 Sep 2007
#8
Have you thought of using gtkdialog? Look at the example in the top-right corner.


========= SCRAPER REMOVED AN EMBEDDED LINK HERE ===========
url was:"http://linux.pte.hu/~pipas/gtkdialog/screenshots.html"
linktext was:"http://linux.pte.hu/~pipas/gtkdialog/screenshots.html"
====================================
" onclick="window.open(this.href);return false

john
Posts: 200
lagopus
Joined: 15 Oct 2008
#9
My modest contribution:

Code: Select all

#!/bin/bash

##  Title: Zen-phonebook
##
##  Purpose: To easily save and search names and phone numbers
##
##  Author: eriefisher@gmail.com" onclick="window.open(this.href);return false
##
##  Dependencies: Zenity and stock Linux tools

touch .zen-phonebook.txt  ## This will look for your phonebook file in your /home directory
                          ## and create it if it doesn't exist. All entries will be saved here.

WIDTH=300
HEIGHT=300
MYTITLE="Zen-phonebook"

while true
do

    ans=$(zenity --title $MYTITLE --list --text"What would you like to do" --radiolist --column"Pick" --column"Options" TRUE"Search for a contact" FALSE"Enter new contact" FALSE"Show all contacts" FALSE"Remove contact" --width 300 --height 250)

    # Cancel
    if ["$ans" ="" ] ; then
        exit 0
    fi

    if ["$ans" ="Search for a contact" ] ; then
        SEARCH=$(zenity --title"Search for" --entry --text"Please enter a name") && cat .zen-phonebook.txt | grep $SEARCH* | zenity --title"Search Results" --text-info --height 200 --width 300
    fi

    if ["$ans" ="Enter new contact" ] ; then
        NEW=$(zenity --title"New Contact" --entry --text"Please enter name and Number") && echo \"$NEW" >> .zen-phonebook.txt && zenity --title"Saved" --info --text"$NEW\n Has been saved"
    fi

    if ["$ans" ="Show all contacts" ] ; then
        cat .zen-phonebook.txt | zenity --title"All contacts" \
                                        --width=$WIDTH \
                                        --height=$HEIGHT \
                                        --text-info
    fi

    if ["$ans" ="Remove contact" ] ; then

        DELETE=$(cat .zen-phonebook.txt | zenity --list \
              --width=$WIDTH \
              --height=$HEIGHT \
              --title="Remove Contact" \
              --column="Contacts")

        if [ -n"$DELETE" ]
            then
            # Confirm
            zenity --title"Remove Contact" --question --text"Remove $DELETE ?"; 
            if ["$?" ="0" ]
            then

                grep -v"$DELETE" .zen-phonebook.txt > /tmp/.zen-phonebook.txt.$$
                mv /tmp/.zen-phonebook.txt.$$ .zen-phonebook.txt

            fi
        fi
    fi
    
    # Do you want to continue?
    zenity --question --title $MYTITLE --width=$WIDTH \
           --text"Select another operation?\n(Cancel to quit $MYTITLE)"
    if ["$?" ="1" ]
    then
        exit 0
    fi

# Done with main loop
done
Posts: 1,520
eriefisher
Joined: 07 Oct 2007
#10
PERFECT!!! This is exactly what I had envisioned. Please put you name one and spread the word.
Posts: 1,081
OU812
Joined: 29 Sep 2007
#11
Congrats, dude.

john