Plug-In: Transform Links
(IMPORTANT: This plug-in requires OmniFocus 4.x)
A pair of plug-ins for transforming links in the note of the selected project or task:
- Replace Markdown Links • This plug-in will replace all occurrences of links in markdown format [Link-Title](Link-URL-String) with link objects.
- Replace Links with Markdown • This plug-in converts all occurrences of link objects into text in markdown link format: [Link-Title](Link-URL-String)
NOTE: The current versions of the plug-ins have been updated to fix an erroneous application version check.
Video: Transform Links |
A pair of plug-ins for transforming links in the note of the selected project or task. NOTE: In the video, the plug-ins have been assigned keyboard shortcuts. |
|
Return to: OmniFocus Plug-In Collection
Replace Markdown Links
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Otto Automator",
"identifier": "com.omni-automation.of.note-replace-markdown-links",
"version": "1.6",
"description": "This plug-in will replace all occurrences of links in markdown format […](…) with link objects, in the note of the selected project or task.",
"label": "Replace Markdown Links",
"shortLabel": "Replace Markdown Links",
"paletteLabel": "Replace Markdown Links",
"image": "link.circle.fill"
}*/
(() => {
var action = new PlugIn.Action(function(selection, sender){
// action code
// selection options: tasks, projects, folders, tags, databaseObjects, allObjects
var item = selection.databaseObjects[0]
// reveal note
tree = document.windows[0].content
tree.nodeForObject(item).expandNote(false)
// identify objects
noteObj = item.noteText
regexMdLinks = /!?\[([^\]]*)\]\(([^\)]+)\)/
strToMatch = regexMdLinks.source
findParams = [Text.FindOption.RegularExpression]
resultRange = noteObj.range
while (resultRange !== null) {
rangeToSearch = noteObj.range
resultRange = noteObj.find(regexMdLinks.source, findParams, rangeToSearch)
if(resultRange){
mdLinkStr = noteObj.textInRange(resultRange).string
x = mdLinkStr.indexOf("]")
y = mdLinkStr.indexOf("(")
mdLinkTitle = mdLinkStr.substr(1, x - 1)
mdLinkURLStr = mdLinkStr.substr(y + 1, mdLinkStr.length)
mdLinkURLStr = mdLinkURLStr.slice(0, -1)
linkURL = URL.fromString(mdLinkURLStr)
if (linkURL){
currentStyle = noteObj.styleForRange(resultRange)
linkObj = new Text(mdLinkTitle, currentStyle)
style = linkObj.styleForRange(linkObj.range)
style.set(Style.Attribute.Link, linkURL)
noteObj.replace(resultRange, linkObj)
} else {console.error("Bad URL:", mdLinkURLStr)}
}
}
});
action.validate = function(selection, sender){
// validation code
// selection options: tasks, projects, folders, tags, databaseObjects, allObjects
return (
selection.databaseObjects.length === 1 &&
selection.tasks.length === 1 ||
selection.projects.length === 1
)
};
return action;
})();
IMPORTANT: Do no run the following plug-in (“Replace Link Objects with Markdown”) if the note currently contains links in markdown format: [LINK NAME](LINK URL). Instead, first convert the “markdown links” to “link objects” using the “Replace Markdown Links” plug-in, then run the the following plug-in (“Replace Link Objects with Markdown”).
Replace Link Objects with Markdown
/*{
"type": "action",
"targets": ["omnifocus"],
"author": "Otto Automator",
"identifier": "com.omni-automation.of.note-replace-links- with-markdown",
"version": "1.3",
"description": "This plug-in will replace, in the note of the selected project or task, all occurrences of links with markdown links: [Link Title](Link URL)",
"label": "Replace Links wth Markdown",
"shortLabel": "Replace Links",
"paletteLabel": "Replace Links",
"image": "link.circle"
}*/
(() => {
var action = new PlugIn.Action(function(selection, sender){
// action code
// selection options: tasks, projects, folders, tags, allObjects, databaseObjects
var item = selection.databaseObjects[0]
// reveal note
tree = document.windows[0].content
tree.nodeForObject(item).expandNote(false)
// identify objects
noteObj = item.noteText
runRanges = noteObj.ranges(TextComponent.AttributeRuns)
runRanges.reverse().forEach(range => {
runStyle = noteObj.styleForRange(range)
linkStr = runStyle.get(Style.Attribute.Link).string
if(linkStr.length > 0){
linkTitle = noteObj.textInRange(range).string
if(linkTitle === linkStr){linkTitle = "LINK"}
mdLinkStr = `[${linkTitle}](${linkStr})`
linkObj = new Text(mdLinkStr, noteObj.style)
noteObj.replace(range, linkObj)
}
})
});
action.validate = function(selection, sender){
// validation code
// selection options: tasks, projects, folders, tags, allObjects
return (selection.tasks.length === 1 || selection.projects.length === 1 )
};
return action;
})();