Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the '10.8: New Notes App Service' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.8: New Notes App Service
Authored by: thawk9455 on Apr 12, '13 10:37:23AM
Here's a slightly different version that doesn't overwrite your clipboard...

Open Automator and create a new Service. Set the Service to receive selected text in any application from the drop down menus.

Drag Run AppleScript from the Utilities library into the workflow.

Paste the below text in place of (* Your script goes here *):

set theInput to quoted form of (input as text)
set noteText to do shell script "echo " & theInput & " | /usr/bin/textutil -stdin -stdout -convert html"
	
tell application "Notes"
	tell account "iCloud"
		if not (exists folder "Notes") then make new folder with properties {name:"Notes"}
		tell folder "Notes"
			make new note with properties {body:noteText}
		end tell
	end tell
end tell



[ Reply to This | # ]
10.8: New Notes App Service
Authored by: Beezlegrunk on Apr 12, '13 02:28:53PM

Followed your instructions, but it didn't work for me. The new service showed up in the Services menu, but nothing happened when I selected text and then invoked the service. If it helps to diagnose the issue, Notes was already running, and I'm using OSX 10.8.3.



[ Reply to This | # ]
10.8: New Notes App Service
Authored by: Beezlegrunk on Apr 14, '13 12:39:22PM

I'm replying to my own post to issue a correction: It turns out the script does work, but because it (deliberately) doesn't steal focus from whatever app you're using when you run the service, you have no way of knowing it did so unless you check, which I obviously didn't do.

One other thing: It also calls every note created from selected text "New note", which can make it a little difficult to tell which note corresponds to which text. Is there any way to have it used the first couple of words in the new note, as Notes does when you type a note manually?

Thanks again for the script, and sorry I cried wolf about it previously ...



[ Reply to This | # ]
10.8: New Notes App Service
Authored by: gcastaneda on Apr 12, '13 04:45:36PM

Unlike the original hint, this version works for me.



[ Reply to This | # ]
10.8: New Notes App Service
Authored by: Sesquipedalian on Apr 13, '13 12:00:30AM
Much better than the original, since you avoid GUI scripting!

However, rather than hard-coding the account to use into the script, it would be better to set the script to always use whatever account the user has set as their default account. So, replace the line that says

tell account "iCloud"

with one that says

tell account id (do shell script "defaults read -app Notes DefaultAccount")

[ Reply to This | # ]
10.8: New Notes App Service
Authored by: Lri on Apr 13, '13 02:33:35AM

If the script doesn't work with rich text input, why do you need to convert the text to HTML? It makes the body use Times 12 instead of the default font.

defaults -app doesn't work with Notes (or a few other applications) for me for some reason. And I didn't even have a DefaultAccount key. It would be easier to just specify the account and folder in the script.

This also selects new notes:

on run {input, parameters}
	tell application "Notes"
		tell account 1 to tell folder "Notes"
			make new note with properties {body:input}
		end tell
		activate
	end tell
	tell application "System Events" to tell process "Notes"
		set selected of row 1 of table 1 of scroll area 1 of group 1 of splitter group 1 of window 1 to true
		set focused of group 1 of UI element 1 of scroll area 1 of group 2 of splitter group 1 of window 1 to true
	end tell
end run
Edited on Apr 13, '13 02:36:10AM by Lri


[ Reply to This | # ]
10.8: New Notes App Service
Authored by: thawk9455 on Apr 13, '13 07:13:16PM

By converting to HTML you maintain line breaks, indentation, etc...

Edited on Apr 13, '13 07:51:38PM by thawk9455



[ Reply to This | # ]
10.8: New Notes App Service
Authored by: thawk9455 on Apr 13, '13 07:59:26PM
I took a little of each of the suggestions above. I didn't activate Notes since I might not want to leave the application I'm in. I did have a default account set so that code works for me. I also was able to get the HTML conversion to set the font size but the font portion doesn't seem to be working. I'd love to hear a way to fix that cleanly...

on run {input, parameters}
	set noteText to do shell script "echo " & quoted form of (input as text) & 
		" | /usr/bin/textutil -stdin -stdout -fontsize 15 -font \"Noteworthy Light\" -convert html"
	tell application "Notes"
		tell account id (do shell script "defaults read -app Notes DefaultAccount")
			if not (exists folder "Notes") then make new folder with properties {name:"Notes"}
			tell folder "Notes"
				make new note with properties {body:noteText}
			end tell
		end tell
	end tell
	tell application "System Events" to tell process "Notes"
		set selected of row 1 of table 1 of scroll area 1 of group 1 of splitter group 1 of window 1 to true
		set focused of group 1 of UI element 1 of scroll area 1 of group 2 of splitter group 1 of window 1 to true
	end tell
end run
Edited on Apr 13, '13 08:05:11PM by thawk9455


[ Reply to This | # ]
10.8: New Notes App Service
Authored by: Lri on Apr 14, '13 06:35:25AM

And apparently the body is always HTML, so you also need to escape characters like <. Just adding white-space:pre seems to preserve whitespace though.

on run {input, parameters}
	tell application "Notes"
		tell folder 1 of account 1
			make new note with properties {body:"<body style=white-space:pre>" & my amp(input) & "</body>"}
		end tell
		activate
	end tell
	tell application "System Events" to tell process "Notes"
		set selected of row 1 of table 1 of scroll area 1 of group 1 of splitter group 1 of window 1 to true
		set focused of group 1 of UI element 1 of scroll area 1 of group 2 of splitter group 1 of window 1 to true
	end tell
end run

on amp(input)
	my replace(input, "&", "&amp;")
	my replace(result, "<", "&lt;")
	my replace(result, ">", "&gt;")
end amp

on replace(input, x, y)
	set text item delimiters to x
	set ti to text items of input
	set text item delimiters to y
	ti as text
end replace

In the previous scripts you should use printf %s, <<<, or shopt -u xpg_echo; echo. echo interprets escape sequences by default in OS X's /bin/sh, so for example do shell script "echo " & quoted form of tab prints a literal tab.



[ Reply to This | # ]