Barebones script for creating new notes in TB8

I’ve had good luck with the following script for importing multiple messages from Apple Mail. Best used first on a new Tinderbox document to avoid messing up an old one. Because of so many Apple Events it’s (much) slower than “spreadsheet import” pasting csv but (in my experience) less fiddly. It demonstrates some possible approaches to using the new scripting support. I hope it will attract improvement suggestions and other scripting examples.

--------------------------------------------------------------------------------
# Imports messages selected in Apple Mail into front Tinderbox 8 document
# Automates setting up container and prototype
# Skips messages already present in designated Tinderbox container
# Slow (lots of Apple Events) so watch progress indicator
--------------------------------------------------------------------------------

set containerName to "Imported Emails"
set prototypeName to "pEmail"
set keyAttributesStr to "URL;MyDate;MyString;MyBoolean"

##  Other user attributes must first be added manually to Tinderbox. See comment below.

global existingURLs -- used to skip duplicate imports

tell application "Tinderbox 8"
	tell front document
		if not (exists) then error "No Tinderbox document open."
		#  create container for imported messages
		if not (exists note containerName) then
			set newNote to make new note at before first note
			tell newNote to set name to containerName
		end if
		#  list URLs of notes already in Tinderbox container (used to skip duplicates)
		set existingURLs to value of attribute "URL" of note containerName's notes
		#  create prototype with key attributes imported message notes will inherit
		if not (exists note prototypeName) then
			set newPrototype to make new note
			tell newPrototype
				set value of attribute "Name" to prototypeName
				set value of attribute "KeyAttributes" to keyAttributesStr
				set value of attribute "badge" to "mail"
				set value of attribute "IsPrototype" to true
			end tell
		end if
	end tell
end tell

tell application "Mail"
	try
		set selectedMessages to selection ## list of references to messages selected in Mail
		set selectedMsgsCount to length of selectedMessages
		if selectedMsgsCount is less than 1 then error "No messages selected in Mail."
		set processedMsgsCount to 0
		my initProgress(selectedMsgsCount) # initialize progress reporter
		repeat with aMessage in selectedMessages
			my importMessage(aMessage, containerName, prototypeName)
			set processedMsgsCount to processedMsgsCount + 1
			my showProgress(selectedMsgsCount, processedMsgsCount) # report progress
		end repeat
	on error error_message number error_number
		if error_number is not -128 then display alert "Mail" message error_message as warning
	end try
end tell
display notification "Done processing " & processedMsgsCount & " messages"

to importMessage(aMessage, containerName, prototypeName)
	try -- used to fake a 'continue' to prevent import of message already in Tinderbox
		tell application "Mail"
			tell aMessage
				set {msgSubj, msgSender, msgContent, msgID} to {subject, sender, content, message id}
				set msgURL to "message://%3C" & msgID & "%3E" --> active link to message
				set dateSentStr to my dateToStr(date sent)
				set existsAttach to exists mail attachments --> boolean
			end tell
			if msgURL is in existingURLs then error "Duplicate" # skip, "continue" to next message
			if msgSubj is equal to "" then set msgSubj to "(no subject)"
			tell application "Tinderbox 8"
				tell front document
					set theContainer to note containerName --> a reference
					set newNote to make new note at theContainer
					tell newNote
						##  Map EXISTING Tinderbox 'attributes' to Mail Message 'properties' here ##
						##  Other user attributes must first be added manually to Tinderbox ##
						set value of attribute "Name" to msgSubj
						set value of attribute "Text" to msgContent
						set value of attribute "MyDate" to dateSentStr
						set value of attribute "MyString" to msgSender
						set value of attribute "URL" to msgURL
						set value of attribute "MyBoolean" to existsAttach
						set value of attribute "Prototype" to prototypeName
						## Assign any non-Mail-related attribute values not inherited from prototype
						set value of attribute "Width" to 8
					end tell
				end tell
			end tell
		end tell
	on error errorMsg number errorNum
		display notification errorMsg & " " & msgURL
	end try
end importMessage

to initProgress(selectedMsgsCount)
	set progress total steps to selectedMsgsCount
	set progress description to "Importing messages ..."
end initProgress

to showProgress(selectedMsgsCount, n)
	set progress additional description to "Processing message " & n & " of" & selectedMsgsCount
	set progress completed steps to n
end showProgress

to dateToStr(aDate) --> convert AppleScript date to string in format that Tinderbox recognizes
	tell aDate to return short date string & ", " & time string
end dateToStr

EDIT:

Here’s a faster, less convoluted, bare-bones Apple Mail to Tinderbox scrip with clickable links back to Mail.


--------------------------------------------------------------------------------
# Imports messages selected in Apple Mail into front Tinderbox 8 document
# Clickable link back to message in Mail -- URL attribute
# TO USE: Select messages in Mail, and with Tinderbox doc open, click run
--------------------------------------------------------------------------------

set containerName to "Imported Emails"

# NOTE: Use only existing Tinderbox attributes; add manually to TBX as needed
set keyAttributesStr to "URL;MyDate;MyString;MyBoolean" --<<<-- these in any Tbx 8 doc by default

# Initialize list variables to hold Apple Mail message properties
set {subjectLst, contentLst, datesentLst, senderLst, msgurlLst, attachLst} to {{}, {}, {}, {}, {}, {}}

set importCounter to 0

tell application "Mail"
	set selectedMessages to selection
	set selectedMsgsCount to selectedMessages's length
	
	# place each Mail message 'property' in an AppleScript list
	repeat with aMessage in selectedMessages
		tell aMessage
			set end of subjectLst to subject
			set end of contentLst to content
			set end of datesentLst to my dateToStr(date sent)
			set end of senderLst to sender
			set end of msgurlLst to "message://%3C" & message id & "%3E" --> clickable link
			set end of attachLst to exists mail attachments --> boolean	
		end tell
	end repeat
end tell

tell application "Tinderbox 8"
	tell front document
		if not (exists) then error "No Tinderbox document open."
		
		#  create container for imported messages
		if not (exists note containerName) then
			set newNote to make new note at before first note
			tell newNote to set name to containerName
		end if
		
		# create a note for each selected Mail message in designated container
		repeat with i from 1 to selectedMsgsCount
			set newNote to make new note at note containerName
			tell newNote
				## Map Tinderbox 'attributes' to Apple Mail message 'properties':
				set value of attribute "Name" to item i of subjectLst
				set value of attribute "Text" to item i of contentLst
				set value of attribute "MyDate" to item i of datesentLst
				set value of attribute "MyString" to item i of senderLst
				set value of attribute "URL" to item i of msgurlLst
				set value of attribute "MyBoolean" to item i of attachLst
				## Assign values to non-Mail-related attributes:
				set value of attribute "KeyAttributes" to keyAttributesStr
				set value of attribute "Width" to 8
			end tell
			set importCounter to importCounter + 1
		end repeat
		
	end tell
end tell

display notification "Imported " & importCounter & " messages of " & selectedMsgsCount & " selected"

## ----------------- handlers (=subroutines) -----------------------

to dateToStr(aDate) --> convert AppleScript date to string format that Tinderbox recognizes
	tell aDate to return short date string & ", " & time string
end dateToStr

And here’s one that adds some bells and whistles like creating a prototype for the imported messages and checking to avoid overwriting messages already in the Tinderbox container (from a previous import), as well as reporting progress.

-------------------------------------------------------------------------------
# Imports messages selected in Apple Mail into front Tinderbox 8 document
# Creates container and prototype (if they don't already exist)
# Skips messages already in container -- so no duplicates
# Links back to message in Mail -- URL attribute

# TO USE: Select messages in Mail, and with Tinderbox doc open, click run
--------------------------------------------------------------------------------

# Name these whatever:
set containerName to "Imported Emails"
set prototypeName to "pEmail"

# NOTE: Use only existing Tinderbox attributes; add manually to TBX as needed
set keyAttributesStr to "URL;MyDate;MyString;MyBoolean" --<<<-- these in any Tbx 8 doc by default

# Initialize list variables to hold Apple Mail message properties
set {subjectLst, contentLst, datesentLst, senderLst, msgurlLst, attachLst} to {{}, {}, {}, {}, {}, {}}

set {importCounter, processedCounter} to {0, 0}

tell application "Mail"
	set selectedMessages to selection
	set selectedMsgsCount to length of selectedMessages
	
	my initProgress(selectedMsgsCount) # initialize progress reporter
	
	# place each Mail message 'property' in an AppleScript list
	repeat with aMessage in selectedMessages
		tell aMessage
			set end of subjectLst to subject
			set end of contentLst to content
			set end of datesentLst to my dateToStr(date sent)
			set end of senderLst to sender -- or: (extract name from sender)
			set end of msgurlLst to "message://%3C" & message id & "%3E" --> clickable link
			set end of attachLst to exists mail attachments --> boolean	
		end tell
	end repeat
	
end tell

tell application "Tinderbox 8"
	tell front document
		if not (exists) then error "No Tinderbox document open."
		
		#  create container for imported messages
		if not (exists note containerName) then
			set newNote to make new note at before first note
			tell newNote to set name to containerName
		end if
		
		#  create prototype with key attributes, appearance for imported message notes to inherit
		if not (exists note prototypeName) then
			set newPrototype to make new note
			tell newPrototype
				set value of attribute "Name" to prototypeName
				set value of attribute "KeyAttributes" to keyAttributesStr
				set value of attribute "badge" to "mail"
				set value of attribute "IsPrototype" to true
			end tell
		end if
		
		#  list URLs of notes already in Tinderbox container -- used to skip duplicate imports
		set existingURLs to value of attribute "URL" of notes of note containerName
		
		# create a note for each selected Mail message not already in Tinderbox container
		repeat with i from 1 to selectedMsgsCount
			set alreadyInTbxContainer to item i of msgurlLst is in existingURLs --> boolean
			if not alreadyInTbxContainer then
				set newNote to make new note at note containerName
				tell newNote
					# Use only existing Tinderbox attributes; add manually to TBX as needed
					## Map Tinderbox 'attributes' to Apple Mail message 'properties':
					set value of attribute "Name" to item i of subjectLst
					set value of attribute "Text" to item i of contentLst
					set value of attribute "MyDate" to item i of datesentLst
					set value of attribute "MyString" to item i of senderLst
					set value of attribute "URL" to item i of msgurlLst
					set value of attribute "MyBoolean" to item i of attachLst
					## Assign prototype:
					set value of attribute "Prototype" to prototypeName
					## Assign any non-Mail attribute values not inherited from prototype:
					set value of attribute "Width" to 8
				end tell
				set importCounter to importCounter + 1
			end if
			set processedCounter to processedCounter + 1
			my showProgress(selectedMsgsCount, processedCounter) # report progress
		end repeat
	end tell
end tell

display notification "Imported " & importCounter & " messages of " & selectedMsgsCount & " selected"

## handlers (=subroutines) ##

to dateToStr(aDate) --> convert AppleScript date to string format that Tinderbox recognizes
	tell aDate to return short date string & ", " & time string
end dateToStr

to initProgress(selectedMsgsCount)
	set progress total steps to selectedMsgsCount
	set progress description to "Processing messages ..."
end initProgress

to showProgress(selectedMsgsCount, processedCounter)
	set progress additional description to "Importing message " & processedCounter & " of " & selectedMsgsCount
	set progress completed steps to processedCounter
end showProgress

3 Likes