Make NoteList

Description

This script requires access to the ListSort and QuickSort handlers which I submitted separately; change the line near the top that includes the script for those handlers to point to the correct file. The program displays a list of your Evernote notebooks; you select one and the program does the rest. It sorts the list of Notes (by title) and puts the sorted list into a new note in the selected notebook called "!Note List". Two things could be done to improve this a little (if you know how, please let me know) - 1. Single space the list of note titles in !Note List. 2. Have the script select the new !Note List note in Evernote when done.

The Code

(*  Make NoteList.scpt - copyright 2011 by Robert Homes
Creates a note called !Note List in selected Evernote notebook  *)


set ListHandler to load script file (((path to scripts folder) as string) & "Handlers:ListHandlers.scpt")

tell application "Evernote"
activate

set NumWindows to (count windows)
if NumWindows > 1 then
display dialog NumWindows & " windows are open --close all but 1 first"
return
end if

set theNotes to selection
try
set activeNbName to (name of notebook of item 1 of theNotes)
on error
set activeNbName to ""
end try

-- select Notebook to work with
set NotebookList to (name of every notebook as list)
listSort(NotebookList) of ListHandler

choose from list NotebookList with title "Notebooks" default items (activeNbName as list) without multiple selections allowed and empty selection allowed

if result = false then
return
else
set notebook1 to notebook named result
end if

-- if 0 or 1 notes in notebook, abort
set totalNotes to (count notes in notebook1)
if totalNotes < 2 then
display dialog "Not enough (<2) notes in " & name of notebook1 with icon stop buttons {"Ok"} default button "Ok"
return
end if

-- display the selected notebook
set query string of first window to "notebook:" & (name of notebook1)

-- get list of notes in notebook
set NoteList to (title of every note in notebook1 as list)
listSort(NoteList) of ListHandler

-- see if a note with title '!Note List' is already in notebook
if NoteList contains "!Note List" then
display dialog (name of notebook1) & " notebook already has a !Note List --continue anyway?" buttons {"Abort", "Ok"} default button "Ok"
if button returned of result is "Abort" then return
end if

-- create new note to hold list
set note1 to (create note with text "" notebook notebook1 title "!Note List")

-- set author of note1 to "(script)" (author property not included in dictionary!)

-- add heading to note
set d to current date
set today to (month of d as integer) & "/" & (day of d) & "/" & characters 3 thru 4 of ((year of d) as string)
append note1 text name of notebook1 & " Notes - " & today & return & return

repeat with i from 1 to count items in NoteList
-- log (item i of NoteList as string)
if item i of NoteList ≠ "!Note List" then
append note1 text (item i of NoteList as string)
append note1 text return & return
end if
end repeat

-- redisplay the selected notebook
set query string of first window to "notebook:" & (name of notebook1)

--set selection to note1 -- doesn't work (selection is r/o)
--find notes "intitle:"!Note List" created:(creation date of note1)" -- doesn't work
--find notes "intitle:!Note List" --doesn't work; would filter to those anyway, not 'select" them
-- open note window with note1 --works, but still doesn't select the note that is opened

synchronize
end tell