Tinderbox Forum

Export to Apple Mail as rich text

Here is a bare bones AppleScript export from a Tinderbox note to a rich text mail in Apple Mail (and perhaps other email clients) based on investigations by @satikusala.

It takes the html exported from Tinderbox and uses AsObjC for the conversion. Not easy to write or read. But easy to use. Just copy-paste into Script Editor, select a message in Tinderbox, and click run.

-- 1. get html and note Name from Tinderbox
tell front document of application "Tinderbox 9"
	repeat with aNote in selections
		tell aNote
			set noteHtml to evaluate it with "exportedString(this,\"^text^\")"
			set noteName to value of attribute "Name" -- we will use Name as subject of email
		end tell
	end repeat
end tell

-- 2. send to Apple Mail as formatted rich text
use framework "Foundation" -- for NSString, NSSharingService
set ca to current application -- for more compact AsObjC syntax
--    a. Convert the exported html to NSAttributedString
set theSource to ca's NSString's stringWithString:noteHtml
set theData to theSource's dataUsingEncoding:(ca's NSUTF8StringEncoding)
set anAttributedString to ca's NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:{}
--    b. Use NSSharingService to create email, preserving the formatting in content
set aSharingService to ca's NSSharingService's sharingServiceNamed:(ca's NSSharingServiceNameComposeEmail)
set aSharingService's subject to noteName
tell aSharingService to performSelectorOnMainThread:"performWithItems:" withObject:{anAttributedString} waitUntilDone:false

Results of a test;

Of course, in most situations copy-paste may be just as easy. But this can be extended to automatically fill in multiple recipients listed in a Tinderbox attribute.

set aSharingService's recipients to "recipient1@domain1.com, recipient2@domain2.com"

2 Likes

Many thanks to @satikusala and you - this looks fascinating and potentially very useful. Can you tell me what AsObjC is?
Update: I tried to find it, but can not find a place where I can download it. Any pointers?

This is “the book” on AsObjC.

The author, Shane Stanley, is associated with Late Night Software, publisher of Script Debugger, which maintains a helpful forum here.

AsObjC allows you to call Cocoa frameworks using AppleScript. You don’t have to download it. You can incorporate it into scripts.

Many of the technicalities escape me. I’d be hard pressed, for example, to give a coherent account of exactly what a Cocoa framework is. But that doesn’t prevent me from putting this all to good use!

SG

Thank you, Gerard - so ASObjC seems to be part of the system already. I managed to get the script to produce something. Great start!

However, on a test document in Tinderbox, I could only get the script (which I copied from your message) to create an email with Apple Mail when I triggered it in the ScriptEditor. I did not get it to work when using the AppleScript menu in the menu bar when Tinderbox has the focus. Any idea why? I copy in @satikusala who maybe can help…

Like you, I am only interested in using it, not understanding the intricacies. But this looks promising!

When doing some early recce work for this (for Michael) I started in Script Debugger, then used Apple Script Editor, then the system scripts menu and then tried Shortcuts. A key point learned from that is each uses/needs separate security permissions. If you miss the initial prompt to set permissions (and for [what system/process] to set them, macOS isn’t very good at reminding you. It seems to assume “I didn’t see the message” as “No, never mention this again”. Which is … unhelpful (even if very secure).

Thanks for the hint! There is an element of overprotection here…
Did you find a way around it? Any security settings in the system settings perhaps?

I’m not expert here but, this is where you are looking for:

You can set these manually, though the latter two aren’t apps in the /Applications folder so you might need to google for their location.

Not sure if that helps?

I found Script Menu.app at /System/Library/CoreServices/Script Menu.app. Adding it didn’t help on my machine, though.

Could try saving the script as an “app” and then add that app at System Preferences > Security & Privacy > Privacy > Accessibility. Then run the app (from the Dock or whatever).

Or, just keep running it from Script Editor or Script Debugger. Simple enough.

I understand System Preferences will become Settings, with a complete makeover.