Last active
August 17, 2020 23:09
-
-
Save miklb/a79a05f303322b138562b048f434a236 to your computer and use it in GitHub Desktop.
First pass at an Omnifocus Automation task that takes a selection of tasks from the clipboard, formats them for NotePlan tasks, and sends them to the note for the date selected.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*{ | |
"author": "Michael Bishop", | |
"targets": ["omnifocus"], | |
"type": "action", | |
"identifier": "com.miklb.NotePlan Tasks", | |
"version": "0.2", | |
"description": "A plug-in that takes a selection of tasks and formats them for NotePlan, appending them to the current days note.", | |
"label": "NotePlan Tasks", | |
"mediumLabel": "NotePlan Tasks", | |
"paletteLabel": "NotePlan Tasks", | |
}*/ | |
(() => { | |
var action = new PlugIn.Action(function(selection) { | |
if (Pasteboard.general.hasStrings) { | |
Pasteboard.general.string = ( | |
Pasteboard.general.string | |
.split('\n') | |
.map( | |
p => 0 < p.length ? ( | |
'- [ ] ' + p | |
) : p | |
) | |
.join('\n') | |
); | |
var noteBody = Pasteboard.general.string; | |
var noteBody = encodeURIComponent(noteBody); | |
var locale = "en-us" | |
var monthNames = [] | |
for (i = 0; i < 12; i++) { | |
var objDate = new Date() | |
objDate.setDate(1) | |
objDate.setMonth(i) | |
monthName = objDate.toLocaleString(locale,{month:"long"}) | |
monthNames.push(monthName) | |
} | |
var now = new Date() | |
var currentYear = now.getFullYear() | |
var yearNames = new Array() | |
var yearIndexes = new Array() | |
for (i = 0; i < 4; i++) { | |
yearNames.push(String(currentYear + i)) | |
yearIndexes.push(i) | |
} | |
var dayCount = new Date(currentYear, 1, 0).getDate() | |
var dayIndexes = new Array() | |
var dayIndexStrings = new Array() | |
for (var i = 0; i < dayCount; i++){ | |
dayIndexes.push(i) | |
dayIndexStrings.push(String(i + 1)) | |
} | |
var inputForm = new Form() | |
var yearMenu = new Form.Field.Option( | |
"yearMenu", | |
"Year", | |
yearIndexes, | |
yearNames, | |
0 | |
) | |
var currentYearIndex = 0 | |
var monthMenu = new Form.Field.Option( | |
"monthMenu", | |
"Month", | |
[0,1,2,3,4,5,6,7,8,9,10,11], | |
monthNames, | |
0 | |
) | |
var currentMonthIndex = 0 | |
var dayMenu = new Form.Field.Option( | |
"dayMenu", | |
"Day", | |
dayIndexes, | |
dayIndexStrings, | |
0 | |
) | |
inputForm.addField(yearMenu) | |
inputForm.addField(monthMenu) | |
inputForm.addField(dayMenu) | |
var formPrompt = "Select the date:" | |
var buttonTitle = "OK" | |
var formPromise = inputForm.show(formPrompt, buttonTitle) | |
inputForm.validate = function(formObject){ | |
var yearMenuIndex = formObject.values["yearMenu"] | |
var chosenYear = parseInt(yearNames[yearMenuIndex]) | |
var monthMenuIndex = formObject.values["monthMenu"] | |
if(monthMenuIndex != currentMonthIndex || yearMenuIndex != currentYearIndex){ | |
inputForm.removeField(inputForm.fields[2]) | |
currentMonthIndex = monthMenuIndex | |
currentYearIndex = yearMenuIndex | |
} | |
if (formObject.fields.length == 2){ | |
var dayCount = new Date(chosenYear, monthMenuIndex + 1, 0).getDate() | |
var dayIndexes = new Array() | |
var dayIndexStrings = new Array() | |
for (var i = 0; i < dayCount; i++){ | |
dayIndexes.push(i) | |
dayIndexStrings.push(String(i + 1)) | |
} | |
var dayMenu = new Form.Field.Option( | |
"dayMenu", | |
"Day", | |
dayIndexes, | |
dayIndexStrings, | |
0 | |
) | |
inputForm.addField(dayMenu) | |
} | |
return true | |
} | |
formPromise.then(function(formObject){ | |
var yearMenuIndex = formObject.values["yearMenu"] | |
var yearName = yearNames[yearMenuIndex] | |
var monthMenuIndex = formObject.values["monthMenu"] | |
var monthName = monthNames[monthMenuIndex] | |
var dayMenuIndex = formObject.values["dayMenu"] | |
var dayIndexString = dayIndexStrings[dayMenuIndex] | |
var targetDate = new Date(monthName + " " + dayIndexString + " " + yearName) | |
var formateDate = Formatter.Date.withFormat("yyyyMMdd") | |
var noteplanDate = f.stringFromDate(targetDate) | |
console.log(noteplanDate); | |
console.log(noteBody); | |
URL.fromString('noteplan://x-callback-url/addText?noteDate=' + noteplanDate + '&text=' + noteBody + '&mode=append').open(); | |
}) | |
formPromise.catch(function(error){ | |
console.log("form cancelled", error.message) | |
}) | |
} | |
}); | |
// If needed, uncomment, and add a function that returns true if the current selection is appropriate for the action. | |
/* | |
action.validate = function(selection){ | |
}; | |
*/ | |
return action; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment