Script to Search + Copy

Recommended Posts

I want to automatically extract any text between two specific subheaders (say ***** and !!!!!) from all notes in a notebook into a single note every week. Has anyone ever written a script to search for and copy/export parts of notes? Any thoughts on how it could be done?

Thanks!

Share this post

Link to post

I want to automatically extract any text between two specific subheaders (say ***** and !!!!!) from all notes in a notebook into a single note every week. Has anyone ever written a script to search for and copy/export parts of notes? Any thoughts on how it could be done?

I think you'll have to take this outside of Evernote if you need the search.
Inside Evernote, special characters like * and ! are removed for search indexing.

Maybe you can use a tag to identify the notes to process.

In my backups, I maintain a copy of my notes in HTML format.
I could search that backup.

My solution is write a script (Mac Applescript) and generate 1 or more notes

Share this post

Link to post

I want to automatically extract any text between two specific subheaders (say ***** and !!!!!) from all notes in a notebook into a single note every week. Has anyone ever written a script to search for and copy/export parts of notes? Any thoughts on how it could be done?

Sounds like a good idea. I did not have such a script, but it caught my interest. So here is a DRAFT script, lightly tested, that you can use to get started. I can immediately think of lots of improvements I would want, but I'll leave that up to you. If you do make any improvements, please post your script back here so others can benefit as well.

If you find any bugs or issues, please post back here.

I strongly suggest that you create a TEST Notebook to use in testing and further developing this script.Use at your own risk! It works for me, but it DOES create a new Note in your Notebook.

Share this post

Link to post

I am doing the following to get the title of the origin note, so that I know where the copied text is coming from:

setoriginTitletotitleofoNote

setfoundItemsto {}

if (countofparsedList) > 1 thensetendoffoundItemstooriginTitle

Question: How do I get the link of the current oNote so that the title is a link to the note the summary came from? I can't find it anywhere in the documentation.

The only other change that I will make is to have separate scripts to create notes in different notebooks depending on which subheader I write under. For example, I will have a Key Learnings notebook and a To-do list notebook that all come from a Meetings notebook that I already have. That was as simple as creating a targetNb property and changing the beginDelim and endDelim.

note link(text, r/o) : A URL specifying this note. NOTE: if this note is in a synchronized notebook but has not yet been synchronized itself, the result will be nil.

reminder time(date or missing value) : The reminder time for the note.

reminder done time(date or missing value) : The time the reminder for the note was marked completed.

reminder order(date or missing value) : The reminder order time for the note. When not empty, this is what indicates the note has a reminder. To add a reminder to a note, typically set its reminder order to the current date.

(*===============================================================================[EN]ExtractText in Delimiters(AS)===============================================================================
VER:1.2 LAST UPDATE:2016-06-10
PURPOSE:•Extract text in delimited from all Notes in SpecifiedNotebook
AUTHOR:JMichaelTXFind any bugs/issues or have suggestions for improvement?Please post below
CHANGE LOG:1.22016-06-10 ADDED:Exclude empty found items (nothing between delimiters)
CHANGED:Do NOT produce a SummaryReportNoteif no notes are found with delimiters.1.12016-06-08 ADDED to output:•SourceNoteTitle as hyperlink
•LastUpdate of SourceNote
REQUIRED:1.Mac OS X Yosemite10.10.5+2.MacApplications•EvernoteMac6.7+
REF:The following were used in some way in the writing of this script.1.Requested by:Script to Search+Copy
https://discussion.evernote.com/topic/96601-script-to-search-copy/===============================================================================*)### SCRIPT SETUP ###--Change the below properties to suit your needs
property nbName :"TestExtractText"
property beginDelim :"***"
property endDelim :"!!!"--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~set newNoteHTML to "<div id=\"en-note\">"& linefeed & linefeed --&"<div><br /></div>"--Must add "</div>" at END of all text for note --
tell application "Evernote"set noteList to every note of notebook nbName
set numNotesWithDelim to 0
repeat with oNote in noteList
set noteHTML to HTML content of oNote
set noteText to do shell script "echo "& quoted form of noteHTML & space &"| textutil -convert txt -stdin -stdout"setAppleScript's text item delimiters to {beginDelim, endDelim}set parsedList to text items of noteText
--set foundItems to even items of parsedList
set foundItems to {}
repeat with iItem from 2 to (count of parsedList) by 2##CHG: test for no chars between delimiters ##set textStr to text of item iItem of parsedList
if textStr ≠"" then
set end of foundItems to item iItem of parsedList
end if
end repeat
if((count of foundItems)>0) then ##CHG:Add IF
set numNotesWithDelim to numNotesWithDelim +1##CHG:--- CREATE HYPERLINK TO NOTE ---set noteTitle to title of oNote
set noteLink to note link of oNote
set noteHTMLLink to "<a href=\""& noteLink &"\">"& noteTitle &"</a>"set noteUpdated to (modification date of oNote)set noteUpdated to date string of noteUpdated
setAppleScript's text item delimiters to ("</div>"& linefeed &"<div>")set textToAddToNote to foundItems as text
log textToAddToNote
##CHG: ADD Note link to output
set newNoteHTML to newNoteHTML & linefeed &¬"<div> <br/>"&"Text From: "& noteHTMLLink &" (Updated: "& noteUpdated &")</div>"& linefeed ¬&"<div>"& textToAddToNote &"</div>"
end if--DelimitedText was found
log "end of repeat"
end repeat
##CHG:MakenewNote ONLY ifNoteswithDelimiters were found ##if(numNotesWithDelim >0) then
set newNoteHTML to newNoteHTML & linefeed &"</div>"
log newNoteHTML
set titleNote to nbName &": Weekly Summary "& date string of (current date)
create note with html newNoteHTML ¬
title titleNote ¬
notebook nbName
elseset msgStr to "••• NO Notes were found with the Delimiters •••"
log msgStr
display dialog msgStr with title (name of me)
end if
end tell --Evernote

Please let me know if you find any bugs or issues.

Share this post

Link to post

Awesome, thanks! Good call-out on style. And the date updated is a good idea.

1) Is there a way for me to get the author's name too? I was thinking that I could get the author's name from the "Created by" field, which would be ideal. An alternative might be to get the author's initials from a tag that authors would use. I don't think I should use the "Author" field because if you duplicate and modify a note, the original author remains. I saw that @DTLow mentioned that tag info is stored as
<meta name="keywords" content="!Journal"/> but wasn't sure how to use that info.

2) @DTLow You wrote, "My initial thought was to use the first delimiter, but then walk through the parsed text to find the terminating delimiter;text 1 thru (offset of "!!!" in parcedText)"

How would walking through the text eliminate the devastation of a missing terminating delimiter? Wouldn't it end up including everything in the rest of the note regardless of approach taken.

Share this post

Link to post

2) @DTLow You wrote, "My initial thought was to use the first delimiter, but then walk through the parsed text to find the terminating delimiter;text 1 thru (offset of "!!!" in parcedText)"

How would walking through the text eliminate the devastation of a missing terminating delimiter? Wouldn't it end up including everything in the rest of the note regardless of approach taken.

Lets say you have an example of a note that contains 50 sections of text to extract, in the form of extra typing ****aaaaa!!!!! extra typing *****bbbbb!!!!! extra typing ....
You only want the aaaaaa and bbbbbb and not the extra typing

If you parse with the only first delimiter, you will get 51 extracts in the form ofextra typingaaaaaaaaaaaa !!!!! extra typing
bbbbbbbbbbb !!!!! extra typing
A second pass is required to remove the the extra typing and can be done using the offset command. No big deal if the delimiter is missing or mistyped

If you parse with the two delimeters, you will get 101 extracts in the form ofextra typing aaaaaaaaaaaaextra typing
bbbbbbbbbbb extra typing
A second pass is required to remove the extra typing. An assumption was made that this would be done by only using every second line.

Share this post

Link to post

1) Is there a way for me to get the author's name too? I was thinking that I could get the author's name from the "Created by" field, which would be ideal. An alternative might be to get the author's initials from a tag that authors would use. I don't think I should use the "Author" field because if you duplicate and modify a note, the original author remains. I saw that @DTLow mentioned that tag info is stored as
<meta name="keywords" content="!Journal"/> but wasn't sure how to use that info.

I was talking about my exported html notes, which is another discussion
and Author doesn't get included in the export

You can retrieve the "Author" field. Its not listed in the Applescript dictionary, but I usedsettheAuthortoauthorofoNote
There is no "Created by" field.
You can retrieve tags from the original note.

Also with our new skills (thanks JM), we can extract any part from the text of the original note if there is a consistent delimiter, like your "Created by" example

Share this post

Link to post

Huh. I can't figure it out why the same script would work for you, but not for me. I get the title of the note regardless of whether or not it has delimited content.

Here's a debugging tip. Add some "Display Dialog xxxx" statements to your script.
This will confirm that you're executing the correct script, and may give you an idea of what is being added to the Found list.

CHANGED: Do NOT produce a Summary Report Note if no notes are found with delimiters.

If no notes are found, a popup dialog is displayed, so stating.

Thanks JMichael -- the not producing a note when no notes found works great.

I tried to get at this above but here's a bit more context. In the check to see if it's nothing is between the delimiters (""), if you type just the delimiters and run it it works great. However, try the following steps and see if it still works?

1) Type the delimiters

2) Carriage return in between the delimiters

3) Erase the carriage return you just typed between the delimiters (thus bringing it back to an empty string so your check should work)

4) Run script

Does this produce the same results for you? I get it checking against a not null string (</span></span><span style="font-size: 12px;"><span style="font-family: Verdana;">)

Note: I would actually want to keep the carriage return in place and have the script ensure there is text between, not just an empty string or one/set of carriage returns. I'm just observing that it's checking against other pieces such as font if there is a carriage return and/or there was at some point a carriage return.

Share this post

Link to post

Note: I would actually want to keep the carriage return in place and have the script ensure there is text between, not just an empty string or one/set of carriage returns. I'm just observing that it's checking against other pieces such as font if there is a carriage return and/or there was at some point a carriage return.

The latest changes bypass an empty string, and you can add a bypass for the "default" text
As to other underlying html text, perhaps you should look for a process to convert the entry to pure text.

Share this post

Link to post

@JMichaelTX -- how's the update going? My project was temporarily put on hold, but I was thinking about it today and was curious if you'd had any more progress.

Sorry it is taking so long. This project got put on hold due to other pressing tasks. Hopefully I can finish this script and publish it within the next week. If you don't hear from me by then, feel free to ping me again.

Share this post

Link to post

Sorry it is taking so long. This project got put on hold due to other pressing tasks. Hopefully I can finish this script and publish it within the next week. If you don't hear from me by then, feel free to ping me again.

Just wanted to check in again and see if you had anything else on this -- thanks, @JMichaelTX!!