Scripting Notes in Mail

There is “Notes” under Reminders in Mail. I checked the Mail dictionary and i think they are not apple scriptable. I dont know of any freeware that can help me script non-applescriptable apps.I want to do this:
I want to use it instead of Stickes. (After selecting text or for making a new note Cmd-Shift-Y is the shortcut for Stickies) I dont want to use Stickies or something like that because i dont want to open Stickies to see it as i clutters my desktop. Mail is always open and i find it a very convenient place for notes.
Would it be possible?

Any application is scriptable. If it’s not scriptable directly with applescript commands then it can always be scripted using gui scripting. The following will do what you want with gui-type commands. To use it just highlight some text in any application. This would be the text that you want to put into the body of the note. Then run this script. Note that the delays in the script may or may not be necessary… you can check on that.

Also note that you should run this script from the script menu. This will ensure that the application where you have the selected text is frontmost when you choose the script from the script menu… that way the copy command which places the highlighted text onto the clipboard will work properly.

set myDelay to 0.3

-- hide the script in case it becomes frontmost when the script is run
set scriptName to name of me
set frontApp to my getFrontApp()
if frontApp is scriptName then
	tell application "System Events" to keystroke tab using command down
	delay myDelay
end if

tell application "System Events"
	-- put the selected text onto the clipboard
	keystroke "c" using command down
	delay myDelay
	tell application "Mail" to activate
	delay myDelay
	-- make a new note
	keystroke "n" using command down & control down
	delay myDelay
	-- get the clipboard contents
	set theText to (the clipboard) as text
	-- enter the text into the note
	keystroke theText
end tell


(*=============== SUBROUTINES ===============*)
on getFrontApp()
	set colon to ":" as Unicode text
	set dot to "." as Unicode text
	set appPath to (path to frontmost application as Unicode text)
	considering case
		if (appPath ends with colon) then
			set n to -2
		else
			set n to -1
		end if
		set astid to AppleScript's text item delimiters
		set AppleScript's text item delimiters to colon
		set appname to text item n of appPath
		if (appname contains dot) then
			set AppleScript's text item delimiters to dot
			set appname to text 1 thru text item -2 of appname
		end if
		set AppleScript's text item delimiters to astid
	end considering
	return appname
end getFrontApp

Thank you very much regulus6633 for your help.
The script does almost everything that i want except that it opens a note and begins to type this:

And when i ran the script very first time, i got the above error in Script editor. It happened a couple of times and since then it has happened in new Note which is created.
And yes i ran it from the Script menu as you told me and the result was the same.

also it would be good if it would be possible to open the note without opening Mail or hiding Mail after the note is opened (though its ok if it is not possible)

Thanks again for your help.

It sounds like the “copy” function isn’t working well, so you need to fix that. There’s 3 ideas that I have:
1. try using a longer delay time in the script. Maybe the “.3” seconds isn’t long enough. Try a very long delay time and see if it works. If it does then you know that the delay time is the problem… then you need to figure out how small you can make the delay time.

2. Maybe these 2 lines are the problem…

set scriptName to name of me
set frontApp to my getFrontApp()

Replace those lines with the following to see if scriptName and frontApp variables are working properly. The 2 variables need to be the same in order for the copy function to work.

set scriptName to name of me
display dialog scriptName
set frontApp to my getFrontApp()
display dialog frontApp

3. Remove the code for the copy function. Then you just have to copy the text to the clipboard yourself before you run the script. The script would then become the following:

set myDelay to 0.3
tell application "System Events"
	tell application "Mail" to activate
	delay myDelay
	-- make a new note
	keystroke "n" using command down & control down
	delay myDelay
	-- get the clipboard contents
	set theText to (the clipboard) as text
	-- enter the text into the note
	keystroke theText
end tell

This isn’t possible. GUI scripting works on the frontmost application… therefore mail has to come to the front and stay in the front while the script is running.

ok i just read this and nothing below it.
changed my delay to 3 instead of 0.3
and thats it.
i will find out whats the smallest delay.

thanks again.

it looks like i always have to run the script from the Script menu. How will I set a keyboard shortcut for it? I generally use QuickSilver triggers but if there is another way i would not mind.

or is it that i have to write another script (using keystrokes method) to run that script from script menu ?