Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Need an Applescript that will Color Code paragraphs , Red, Green, Yellow; respectively in Apple Notes

Is it possible to make an Applescript that will Color Code paragraphs , Red, Green, Yellow; respectively in Apple Notes?


Would like each paragraph to be a separate color. Every script I've tried pulls up an error? Is this doable?


Thanks

iMac 21.5″, macOS 10.13

Posted on Jan 26, 2024 6:48 PM

Reply
14 replies
Sort By: 

Jan 27, 2024 8:37 AM in response to raspberryaddiction

The Notes scripting dictionary allows for the ability to create a new note with a body resource containing text HTML source. Like this:



The AppleScript code that produced this:


use scripting additions

tell application "Notes"
	set T1 to "first paragraph text"
	set T2 to "second paragraph text"
	set T3 to "third paragraph text"
	set T1_red to my colorize_paragraph(T1, "red")
	set T2_green to my colorize_paragraph(T2, "green")
	set T3_yellow to my colorize_paragraph(T3, "yellow")
	
	set colorHTML to T1_red & T2_green & T3_yellow
	
	make new note with properties {body:colorHTML, name:"color test"}
end tell
return

on colorize_paragraph(atext, acolor)
	set html_text to "
	<p style=\"color:" & acolor & ";\">" & atext & "</p>"
	return html_text as text
end colorize_paragraph





Reply

Jan 27, 2024 12:41 PM in response to raspberryaddiction

I have added a new handler that prompts you for the color coded paragraph to paste. Only the last few words of the pasted paragraph will appear in the display dialog but the entire paragraph makes it into the new note. I have also added some spacing and font size setting to clean up the paragraphs.



use scripting additions

tell application "Notes"
	
	set T1_red to my colorize_para("red")
	set T2_green to my colorize_para("green")
	set T3_yellow to my colorize_para("yellow")
	
	set colorHTML to T1_red & T2_green & T3_yellow
	
	make new note with properties {body:colorHTML, name:"color test"}
end tell
return

on colorize_paragraph(atext, acolor)
	set html_text to "
	<p style=\"font-size:14pt;color:" & acolor & ";\">" & atext & "</p><br />"
	return html_text as text
end colorize_paragraph

on colorize_para(acolor)
	set atext to text returned of (display dialog "Enter " & acolor & " text here: " default answer "")
	
	set html_text to "
	<p style=\"font-size:14pt;color:" & acolor & ";\">" & atext & "</p><br />"
	return html_text as text
end colorize_para

Reply

Jan 27, 2024 11:44 AM in response to raspberryaddiction

Thanks; I see it creates a new note called color test; I see I can just insert text manually; but still can't paste an entire paragraph. Or is there a way to paste the entire document and have each paragraph set to the appropriate color?


Thanks again

Reply

Jan 27, 2024 1:26 PM in response to VikingOSX

appreciate your time; I'll play with that script. Thanks. As a songwriter I like to color code paragraphs of songs and it's just a time consuming nuisance. This is a job for applescript. Thanks again.


That comes pretty close; I'll see if I can find a way to allow it to paste the entire note in a single prompt with say 12 paragraphs and have it color code each green, red, yellow; ad infinitum. Thanks again.

Reply

Jan 27, 2024 1:41 PM in response to VikingOSX

I was able to create a script that allows one prompt for the entire document but it changes every line to a different color. Are you able to fix this so it changes every paragraph to a different color? Thanks


tell application "Notes"

set inputText to text returned of (display dialog "Paste the entire document here:" default answer "")


set paragraphsList to paragraphs of inputText

set colorHTML to ""


repeat with i from 1 to count of paragraphsList

set currentParagraph to item i of paragraphsList

set currentColor to my get_color(i)


set html_text to "

<p style=\"font-size:14pt;color:" & currentColor & ";\">" & currentParagraph & "</p><br />"


set colorHTML to colorHTML & html_text

end repeat


make new note with properties {body:colorHTML, name:"New Color Template"}

end tell

return


on get_color(index)

-- Determine color based on the index

if (index mod 3) is 1 then

return "green"

else if (index mod 3) is 2 then

return "red"

else

return "yellow"

end if

end get_color


Reply

Jan 28, 2024 5:58 AM in response to raspberryaddiction

I copied your script verbatim and ran it against the same pasted document content that I used to produce the tricolor Notes content I posted earlier. Got that result, with paragraphs colored green, red, and yellow in that order.


I am using a text document, where each new paragraph is preceded by just a newline, or a blank line. Makes no difference in the result.


I see no evidence of alternating line colors here with Notes on macOS Sonoma 14.3.

Reply

Jan 29, 2024 2:43 AM in response to raspberryaddiction

AppleScript expects plain text, and copying content from a word-processing document or another non-plain text source will likely introduce control characters and attributed text that interferes with AppleScript's handling of that content.


I just fired up my 2011 Mac mini running macOS High Sierra 10.13.6, copied your unchanged AppleScript into that script editor, and using the same plain text as I have done before, the script produced the separate colored paragraphs in Apple Notes.

Reply

Jan 29, 2024 5:15 AM in response to raspberryaddiction

What are you really up to? Not about Notes or your proposed solution using Notes, but what issue or requirement are you looking to solve? Terminal session with a shell script can show colored text, for instance.


Maybe write a small Swift app? Or Python or whatever?


If this requirement really centrally involves Notes, maybe pick an alternative app better suited to your (unstated) goals? Notes has gotten some improvements, but it’s still intentionally a simple and limited tool.



PS: Mac and Pasting Text: in various tools, Command-Shift-V removes most formatting

Reply

Need an Applescript that will Color Code paragraphs , Red, Green, Yellow; respectively in Apple Notes

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.