×

Craft eXtension: Append Block Link to Project

This Craft extension will append a link to the selected block to a project chosen in OmniFocus.

DOWNLOAD CRAFT EXTENSION

Append Block Link to Chosen Project
Recreates the structured Craft selection in OmniFocus as a project.

Here is the main script from the Craft eXtension main HTML file:

Main Script


<script> btn = document.getElementById("F35ECB19-ED91-48B6-9579-770EC3022D6F"); btn.addEventListener("click", async () => { try { // GET THE CURRENT PAGE result = await craft.editorApi.getSelection() if (result.status !== "success") { throw new Error(result.message) } else { var selectionData = result.data } // GET THE INFO FOR THE FIRST SELECTED BLOCK var spaceID = selectionData[0]["spaceId"] var blockID = selectionData[0]["id"] var craftData = {"spaceID":spaceID,"blockID":blockID} // FUNCTION TO BE EXECUTED BY OMNIFOCUS function appendLinkToProjectNote(passedData){ // CREATE FORM FOR GATHERING USER INPUT var inputForm = new Form() // CREATE TEXT FIELD var textField = new Form.Field.String( "textInput", "Search", null ) // CREATE MENU var popupMenu = new Form.Field.Option( "menuItem", "Results", [], [], null ) popupMenu.allowsNull = true popupMenu.nullOptionTitle = "0 items" // ADD THE FIELDS TO THE FORM inputForm.addField(textField) inputForm.addField(popupMenu) // PRESENT THE FORM TO THE USER var currentValue = null formPrompt = "Enter a project title:" formPromise = inputForm.show(formPrompt,"Continue") // VALIDATE THE USER INPUT inputForm.validate = function(formObject){ var textInput = formObject.values["textInput"] if(textInput !== currentValue){ currentValue = textInput // remove popup menu if (inputForm.fields.length === 2){ inputForm.removeField(inputForm.fields[1]) } } if(inputForm.fields.length === 1){ // search using provided string if (!textInput){var searchResults = []} else { var searchResults = projectsMatching(textInput) } var searchResultNames = searchResults.map(item => item.name) var searchResultIDs = searchResults.map(item => item.id.primaryKey) var popupMenu = new Form.Field.Option( "menuItem", "Results", searchResultIDs, searchResultNames, null ) popupMenu.allowsNull = true popupMenu.nullOptionTitle = String(searchResults.length + " items") inputForm.addField(popupMenu) return false } if(!textInput){return false} if(inputForm.fields.length === 2){ menuValue = formObject.values["menuItem"] if(menuValue === undefined || String(menuValue) === "null"){ return false } return true } } // PROCESSING USING THE DATA EXTRACTED FROM THE FORM formPromise.then(function(formObject){ var projectID = formObject.values["menuItem"] var projectObj = Project.byIdentifier(projectID) blockID = passedData["blockID"] spaceID = passedData["spaceID"] backLink = `craftdocs://open?blockId=${blockID}&spaceId=${spaceID}` if (projectObj.note.length === 0){ var noteString = backLink } else { var noteString = `\n${backLink}` } projectObj.appendStringToNote(noteString) URL.fromString(`omnifocus:///task/${projectID}`).open() }) } // CREATE FUNCTION AND CONTENT STRINGS contentString = JSON.stringify(craftData) encodedContent = encodeURIComponent(contentString) functionString = appendLinkToProjectNote.toString() encodedFunction = encodeURIComponent(functionString) // CREATE SCRIPT URL url = 'omnifocus://localhost/omnijs-run?script=' + '%28' + encodedFunction + '%29' + '%28' + 'argument' + '%29' + '&arg=' + encodedContent // EXECUTE THE URL await craft.editorApi.openURL(url) } catch(err){ throw `${err.name}\n${err.message}` } }); </script>