VCOF0102
OmniFocus: New «Nth» «Weekday» Task
This command creates a new task based upon the requested relative weekday of the current month, i.e. Third Tuesday, Second Wednesday, Last Friday, etc.
NOTE: If the requested weekday has already occurred, it assumed to be for the next month.
NOTE: The default due time (set in OmniFocus preferences) is used for the due time of the created task.
Scope and Syntax
Language: English (United States 🇺🇸 · Great Britain 🇬🇧 · Australia 🇦🇺 · Canada 🇨🇦) • Scope: System-Wide
Command phrase variations (words in [brackets] are optional):
New «Nth» «Weekday» Task
- “New First Monday Task”
- “New Second Monday Task”
- “New Third Monday Task”
- “New Fourth Monday Task”
- “New Last Monday Task”
- “New Final Monday Task”
- “New First Tuesday Task”
- “New Second Tuesday Task”
- “New Third Tuesday Task”
- “New Fourth Tuesday Task”
- “New Last Tuesday Task”
- “New Final Tuesday Task”
- etc.
Requirements
- Application: OmniFocus 4
- System: macOS 12 (Not available on iOS/iPadOS because installation would require 35 separate installation components — one per relative weekday request)
Video: New Nth Weekday Task |
Voice commands create a new task based upon the requested relative weekday of the current month, i.e. Third Tuesday, Second Wednesday, Last Friday |
|
Downloads
- ⇩ COMMANDS-FILE OmniFocus 4.0 (macOS)
Script Code
Here is the script code:
New «Nth» «Weekday» Task
(async () => {
try {
function createUtterance(textToSpeak){
langCode = Speech.Voice.currentLanguageCode
voiceObj = Speech.Voice.withLanguage(langCode)
utterance = new Speech.Utterance(textToSpeak)
utterance.voice = voiceObj
utterance.rate = Speech.Utterance.defaultSpeechRate
return utterance
}
synthesizer = new Speech.Synthesizer()
function weekdayOccurrencesInMonth(weekdayIndex, monthIndex, yearIndex){
cal = Calendar.current
dc = new DateComponents()
dc.day = 1
dc.month = monthIndex
dc.year = yearIndex
monthLength = new Date(yearIndex, monthIndex, 0).getDate()
matchedDates = new Array()
for (var i = 1; i < (monthLength + 1); i++) {
dc.day = i
var d = cal.dateFromDateComponents(dc)
if (d.getDay() === weekdayIndex){matchedDates.push(d)}
}
return matchedDates
}
// FUNCTION FOR ORDINAL STRINGS: 1st, 2nd, 3rd, 4th...
function ordinal(n) {
var s = ["th", "st", "nd", "rd"];
var v = n%100;
return n + (s[(v-20)%10] || s[v] || s[0]);
}
// DECLARE TARGETS
var weekdayIndicator = 1 // Sunday = 0, Monday = 1
var occurenceIndicator = 1 // 10 = last, 1 = 1st, 2 = 2nd, 3 = 3rd
var occurenceIndex = occurenceIndicator - 1
// GET CURRENT DATE COMPONENTS
var cal = Calendar.current
var now = new Date()
var today = cal.startOfDay(now)
var todayDC = cal.dateComponentsFromDate(today)
var monthIndex = todayDC.month
var dayIndex = todayDC.day
var yearIndex = todayDC.year
var currentLocale = Calendar.current.locale
var currentMonthTitle = today.toLocaleString(currentLocale, {month:"long"})
// GET ARRAY OF WEEKDAYS IN CURRENT MONTH
var dateObjs = weekdayOccurrencesInMonth(weekdayIndicator, monthIndex, yearIndex)
if (occurenceIndicator === 10){
var targetDate = dateObjs[dateObjs.length - 1]
} else if (occurenceIndicator > dateObjs.length){
monthTitle = dateObjs[0].toLocaleString(currentLocale, {month:"long"})
weekdayTitle = dateObjs[0].toLocaleString(currentLocale, {weekday:"long"})
throw {
name: "Request Error",
message: `The month of ${monthTitle} does not contain ${occurenceIndicator} instances of ${weekdayTitle}`
}
} else {
var targetDate = dateObjs[occurenceIndex]
console.log("Requested Date", targetDate)
}
// CHECK IF REQUEST DATE HAS ALREADY PASSED THIS MONTH
var datePassed = false
if(today > targetDate){
var datePassed = true
todayDC.month = todayDC.month + 1
futureDate = cal.dateFromDateComponents(todayDC)
dc = cal.dateComponentsFromDate(futureDate)
monthIndex = dc.month
yearIndex = dc.year
dateObjs = weekdayOccurrencesInMonth(weekdayIndicator, monthIndex, yearIndex)
if (occurenceIndicator === 10){
var targetDate = dateObjs[dateObjs.length - 1]
} else if (occurenceIndicator > dateObjs.length){
monthTitle = dateObjs[0].toLocaleString(currentLocale, {month:"long"})
weekdayTitle = dateObjs[0].toLocaleString(currentLocale, {weekday:"long"})
throw {
name: "Request Error",
message: `The month of ${monthTitle} does not contain ${occurenceIndicator} instances of ${weekdayTitle}`
}
} else {
var targetDate = dateObjs[occurenceIndex]
}
}
console.log("Target Date", targetDate)
// DEFAULT DUE TIME
defaultDueTime = settings.defaultObjectForKey('DefaultDueTime')
timeElements = defaultDueTime.split(":")
dc = cal.dateComponentsFromDate(targetDate)
dc.hour = parseInt(timeElements[0])
dc.minute = parseInt(timeElements[1])
var targetDateTime = cal.dateFromDateComponents(dc)
dateString = Formatter.Date.withFormat('LLLL d, @ h:mma').stringFromDate(targetDateTime)
positionalIndicator = (occurenceIndicator === 10) ? "Last":ordinal(occurenceIndicator)
weekdayTitle = targetDate.toLocaleString(currentLocale, {weekday:"long"})
taskTitle = `${positionalIndicator} ${weekdayTitle} Task`
task = new Task(taskTitle)
task.dueDate = targetDateTime
taskID = task.id.primaryKey
URL.fromString(`omnifocus:///task/${taskID}`).open()
if(datePassed){
var responsePrefix = `Since ${positionalIndicator} ${weekdayTitle} for ${currentMonthTitle} has already passed, `
} else {
var responsePrefix = ""
}
responseString = `${responsePrefix}Task added for ${positionalIndicator} ${weekdayTitle}, ${dateString}`
utterance = createUtterance(responseString)
synthesizer.speakUtterance(utterance)
}
catch(err){
utterance = createUtterance(err.message)
synthesizer.speakUtterance(utterance)
//new Alert(err.name, err.message).show()
}
})();
LEGAL
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. OMNI-AUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. OMNI-AUTOMATION.COM provides this only as a convenience to our users. OMNI-AUTOMATION.COM has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and OMNI-AUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from OMNI-AUTOMATION.COM and that OMNI-AUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.