User Tools

Site Tools


insert_bookmark.sh

I modified the standard insert_bookmark.sh script to include check for duplicates and corrected some minor bugs. Here below the code:

#!/bin/sh
 
[ -d "${XDG_DATA_HOME:-$HOME/.local/share}/uzbl" ] || exit 1    # Check for the local folder for bookmarks
which zenity &>/dev/null || exit 2                              # Check for zenity
 
file=${XDG_DATA_HOME:-$HOME/.local/share}/uzbl/bookmarks        # Select the bookmark file
newfile=/tmp/uzbl_insert_bookmark                               # Select a tmp file for merging tags
 
url=$6                                                          # URL is taken from UZBL
title=`echo "$7" | sed 's/\t/ /g'`                              # Title is taken from UZBL; replace tabs, we use TAB as a delimiter
new=`echo -e "$url $title\t"`                                   # Add a TAB at the end, as a delimiter between title and TAGS
 
# Ask the user for a title, tags and confirm
new=`zenity --entry --text="Add bookmark. Add tags after the TAB, separated by spaces" --entry-text="$new"`
 
exitstatus=$?                                                   # If the user changes his mind, do nothing and exit
if [[ $exitstatus -ne 0 ]]; then
 exit $exitstatus
fi
 
old=`cat $file | grep $url`                                     # Check for dupes. Select the whole line in the bookmark file matching the url
 
if [[ -z "$old" ]]; then                                        # If no dupes are found...
 echo -e "$new" >> "$file"                                      # ... append the new bookmark to the file
else
 
 oldtags=`echo -e "$old" | cut -f2`                             # Select the string containing the old tags (TAB delimiter)
 newtags=`echo -e "$new" | cut -f2`                             # Select the string containing the new tags (TAB delimiter)
 
 if [[ "$oldtags" != "$newtags" ]]; then                        # If a dupe is found, check for different tags...
  # TODO: incorporate tag order change
 
   newtagarray=($newtags)                                       # Convert the newtags string into a tag array
   mergetags=$oldtags                                           # Create the mergetags string and add to it all new different tags
   for i in ${newtagarray[@]}; do
    [[ $oldtags != *$i* ]] &&  mergetags="$mergetags $i"
   done
 
   sed 's/'"$oldtags"'/'"$mergetags"'/' < $file > $newfile      # Substitute the merged tags and copy in a new file (overwriting)
   # TODO: check for the full line, not only for the tags. I meet issues with escape commands in the URL part.
 
   mv $newfile $file                                            # ... move back the new file to the old one
 
 else exit 3                                                    # If a dupe exists, but the tags are the same, do nothing
 fi
 
fi
insert_bookmark.sh.txt · Last modified: 2016/08/31 14:10 (external edit)