Paragraph styled Notes

Doing some research what is possible in Notes, its in early state. (I will update)

(**
* Paragraph styles of the body property in Notes.
**
* set theTitle to "<div><h1>Title</h1></div>"
* set theHeading to "<div><h2>Heading</h2></div>"
* set theSubheading to "<div><h3>Subheading</h3></div>"
* set theBody to "<div>Body</div>"
* set theMonospaced to "<div><tt>Monospaced</tt></div>"
*
* set theBulletedList to "<ul>
* <li>Bulletet list</li>
* </ul>"
*
* set theSpace to "<div><br></div>"
*
* set theDashedList to "<ul class=\"Apple-dash-list\">
* <li>Dashed list</li>
* </ul>"
*
* set theNumberedList to "<ol>
* <li>Numbered list</li>
* </ol>"
*
*)

-- Make a new body text with title and body.
-- its makeNoteInFolder:"MyNotes" itsTitle:"Title" itsBody:"Body"

-- Make a new body text with title and bullered list.
its makeNoteInFolder:"MyNotes" itsTitle:"Bulleted list" itsBulletedLists:{"bulletedString 1", "bulletedString 2"}

-- return its getAllBodyAndPlainTextInFolder:"MyNotes" noteNames:(its getAllNamesInFolder("MyNotes")) targetName:"Bulleted list"

on makeNoteInFolder:folderName itsTitle:titleString itsBulletedLists:bulletedStrings
	set titleString to "<div><h1>" & titleString & "</h1></div>"
	
	set returnString to ""
	repeat with anItem in bulletedStrings
		set bulletedListString to "<ul>
<li>" & (contents of anItem) & "</li>
</ul>"
		set returnString to returnString & bulletedListString & linefeed
	end repeat
	
	set bulletedListString to returnString
	set theBody to titleString & bulletedListString
	
	tell application "Notes"
		tell folder folderName
			make new note at end with properties {body:theBody}
		end tell
	end tell
end makeNoteInFolder:itsTitle:itsBulletedLists:

(**
* [makeNoteInFolder:string folderName itsTitle:string titleName]
* 	Make a new note with title and body (Paragrah Styled)
*)
on makeNoteInFolder:folderName itsTitle:titleString itsBody:bodyString
	set titleString to "<div><h1>" & titleString & "</h1></div>"
	set bodyString to "<div>" & bodyString & "</div>"
	
	set theBody to titleString & bodyString
	
	tell application "Notes"
		tell folder folderName
			make new note at end with properties {body:theBody}
		end tell
	end tell
end makeNoteInFolder:itsTitle:itsBody:


(**
* [getAllBodyAndPlainTextInFolder:string folderName noteNames:list noteNames targetName:string targetName)]
* 	Get the body and plain text of note
*)
on getAllBodyAndPlainTextInFolder:folderName noteNames:noteNames targetName:targetName
	tell application "Notes"
		tell folder folderName
			try
				if targetName is in noteNames then
					return {|plainText|:its plaintext of note targetName, bodyText:its body of note targetName}
				else
					display alert "Error: The note name \"" & targetName & "\" do not exists."
				end if
			end try
		end tell
	end tell
end getAllBodyAndPlainTextInFolder:noteNames:targetName:

(**
* [getAllNamesInFolder(string folderName)]
* 	Get the names of note
*)
on getAllNamesInFolder(folderName)
	tell application "Notes"
		tell folder folderName
			return (its name of note)
		end tell
	end tell
end getAllNamesInFolder

This script include some more tests.

(**
* Paragraph styles of the body property in Notes.
**
* set theTitle to "<div><h1>Title</h1></div>"
* set theHeading to "<div><h2>Heading</h2></div>"
* set theSubheading to "<div><h3>Subheading</h3></div>"
* set theBody to "<div>Body</div>"
* set theMonospaced to "<div><tt>Monospaced</tt></div>"
***
* set theBulletedList to "<ul><li>Bulletet list</li></ul>"
* set theLineBreak to "<div><br></div>"
* set theDashedList to "<ul class=\"Apple-dash-list\"><li>Dashed list</li></ul>"
* set theNumberedList to "<ol><li>Numbered list</li></ol>"
***
* set theBold to "<div><b>Bold font style</b></div>"
* set theItalic to "<div><i>Italic font style</i></div>"
* set theUnderline to "<div><u>Underline font style</u></div>"
* set theStrikethrough to "<div><strike>Strikethrough font style</strike></div>"
***
* set theFontColor to "<div><font color=\"#FF2600\">FontColorText</font></div>"
* set theFontColor "<span style='color: red; font-style: oblique;'>FontColorText</span>"
*)

-- Make a new body text with title and body.
-- its makeNoteInFolder:"MyNotes" itsTitle:"Title" itsBody:"Body" lineBreakAtEnd:true

(*
-- Example of using GUI scripting to make checklist
set checkList to {"CheckPoint 1", "CheckPoint 2", "CheckPoint 3"}
repeat with i from 1 to count checkList
	(its clickMenuItem:"Checklist" inMenuBarItem:"Format")
	its typeMessage(item i of checkList)
end repeat
*)

-- Make a new body text with title and bulleted list.
-- its makeNoteInFolder:"MyNotes" itsTitle:"Bulleted list" itsBulletedLists:{"bulletedString 1", "bulletedString 2"}

return its getAllBodyAndPlainTextInFolder:"MyNotes" noteNames:(its getAllNotesNamesInFolder("MyNotes")) targetName:"Bulleted list"

on makeNoteInFolder:folderName itsTitle:titleString itsBulletedLists:bulletedStrings
	set titleString to "<div><h1>" & titleString & "</h1></div>"
	
	set returnString to ""
	repeat with anItem in bulletedStrings
		set bulletedListString to "<ul>
<li>" & (contents of anItem) & "</li>
</ul>"
		set returnString to returnString & bulletedListString & linefeed
	end repeat
	
	-- Set linebreak or space between paragraph.
	set theLineBreak to "<div><br></div>"
	
	(*
	-- Examples to set a color style and font-style. (ps. still do testing...)
	set theColoredText to "<span style='color: green; font-style: oblique;'>"
	set theColoredTextEndTag to "</span>"
	*)
	
	set bulletedListString to returnString
	
	-- Test Title & BulletedList
	-- set theBody to titleString & bulletedListString
	-- set theBody to titleString & bulletedListString & theLineBreak
	
	-- The 'theColoredTextEndTag' doesn't do anything... still investigate.
	-- set theBody to titleString & theColoredText & bulletedListString & theLineBreak & theColoredTextEndTag
	
	tell application "Notes"
		tell folder folderName
			make new note at end with properties {body:theBody}
		end tell
	end tell
end makeNoteInFolder:itsTitle:itsBulletedLists:

(**
* [makeNoteInFolder:string folderName itsTitle:string titleString itsBody:string bodyString lineBreakAtEnd:boolean]
* 	Make a new note with title and body (Paragrah Styled)
*)
on makeNoteInFolder:folderName itsTitle:titleString itsBody:bodyString lineBreakAtEnd:lineBreakAtEnd
	set titleString to "<div><h1>" & titleString & "</h1></div>"
	set bodyString to "<div>" & bodyString & "</div>"
	set theLineBreak to "<div><br></div>"
	
	if lineBreakAtEnd is true then
		set theBody to titleString & bodyString & theLineBreak
	else
		set theBody to titleString & bodyString
	end if
	
	tell application "Notes"
		tell folder folderName
			make new note at end with properties {body:theBody}
		end tell
	end tell
end makeNoteInFolder:itsTitle:itsBody:lineBreakAtEnd:


(**
* [getAllBodyAndPlainTextInFolder:string folderName noteNames:list noteNames targetName:string targetName)]
* 	Get the body and plain text of note
*)
on getAllBodyAndPlainTextInFolder:folderName noteNames:noteNames targetName:targetName
	tell application "Notes"
		tell folder folderName
			try
				if targetName is in noteNames then
					return {|plainText|:its plaintext of note targetName, bodyText:its body of note targetName}
				else
					display alert "Error: The note name \"" & targetName & "\" do not exists."
				end if
			end try
		end tell
	end tell
end getAllBodyAndPlainTextInFolder:noteNames:targetName:

(**
* [getAllNotesNamesInFolder(string folderName)]
* 	Get the names of note
*)
on getAllNotesNamesInFolder(folderName)
	tell application "Notes"
		tell folder folderName
			return (its name of note)
		end tell
	end tell
end getAllNotesNamesInFolder


(*** GUI Scripting ***)

on clickMenuItem:menuItem inMenuBarItem:menuBarItem
	tell application "System Events" to tell process "Notes"
		set frontmost to true
		tell menu bar 1 -- 
			tell menu bar item menuBarItem
				click (menu item menuItem of menu 1)
			end tell
		end tell
	end tell
end clickMenuItem:inMenuBarItem:

on typeMessage(messageString)
	tell application "System Events" to tell process "Notes"
		set frontmost to true
		keystroke messageString
		keystroke return
		keystroke return
	end tell
end typeMessage

By counting the paragraph of a string of plain text, we could build styled Notes.

We have a handler to feed plain text’s (list), this text will produce paragraph styled body.

The more normal approach could be to write the text as normal and count the paragraph.
In this example I have use AppleScript’s delimiter to delete the first and last item.

ex.

set theBodyString to "
Title: Orange

Category: Food
Description: Orange tropic sweet fruit

Phone: +1 234 567 890
"
set paragraphList to its stringToParagraphList(theBodyString)

its setTitle:"MyNote" inFolder:"MyNotes" bodyStrings:paragraphList

on setTitle:titleName inFolder:folderName bodyStrings:bodyStrings
	
	-- Set the title of the note
	set titleString to "<div><h1>" & titleName & "</h1></div>"
	
	set theString to ""
	repeat with anItem in bodyStrings
		set bodyString to "<div>" & (contents of anItem) & "</div>"
		set theString to theString & bodyString & linefeed
	end repeat
	
	set theLineBreak to "<div><br></div>"
	
	set bodyString to theString
	
	tell application "Notes"
		tell folder folderName
			if titleName is in (its name of note) then
				tell note titleName to set body to (titleString & bodyString & theLineBreak)
			else
				make new note at end with properties {body:(titleString & bodyString & theLineBreak)}
			end if
		end tell
	end tell
end setTitle:inFolder:bodyStrings:

on stringToParagraphList(theItem)
	set textString to ""
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to linefeed
	set textString to theItem as text
	set AppleScript's text item delimiters to ASTID
	
	set paragraphList to {}
	repeat with i from 1 to (count paragraph of textString)
		set end of paragraphList to (paragraph i of textString)
	end repeat
	return items 2 thru -2 of paragraphList
end stringToParagraphList

So many people have done this already… :wink:

set theBodyString to "
h2: Title: Orange
->:
bb: Category: Food
bb: Description: Orange tropic sweet fruit
->:
bb: Name: Adam 'Red' Andersen
bb: Phone: +1 234 567 890
"
set paragraphList to its stringToParagraphList(theBodyString)

its setTitle:"MyNote" inFolder:"MyNotes" bodyStrings:paragraphList

on setTitle:titleName inFolder:folderName bodyStrings:bodyStrings
	
	-- Set the title of the note
	set titleString to titleName
	
	set theString to ""
	repeat with anItem in bodyStrings
		set bodyString to contents of anItem
		if (contents of anItem) begins with "h2:" then
			set bodyString to "<div><h2>" & text items 5 thru -1 of (contents of anItem) & "</h2></div>"
		else if (contents of anItem) begins with "bb:" then
			set bodyString to "<div>" & text items 5 thru -1 of (contents of anItem) & "</div>"
		else if (contents of anItem) begins with "->:" then
			set bodyString to "<div><br></div>"
		end if
		set theString to theString & bodyString & linefeed
	end repeat
	
	set theLineBreak to "<div><br></div>"
	
	set bodyString to theString
	
	tell application "Notes"
		tell folder folderName
			if titleName is in (its name of note) then
				tell note titleName to set body to (titleString & bodyString & theLineBreak)
			else
				make new note at end with properties {body:(titleString & bodyString & theLineBreak)}
			end if
		end tell
	end tell
end setTitle:inFolder:bodyStrings:

on stringToParagraphList(theItem)
	set textString to ""
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to linefeed
	set textString to theItem as text
	set AppleScript's text item delimiters to ASTID
	
	set paragraphList to {}
	repeat with i from 1 to (count paragraph of textString)
		set end of paragraphList to (paragraph i of textString)
	end repeat
	return items 2 thru -2 of paragraphList
end stringToParagraphList

More eligant way would be to make everything to be records.

set theString to "Hello World!"

its setHeadingString:theString headingLevel:"h2"

on setHeadingString:textString headingLevel:headingLevel
	set theHeading to {h1:"<div><h1>" & textString & "</h1></div>", h2:"<div><h2>" & textString & ¬
		"</h2></div>", h3:"<div><h3>" & textString & "</h3></div>"}
	
	if headingLevel is "h1" then
		return h1 of theHeading
	else if headingLevel is "h2" then
		return h2 of theHeading
	else if headingLevel is "h3" then
		return h3 of theHeading
	end if
end setHeadingString:headingLevel: