How do I define a link that points to a particular location in an arbitrary FoldingText document? For example, if I have a document stored in iCloud called “mystuff.ft”, how would I create a link that would open to a specific location in that document?
I don’t think that the url scheme yet supports links to particular documents or locations within them, but in the meanwhile you can, of course, call an applescript (for example from the command line, directly or indirectly) passing a filename and a line number to it.
To open test.ft and automatically select line 12, I just ran this from a shell script:
property pjsSelectLine : "
function(editor, options) {
var tree=editor.tree(),
lngLine=options.linenum,
oNode=tree.lineNumberToNode(lngLine),
rngSeln=tree.createRangeFromLocation(oNode.lineTextStart(), oNode.line().length);
//debugger;
tree.ensureClassified();
editor.scrollToLine(lngLine);
editor.setSelectedRange(rngSeln);
return true;
}
"
on run argv
if class of argv = list then
OpenFile(item 1 of argv)
GotoLine(item 2 of argv)
end if
end run
on OpenFile(strUnixPath)
tell application "FoldingText"
activate
set varOpened to open strUnixPath
end tell
end OpenFile
on GotoLine(varLine)
tell application "FoldingText"
activate
tell front document
return evaluate script pjsSelectLine with options {linenum:varLine as integer}
end tell
end tell
end GotoLine
How does the ftdoc:// scheme react to changed documents? E.g., can I link to a h2 header and always return to that header, even if the line number of the header has changed?
FoldingText lines don’t have ids that persist between sessions, but you could use an ftdoc:// link with just a filepath and a ?nodepath= switch to focus down to a particular heading. (dropping the ?line= switch)
(e.g. with a nodepath like /*/name of header
or perhaps: //headername and @type=heading)
Alternatively, and this might be more helpful, I could add a feature which selected lines by nodepath match, rather than by line number, (as an alternative to using the nodepath for filtering).
I’ll need to find a moment to sit down with pencil and paper to decide what the new switch should be. Perhaps something like ?=selectnodepath
Copies a minimum unique nodepath for the selected line to ?selnpath=
copies the text of the line to ?find=
still records any ?line=N (with any selection offsets)
ftdoc:// now:
Opens the specified document, and
applies any ?nodepath= switch as a filter
tries to restore the selection from ?selnpath=, ?find= and ?line= in that order
The ?selnpath= minimal nodepath should be more resilient to text edits than a line number - it will continue to find a line as long as the broad nesting and heading titles are preserved.
?find= should be a useful fall-back
All of these switches are optional, and can be used on their own.
(the offset switches are ignored unless there is a ?line= switch )
The ?line= switch may, at first sight, seem a puzzling inclusion, given the short shelf-life of line numbers, but it turns out to be useful when generating quick overview perspectives across several files (with the FT Command line interface), and using them to click straight back to particular files and lines before further editing.
Wow, this is amazing. Jesse should put you on payroll! I can’t wait to try this out. Thanks so much for sharing your work here. I’m not sure my technical skills would be up to the task of figuring this out on my own–I’m still not sure how to work with the command line / node path interface.
Jesse: I’m hoping this same approach will work for the next update to Taskpaper, since it is still presumably built from similar internals?
Simply amazing. Thanks again for sharing your work! Your plugins, and Jamie’s Filter plugins, have made FoldingText one of my most used, and useful, apps.
I added this to my Alfred Workflow for FoldingText. It is great. Now, I have full bookmarking in Alfred for FoldingText!! Really neat. Thanks for the ideas!!!