Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Nautilus has its notes-for-files
Tomboy has it's notes for linking-ideas
The screen itself has its sticky notes

Is there an app for jotting down notes for a particular window?
... probably based on the window's title, and displayed via shortcut key..

share|improve this question
    
To what end? Also, once the window closes, what then? What kind of notes? –  Scaine Dec 29 '10 at 15:46
    
To what end? .. the same "end" as for any note ever taken/written.. as a reminder. Like some particular figures I'd like to compare now, based on yesterdays figures... There are many ways I could use it... At the moment I keep all this type of information in a few select files.. but seperating the data from its source is not usually a preferred optione.. Hence Nautilus Notes.. one note, one file.... The "closed window" will be opened again tomorrow, and it would be much nicer to press a hotkey for my reminders regarding this paritcular window. –  Peter.O Dec 29 '10 at 15:54

2 Answers 2

up vote 1 down vote accepted

Well, I've been on the learning curve again... (late nights and lots of coffee).

The net result is a moderately useable bash script (using an sqlite db/table) which does what I was after...
... ie. Creates a grouping of notes, based on window titles...

It works.. but it's definitely "alpha"..

So, I've got my home-grown app! ... I'm posting it here for anyone interested in a "look-see"...

Here it is:


#!/bin/bash

clearAllDataAndStartAgain="false"

# Name: tinotes (Title Notes)
# 
# What does it do? ... Aside from giving me a great opportunity to learn
#      more about bash (and sqlite, which I had previously barely touched).. not much ;)
#      Actually, it allows you to keep notes, grouped by window title.
#      The notes are kept in an sqlite database. 
#      Each timestamped note-"event" is stored in a row of the main (only) table.
#      You can consolidate notes in a semi ad-hoc manner... 
#      Notes are presented to you in a single editable text file (in Leafpad).  
#      You must keep at least one "tinotes" timestamp-line at the top of the text...
#      All lines above this topmost timestamp-line are ignored,
#      including the "title", which is presented upon each viewing.
#      If you remove any/all other "timestamp line(s)", those particular rows are 
#      deleted from the sqlite table, and the text which remains is associated with
#      the immediately preceding timestamp-line (and updated as such in the sqlite table)
#         
# How do you drive it? .. Just associate `tinotes.sh` with a shortcut-key.. That's' it!
#      Just press the shortcut-key and you will be presented with Leafpad displaying 
#      a  "title line" at the very top, and on line 2, is a "tinotes timestamp" line.  
#      Type your note below the  timestamp-line, then Save and Exit Leafpad,..
#      NB.. You MUST fully EXIT Leafpad.. the script cannot proceed until you EXIT Leafpad..
#      Leafpad is effectively being used as a Dialog-box with "normal" editing capability.
#      A warning Dialog appears if another instance of this script is currently running.. 
#      (actually, if another ***tinotes** leafpad window is running).
#             
# Caveat: This version is very much "demo-only"... 
#         It works, but has typical "alpha" bugs... 
#         I've presented it here, for anyone who is interested in the general idea.
#             
# dependencies
# ============
#      sqlite3 ,, the notes are stored in a sqlite db.
#      leafpad .. to edit/view the notes (no need to re-invent the wheel)
#       wmctrl .. to detect if a ***tinotes*** leafpad window is currently open.
#      xdotool .. to get the active window's' ID
#     xwininfo .. to get a window's title via it's ID.
#       md5sum .. to check for mods to the presented/added notes data 
#       zenity .. for warning dialog(s)
#          sed .. versatile!
#           wc .. word count? .. line count (here)

############################################################################

function FinalizeNote {
  title="$1"; note="$2"; timeline="$3"; preList="$4"

  # Remove trailing whitespace
  note="${note%"${note##*[![:space:]]}"}"
  # Does this timestamp exist in the  PRE-LIST
  timestamp=${timeline:0:19}
  preCt=$(sed -n "s/^$timestamp.\(.*\)/\1/p" "$preList" |wc -l)
  if [ "$preCt" -eq "0" ] ; then
      sqlite3 "$appdb" "INSERT INTO Tmain (time,title,note) VALUES('$timestamp','$title','$note');"
  elif [ "$preCt" -gt "1" ] ; then
      excod=3
      zenity --error --text "$preCt duplicate timestamps found. \
      \nThis is an 'alpha' problem! \
      \n\n    Exiting with code $excod"
      exit $excod
  else
      # TODO: At it currently stands, it is only known that "something" has changed, 
      #       but not which rows have been effected. (Just update them all, for now.)
      id=$(sed -n "s/^$timestamp.\(.*\)/\1/p" "$preList")
      sqlite3 "$appdb" "UPDATE Tmain SET note='$note' WHERE id='$id';"
      # Remove line from PRE-LIST
      sed -i -e "/^$timestamp.*$/d" "$preList"
  fi
}

############################################################################

  sep="∘" # Field seperator 
  sepmulti="∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘∘"
  sepfirst="∘∘(write∘new∘notes∘below∘this∘line)∘∘"
  appnam="tinotes"
  appdir="$HOME/.$appnam"
  appdb="$appdir/$appnam.db"
  appsql="$appdir/$appnam.sql"
  apptxt="$appdir/$sep$sep$sep$appnam$sep$sep$sep"
  preList="$appdir/$appnam.pre"; echo -n>"$preList"

  tiwid=0 # Check for a currently running Instance of the "Leafpad"  window...   
  tiwid=$(wmctrl -l |sed -n "s/\([^ ]\+\) \+[^ ]\+ \+[^ ]\+ \*\?$(basename $apptxt)$/\1/p")
  if [ "${tiwid:0:2}" == "0x" ] ; then
      # Warn of the need to finalize an already-running instance of this script
      zenity --warning  --text "You must first finalize a previously opened \n\t 'tinotes' window.\
      \n\n  To activate the window, select 'OK'"
      # 'wmctrl' can only activate via a title
      #   ..and it is case-insensitive and wildcarded!! 
      # so use 'xdotool'
      xdotool windowactivate --sync $(($tiwid))
      exit
  fi

  #################################################################################################################################################
  [ "$clearAllDataAndStartAgain" = "true" ] && [ -d "$appdir" ] && rm -rf  "$appdir" # Typically for TESTING ONLY; Remove all data,and start afresh!  
  #################################################################################################################################################

  # First-time create app's main directory
  [ ! -d "$appdir" ] && mkdir   "$appdir"
  # First-time Create the Tmain table
  [ ! -f "$appdb" ] && sqlite3 "$appdb" "create table Tmain (id INTEGER PRIMARY KEY,time DATE,title TEXT,note TEXT);"

# Get the active-window's title
# =============================
  activeT=$(xwininfo -id "$(xdotool getactivewindow)" |sed -n \
  "2s/^xwininfo: Window id: \(0x[[:xdigit:]]\+\) \x22\(.*\)\x22$/\2/p")

# Does a previous row of the same title already exist?
# ====================================================
  # First, prepare a new/"empty" note entry
    echo "$activeT" >"$apptxt"
    echo -e $(date '+%Y-%m-%d %H:%M:%S' |tr -d '\n')" $sep$appnam$sepfirst\n\n\n">>"$apptxt"

  # Count existing rows for this title
  if [ $(sqlite3 "$appdb" "SELECT COUNT(title) FROM Tmain WHERE title = '$activeT';") -gt 0 ] ; then
      # Previous notes FOUND for this window title
      # Make a list of timestamps to match-off against any user mods 
      sqlite3 "$appdb" "SELECT time,id FROM Tmain WHERE title='$activeT';" >"$preList"

      # Build and Run the query to construct the "timeline"
      echo -e ".separator "$sep"\n\
      SELECT time||' $sep$appnam$sepmulti','\n\n'||note||'\n' FROM Tmain WHERE title='$activeT' order by time desc;">"$appsql"
      sqlite3 "$appdb" <"$appsql">>"$apptxt"
  fi
  # Present the notes to the user (get the  "before" md5sum, to compare with user's access/mods)  
  md5bef=$(md5sum "$apptxt" |sed "s/\([[:xdigit:]]\+\).*/\1/")
  leafpad "$apptxt" # The script waits for the user to close "leafpad"
  md5aft=$(md5sum "$apptxt" |sed "s/\([[:xdigit:]]\+\).*/\1/")
  # Check for any modifications to the notes
  if [ "$md5bef" == "$md5aft" ] ; then
      # No changes have been made.
      exit 0
  fi
  # The notes have been modified. Commit the changes!
  # It's time to analyze the changes, and tidy up.
  #   Remove leading and trailing blank lines (per timestamp).
  #   Delete rows for which the timestamp has been removed.
  #   Update the table for all the remaining rows.
  note=""
  lnCt=0
  while read line ; do
      lnCt=$((lnCt+1))
      [ "$lnCt" -eq "1" ] && continue  # The first line is a throw-away copy of the "title"
      # Find timeline. Any leading data is lost. (It shouldn't be there... common-sense!)
      if [[ "$line" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}\ ∘.*∘$ ]] ; then
          # FOUND timeline
          if [ "$note" != "" ] ; then
              FinalizeNote "$activeT" "$note" "$timeline" "$preList"
          fi
          note=""
          timeline=$line
      elif [ "$line" = "" ] ; then
          if [ "$note" != "" ] ; then
              note="$note$line"$'\n' # append this blank line
          fi
      else
          note="$note$line"$'\n' # append this non-blank line
      fi 
  done <"$apptxt"
  #
  if [ "$note" != "" ] ; then
      FinalizeNote "$activeT" "$note" "$timeline" "$preList"
  fi
  # Delete rows for timestamps still in the PRE-LIST
  while read line ; do
      eval $(echo "$line" |sed -n "s/.*|\([0-9]\+\)$/id=\1/p")
      sqlite3 "$appdb" "DELETE FROM Tmain WHERE id='$id';"
  done <"$preList"

exit 

############################################################################
share|improve this answer
    
You should accept this as the answer, it's fantastic! And thank you for explaining it, not just posting a bunch of opaque commands. –  Stefano Palazzo Jan 4 '11 at 21:14

Hmm, the best I can think of is going into ccsm and playing with selecting windows by name (I'd start with the "Window Rules" plugin). There is a way to move or sticky windows by name, so if you named your windows uniquely, and you were clever, maybe this could work...

share|improve this answer
    
Syzygy.. thanks for the thought, but what I'm looking for is something which will "automatically" create an empty note based on the window title, and then after I type in some information, I can recall that note via the same shortcut key, either when I'm in the window, or independantly via a search of all the collected notes... If nothing already exists, I'll rough something together myself... it seems like a fairly simple idea, and a small targeted project is the best way to learn, especialy if you are scratching your own itch... but a pre-made app would be preferable. –  Peter.O Dec 29 '10 at 17:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.