Hey guys, took me some time but I got it working this morning (I'm Australian). Here's my version of the script that swaps around the title of the note and the first line of the body, based on all the notes you have selected in Evernote. I removed the dialog box from JMichaelTX's code for my own convenience
property ptyScriptName : "Set Evernote Note Title to 2nd Body Line Using Plain Text -- TEST"
property ptyScriptVer : "BETA 2.0"
property ptyScriptDate : "2019-05-19"
property ptyScriptAuthor : "JMichaelTX"
(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE:
• Update Evernote Note Title and Body Using HTML
• Set Note Title to 2nd Body Line
• Insert Original Note Title at Top of Body
RETURNS: Revised Evernote Note
REQUIRED:
1. macOS 10.11.6+
2. Mac Applications
• Evernote Mac 6.11.1+
TAGS: @SW.EN @CAT.Notes @CAT.HTML @type.Example
REF: The following were used in some way in the writing of this script.
1. 2019-05-17, DTLow, Evernote User Forum
Bulk Rename Notes with Script Editor
https://discussion.evernote.com/topic/120023-bulk-rename-notes-with-script-editor/?do=findComment&comment=537798
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation" -- this may not be required
use framework "AppKit" -- this may not be required
use scripting additions
property LF : linefeed
tell application "Evernote"
set selectedNotes to (get selection)
repeat with oNote in selectedNotes
tell oNote
set noteTitle to title
set noteBodyHTML to HTML content -- Retrieve note contents
set noteBodyPtxt to my HTMLDecode(noteBodyHTML)
set nbLines to paragraphs of noteBodyPtxt
--- Extract the 2nd Line of Text ---
-- (ignoring blank lines)
set lineCount to 0
repeat with oLine in nbLines
if ((contents of oLine) ≠ "") then set lineCount to lineCount + 1
if (lineCount ≥ 1) then
set nbLine2 to contents of oLine
exit repeat
end if
end repeat
--- Replace Unusual "Â" Character with Space ---
set nbLine2Clean to my regexChange("Â", " ", nbLine2)
--- REPLACE INVISIBLE CHARS FOUND IN SOME HTML (like OUTLOOK 2011) WITH SPACE ---
-- set nbLine2Clean to regexChange("\\x{A0}|\\x{2028}", " ", nbLine2Clean)
set bodyLine2 to nbLine2Clean
--- Change Note Title to 2nd Body Line ---
set title to bodyLine2
--- Insert Original Title at Top of Body ---
set HTML content to ("<div>" & noteTitle & "</div>") & noteBodyHTML
end tell
end repeat
end tell
--~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on regexChange(pFindStr, pReplaceStr, pSourceStr) -- @RegEX @Change @Replace @Strings @ASObjC
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
(* VER: 2.0 2018-07-06
PURPOSE: Change ALL Occurances of pFindStr into pReplaceStr in pSourceStr
METHOD: Uses ASObjC RegEx (which is based on ICU Regex)
PARAMETERS:
• pFindStr | text | RegEx Pattern to Find
• pReplaceStr | text | Replace string which
• May contain these RegEx MetaChars:
\\n, \\r, \\t for linefeed, return, and tab
$<CG>, where <CG> is Capture Group number
• pSourceStr | text | Source String to be searched
RETURNS: Revised String with all changes that were found.
AUTHOR: JMichaelTX -- with help from NigelGarvey
## REQUIRES: use framework "Foundation"
REF:
1. 2018-07-06, NigelGarvey, Late Night Software Ltd.
ASObjC RegEx Change Handler
http://forum.latenightsw.com/t/asobjc-regex-change-handler/1395/10
2. ICU RegEx Users Guide
http://userguide.icu-project.org/strings/regexp
--–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
*)
set nsCurApp to current application
-- Replace any backslash-n sequences in the replace string with actual character --
set nsReplaceStr to nsCurApp's NSString's stringWithString:pReplaceStr
set nsReplaceStr to nsReplaceStr's stringByReplacingOccurrencesOfString:"\\n" withString:(linefeed)
set nsReplaceStr to nsReplaceStr's stringByReplacingOccurrencesOfString:"\\r" withString:(return)
set nsReplaceStr to nsReplaceStr's stringByReplacingOccurrencesOfString:"\\t" withString:(tab)
set nsSourceStr to nsCurApp's NSString's stringWithString:pSourceStr
set nsSourceStr to (nsSourceStr's stringByReplacingOccurrencesOfString:pFindStr withString:nsReplaceStr options:(nsCurApp's NSRegularExpressionSearch) range:{0, nsSourceStr's |length|()})
return nsSourceStr as text
end regexChange
--~~~~~~~~~~~~~~~~~~~~ END of Handler ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on HTMLDecode(HTMLString) -- @HTML @Strings @Decode @Convert @Text @PlainText @ASObjC
--------------------------------------------------------------------------------------
(*
From: Shane Stanley <sstanley@myriad-com.com.au>
Date: Thu, Jun 16, 2016 at 8:56 PM
To: "ASUL (AppleScript)" <applescript-users@lists.apple.com>
Subject: Re: Convert HTML Text to Plain Text
-- ASObjC handler is 2.03 X as fast as Shell Handler
*)
-- make it an NSString
set theString to current application's NSString's stringWithString:HTMLString
-- get raw data
set theData to theString's dataUsingEncoding:(current application's NSUTF8StringEncoding)
-- convert to attributed string
set attStr to current application's NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:(missing value)
-- extract just the text
set plainText to attStr's |string|() as text
--- REPLACE INVISIBLE CHARS FROM OUTLOOK WITH SPACE ---
-- set plainText to regexChange("\\x{A0}|\\x{2028}", " ", plainText)
return plainText
end HTMLDecode