AppleScript - Quick Memo

Here’s a short AppleScript (based on the Notes AppleScript Programming Code Examples Db) that will ask for a recipient, a subject and the body of the memo. It will then compose and send the memo.

One caveat: You must type in a valid e-mail address, it won’t look up the names. But you don’t have to enter the whole name (User Name/Department/Location/Company) just a valid name (e.g., John H. Smith, Susan Jones, H.L. Johnson).

(I had a version that would also attach a file, but I lost it. It was actually a drag-and-drop application - you could drag the file you wanted to send on it and it would create the memo. If I re-create it, I will post it).

Here’s the code:

property ToAddress : “”

property CCAddress : “”

set ToAddress to text returned of (display dialog “Enter Addressee’s name:” default answer “Send To”)

set mySubject to text returned of (display dialog “Enter Subject:” default answer “Subject”)

set myBody to text returned of (display dialog “Enter Body:” default answer “Body”)

tell application “Notes”

set myDB to make new database with data {"", ""}

try

	openmail myDB

end try



set myDoc to make new document with data {myDB}

replaceitemvalue myDoc itemname "SendTo" newvalue ToAddress

replaceitemvalue myDoc itemname "Subject" newvalue mySubject

replaceitemvalue myDoc itemname "Body" newvalue myBody

--replaceitemvalue myDoc itemname "CopyTo" newvalue CCAddress

send myDoc without attachform

end tell

Subject: AppleScript - Quick Memo & Attachment

Here’s the same AppleScript as previous (create memo) but with the added feature of attaching a file.

property ToAddress : “”

property CCAddress : “”

set ToAddress to text returned of (display dialog “Enter Addressee’s name:” default answer “Send To”)

set mySubject to text returned of (display dialog “Enter Subject:” default answer “Subject”)

set myBody to text returned of (display dialog “Enter Body:” default answer “Body”)

set myFile to (choose file with prompt “Choose a file:”) as string

tell application “Notes”

set myDB to make new database with data {"", ""}

try

	openmail myDB

end try



set myDoc to make new document with data {myDB}

replaceitemvalue myDoc itemname "SendTo" newvalue ToAddress

replaceitemvalue myDoc itemname "Subject" newvalue mySubject





set myRichText to createrichtextitem myDoc itemname "Body"

appendtext myRichText numactions myBody & return & return

embedobject myRichText type 1454 class "" source myFile



send myDoc without attachform

end tell

Subject: AppleScript - Quick Attachment, Memo via Contextual menu

Hi Christopher,maybe this is another modification of your original script, I found this somewhere in the Notes Forums and modified it to my needs.

If you have the Big Cat Scripts (contextual menu) Plugin installed (see http://ranchero.com/bigcat/ ), you can send any file(s) directly from the Finder via Ctrl-Klick.

BUT - I have problems attaching files with long filenames! As I read you extended script, this should be a problem with your script, too.

Regards, Thomas

— Here’s the script —

property ToAddress : “”
property alertFilesize : 2000000

on run
tell application “Finder” to set selecteditems to the selection
set the_File to selecteditems as list
main(the_File)
end run

– ---------------------------- Main Subroutine is used by BigCat ----------------------------
on main(filelist)
(* When double-clicked as an application, it asks for a file *)
log (count item of filelist) & “items in the_File”
log class of filelist

if the (count of items in filelist) is 0 then
	set filelist to choose file with prompt "Choose a file to send:" as list
	-- display dialog (data size of the_File)
	
	(* Send alias as list  - otherwise it creates an error*)
	send_file(filelist as list)
else
	-- display dialog "Los geht’s."
	
	(* Check for large Attachments *)
	getFilesize(filelist)
	
	(* Remove folders from list *)
	set filelist to cleanList(filelist as list)
	
	
	(* Send the files *)
	send_file(filelist)
end if

end main

– ---------------------------- Clean List of folders ----------------------------
on cleanList(filelist)
set cleanFileList to {}
repeat with oneItem in filelist
set singleItem to oneItem as alias
log class of singleItem
if singleItem as text does not end with “:” then
– display dialog item i in filelist as text
set cleanFileList to cleanFileList & (singleItem as alias)
log “<” & singleItem & “> added to cleanFileList”
end if
end repeat
if (count of items in cleanFileList) is 0 then
display dialog “Nothing to send, you might have selected only folders.”
quit
end if
log cleanFileList
return cleanFileList
end cleanList

– ---------------------------- Make Filename-List ----------------------------
on getFilenames(filelist)
set names to {}
repeat with theItem in filelist
(*pfad-Angaben in Filenamen umwandeln )
try
–set theattachments to theattachments & (((folder of theItem as text) & (name of theItem)) as alias)
set names to names & " [" & name of theItem & "] "
on error number -1728
(
Fehler -1728 ignorieren *)
end try
end repeat
–display dialog names
return names
end getFilenames

– ---------------------------- Make Filename-Selection ----------------------------
(* This will make the selcetionList used by getFilesize sub *)
on makeSelection(filelist)
set selectionList to {}
repeat with i from 1 to (count of items in filelist)
set selectionList to selectionList & (name of (item i of filelist))
end repeat
return selectionList
end makeSelection

------------------------------ Get Size of Files ----------------------------
on getFilesize(filelist)
–display dialog “Checking filesize”
set itemSize to “”
repeat with theItem in filelist
– display dialog theItem as string
set itemSize to itemSize + (data size of theItem)
end repeat
–display dialog itemSize / 1000
if the itemSize is greater than alertFilesize then
repeat
set theResult to display dialog “Achtung!” & return & ¬
“Die Attachment(s) sind grösser als 2 MB!” & return & ¬
return & “Filesize: " & {round (itemSize / 1000) rounding as taught in school} & " kB” with icon caution ¬
buttons {“Continue”, “Cancel”, “Show Files”} default button “Cancel”
if button returned of theResult is “Show Files” then
set filelist to choose from list makeSelection(filelist) OK button name ¬
“Continue” with multiple selections allowed and empty selection allowed
else if button returned of theResult is “Continue” then
exit repeat
end if
end repeat
end if
end getFilesize

– – RunScript again
on runAgain(the_File)
–getFilesize(the_File)
send_file(the_File)
end runAgain

------------------------------ Send Function & Notes Stuff ----------------------------
on send_file(filelist)
set CCAddress to “”
set Subject to “”
set ctitems to count of items in filelist
display dialog (“You have selected " & ctitems & " item(s).”) with icon 0 buttons (“I know!”) giving up after 1

---- Ask for recipient
repeat
	set SendTo to display dialog "Send this file to:" buttons {"Cancel", "Add CC List", "OK"} ¬
		default answer

ToAddress default button “OK”
set ToAddress to the text returned of SendTo
if (ToAddress is not “”) then
exit repeat
end if
end repeat

----- Add CC recipient
if the button returned of SendTo is "Add CC List" then
	set CCTo to display dialog "CC this file to: (separate by comma)" buttons {"Cancel", "OK"} ¬
		default answer "" default button "OK"
	if the button returned of CCTo = "OK" then
		set CCAddress to the text returned of CCTo
	end if
end if

----- Enter Subject
set SubjectEntry to display dialog "Enter the subject line:" buttons ¬
	{"Cancel", "OK"} default answer ("File(s): " & (getFilenames(filelist) as string)) default button "OK"
set Subject to the text returned of SubjectEntry

----- Enter Body Text
set BodyEntry to display dialog "Enter any additonal comments:" buttons ¬
	{"Cancel", "OK"} default answer ("See the attachments:" & return) default button "OK"
set BodyText to the text returned of BodyEntry


----- Notes Stuff
tell application "Notes"
	set myDB to make new database with data {"", ""}
	try
		openmail myDB
	end try
	
	set myDoc to make new document with data {myDB}
	set rtitem to make new richtextitem with data {myDoc, "Body"}
	
	replaceitemvalue myDoc itemname "Body" newvalue BodyText
	repeat with i from 1 to ctitems
		tell application "Finder"
			set singleItem to (item i of filelist) as text
			log "Trying to embed: <" & singleItem & ">"
		end tell
		--display dialog singleItem
		try
			embedobject rtitem type 1454 class "" source singleItem
		on error
			tell application "Finder"
				beep
				set theResult to display dialog "Can’t attach a folder or filename is too long." buttons ¬
					{"Show Problem", "Cancel", "Continue anyway"} default button "Continue anyway"
				if button returned of theResult is "Show Problem" then
					display dialog singleItem buttons {"Continue"} default button "Continue"
				end if
			end tell
			-- appenditemvalue myDoc numactions "Body" newvalue ("Not attached: [" & singleItem & "]")
		end try
	end repeat
	replaceitemvalue myDoc itemname "SendTo" newvalue ToAddress
	replaceitemvalue myDoc itemname "Subject" newvalue Subject
	replaceitemvalue myDoc itemname "CopyTo" newvalue CCAddress
	
	send myDoc without attachform
end tell

log "---->>>>>>> Mail created"

end send_file