"Add Selection to InBox" Service


#1

Dear all,
Would it be possible to have a service script, like the “quick entry” TaskPaper, that adds the selected text from any app to an “inbox” predetermined section of an FT already-opened doc? Would be great!
best
ph


#2

Hi,

I just added a quick function to do just that in my Alfred Workflow for FoldingText. You can get the workflow from my GitHub:

The main script is:

property pTitle : "Add Message to @inbox tag"

property pVer : "0.1"
property pAuthor : "Richard Guay"
property pDescription : "

1. Looks for a node with the @inbox tag,
2. add the message given to that point

"

property addMessage : "

function(editor, options) {
	var tree=editor.tree(), q = options.strip;
	
	tree.nodes().forEach(function (oNode) {
		Object.keys(oNode.tags()).forEach(function(strTag) {
			if(strTag === 'inbox') {
				var message = tree.createNode(q);
				oNode.appendChild(message);
			}
		});
	});
	return(q);
}

"

on alfred_script(q)
tell application "FoldingText"
set lstDocs to documents
if lstDocs ≠ {} then
tell item 1 of lstDocs
evaluate script addMessage with options {strip:q}
end tell
end if
end tell
end alfred_script


#3

A couple of quick thoughts about that approach - it doesn’t look as if stops looping after finding its first @inbox-tagged node, so one might experience multiple deliveries, or a lot of continued looping after delivery.

Another approach might be to narrow down the target (e.g. to the first heading named Inbox, or to the first line tagged @inbox) by using lstNodes=editor.tree().evaluateNodePath(strPath)

Where strPath might be something like:

'//(inbox and @type=heading)[0]'

or

'//@inbox[0]'

(A good approach to getting the nodePath you want is to experiment interactively with Jamie Kowalski’s filter plugin)


#4

hi Richard,
thx for your quick reply; I shall try this hint.
best regards,
ph


#5

Yes, I agree a better search would be nice. But, I spent 5 minutes throughing this together from another script. Since I would assume only one @inbox tag, it seemed as a logical approach. Just not totally efficient.


#6

Absolutely – a quick fix is always good, and whatever works is best.

Nodepaths hugely reward exploration (in editing and search as well as in scripting), and in the meanwhile, if you want to stick to iteration, you could use javascript break; under the lines which find and append, and use FoldingText’s built-in boolean function node.hasTag(strTagName) to branch on whether a node has the tag you are looking for.

file:///Applications/FoldingText.app/Contents/Resources/dist/sdk/docs/node.Node.html#hasTag

or FoldingText > Help > Software Development Kit > Documentation > Node > hasTag()


#7

Okay, I cleaned it up as you said and added another command to list all tags and add a note to the selected tag. I updated the GitHub for the Alfred FoldingText Workflow referenced above. Try it out and let me know how it works for you all.

I added a @math tag to a line with my imath extension. Now, I can just through equations to it and it get evaluated. Very handy.


#8

Hey Rob/Complexpoint— I’m trying to cobble together my own script to grab the uid and subject from the currently selected message in mail.app and import into my “inbox”. Do you think this might be a good starting point, or would you advise a different route? My grasp of Applescript isn’t particularly strong, but I’m willing to spend some time trying to hack something together…


#9

Your inbox is a particular file ?

Or something like an ## Inbox heading in a particular file ?

(Applescript should be fine – and of course there is always drag and drop from Mail to FT, which creates a Markdown link)

(If you’re Inbox is a file rather than a heading, you could write something to simply append the email link to the end of an existing text file, without having to wake up FoldingText, if it isn’t active)


#10

I have a main file for tasks and projects. My inbox is a top level heading (#) near the top of that file. I’m currently using the drag and drop method, but I’m trying to figure out a method I can attach to a keypress/shortcut. I figure that’ll be easier when processing a list of incoming email.

Also: it seems I’m not 100% accurate with the drop. The number of times I’ve dropped a mail uid in the middle of a pre-existing task and had to backtrack… *smiles


#11

Perhaps sth like this rough draft ?

property pTitle : "Selected Mail.app msg to FT Inbox"
property pVer : "0.1"
property pAuthor : "Rob Trew Twitter: @complexpoint"

-- EDIT THE FOLLOWING DETAILS TO SET THE BEHAVIOUR OF THE SCRIPT
property pblnAppendToFile : false -- (set to true if you simply want to append to the end of a named text file)
property pblnAddToTop : true -- (if adding to # Inbox section, add at top or end ?)
property pstrFilePath : "$HOME/Library/Application Support/Notational Velocity/Inbox.txt"
property pstrNodePath : "/Inbox" -- (Assumes that inbox is a top level heading, if it exists)

property precOptions : {inboxpath:pstrNodePath, top:pblnAddToTop}

property pstrJS : "

	function(editor, options) {
		var oTree = editor.tree(),
			lstInbox = oTree.evaluateNodePath(options.inboxpath),
			oInbox, strText, lstChiln, strMsg=options.msg, lstLines = strMsg.split('\\n'),
			lngLines = lstLines.length, i, oFirstChild=null, blnTop = options.top;
	
		if (lngLines) {
			// CHECK THAT WE HAVE AN INBOX (CREATING ONE IF NECESSARY)
			if (lstInbox.length) {
				oInbox = lstInbox[0];
			} else {
				oInbox = oTree.createNode('# Inbox');
				oTree.appendNode(oInbox);
			}
			oTree.ensureClassified();
			if (oInbox.hasChildren()) {
				oFirstChild = oInbox.children()[0];
			}
			// ADD NEW LINES EITHER AT START OR END OF INBOX
			if (blnTop && oFirstChild) {
				for (i=0; i<lngLines; i++) {
					oInbox.insertChildBefore(oTree.createNode(lstLines[i]),oFirstChild);
				}
			} else {
				for (i=0; i<lngLines; i++) {
					oInbox.appendChild(oTree.createNode(lstLines[i]));
				}
			}
		}
	}

"

on run
	-- TRY TO GET A NORMALISED VERSION OF THE FILENAME
	set strPath to (do shell script "echo \"" & pstrFilePath & "\"")
	
	-- GET MD LINKS TO ANY SELECTED MAIL.APP MESSAGES
	set strMD to MailSelnAsMd()
	if strMD ≠ "" then
		
		-- EITHER APPEND TO AN INBOX TEXT FILE (IF property pblnAppendToFile : TRUE)
		if pblnAppendToFile then
			set strCMD to "echo " & quoted form of strMD & " >> " & quoted form of strPath
			do shell script strCMD
			
		else
			-- OR OPEN AS DOC IN FT AND ADD TO AN INBOX SECTION (IF property pblnAppendToFile : FALSE)
			set recOptions to {msg:strMD} & precOptions
			tell application "FoldingText"
				set oDoc to open strPath
				tell oDoc to set varResult to (evaluate script pstrJS with options recOptions)
			end tell
		end if
	end if
end run



on MailSelnAsMd()
	tell application "Mail"
		activate
		set lstText to {}
		repeat with refMsg in (selection as list)
			tell (contents of refMsg)
				set strLine to "- [" & sender & "]() [" & subject
				set end of lstText to strLine & "](message://%3c" & message id & "%3e)"
			end tell
		end repeat
		set {dlm, my text item delimiters} to {my text item delimiters, linefeed}
		set strTxt to lstText as rich text
		set my text item delimiters to dlm
		return strTxt
	end tell
end MailSelnAsMd


#12

This does what I was hoping, and I’m looking forward to stepping through and learning from it.

Nice touch, pushing the email address as a separate link.

Many, many thanks!


#13

Quick update on this one: I’ve noticed that some of the links created don’t open. I don’t think there’s a problem with the links as such— if I copy an offending “message://…” link into a browser, the link opens up the appropriate email, but FoldingText refuses to open it. Haven’t yet been able to figure out the difference between links which open and those that don’t, and it may not be specifically to do with FoldingText at all. Anyone have any ideas?


#14

Might be worth asking Jesse – I seem to remember that a fix for some mail address patterns may be coming in a later build.


#15

Can you give an example of a breaking link?


#16

Sent via PM. I’m not versed in the structure of such links, but the one thing I instantly notice that’s different between this link + some others that don’t work and others that do is the presence of underscores…


#17

Not sure why it took me so long to test this properly, but it does seem that FT doesn’t like links containing underscores. Doesn’t matter whether it’s a link to an email address or a regular web URL. Does this affect anyone else, or is it just me?


#18

@complexpoint — what’s the best way to edit the script to url encode ‘message id’? I’m trying to fix the way that underscores break links to mail. I’ve also noticed that some message ids contain ‘%’ symbols, which also break linking.

I tried to make use of a subroutine I found here: http://www.macosxautomation.com/applescript/sbrt/sbrt-08.html - but I’m missing something. I’ve pasted the “encode_text()” subroutine into this script and set my own variable equal to ‘message id’ but I’m missing something (probably very obvious!)— mail throws an error -1708, saying message id doesn’t understand the “encode_text” message.

What I tried to write:

set strLine to "- [" & sender & "]() [" & subject
set myMessageID to encode_text(message id, true, true)
set end of lstText to strLine & "](message://%3c" & myMessageID & "%3e)"

Thanks in advance for any suggestions, as always…


#19

I’m not sure, but I think a fix for that may be in the pipeline …


#20

Very cool. I can wait.