Jump to content

Pearcen

Member
  • Content Count

    2
  • Joined

  • Last visited

  1. I've had a play with the workflow AppleScript and think I have added some logic to help it deal with times. it should now accept times in the formats: <hour> am/pm or 24hr time. eg: 8 am. 3:45 pm, 2:39 am, 05:00 am 00:02, 13:56, 15:00, 23:49 This is my first attempt at AppleScript so hopefully it works and is useful to add to this already great workflow Code replaces all in the RunNSAppleScript block (* For using in Script Editor *) --alfred_script("xxx;;14;Wishlist") on alfred_script(q) set noDate to "no" set theDayArray to {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} (* Explode query input *) set myList to explode(";", q) (* get the Title of the Reminder *) set theReminder to item 1 of myList (* try to get Date, tomorrow or next week on error set to "none" *) set queryDay to item 2 of myList if queryDay is "" then set queryDay to "none" (* Replace . with : <OLD:Set default Reminder Time 8:00> *) set queryTime to item 3 of myList -- if queryTime is "" then set queryTime to "8:00" set queryTime to replace_chars(queryTime, ".", ":") (* Get List of for Reminder *) set queryList to trim(item 4 of myList) (* try to get entered time. if only hours are provided at the missing ":00" *) if queryTime is not "" and queryTime does not contain ":" then set Pos to offset of " " in queryTime set theHour to characters 1 thru (Pos - 1) of queryTime as string set queryTime to theHour & ":00" & characters (Pos) through end of queryTime as string else if (queryTime does not contain "am") and (queryTime does not contain "pm") then set Pos to offset of ":" in queryTime set theHour to characters 1 thru (Pos - 1) of queryTime as string set theHour to returnNumbersInString(theHour) set theHour to theHour as number set queryTime to characters (Pos + 1) through end of queryTime as string -- Get the "minute" set Pos to offset of ":" in queryTime set theMin to characters 1 thru (Pos - 1) of queryTime as string set queryTime to characters (Pos + 1) through end of queryTime as string --Get "AM or PM" set Pos to offset of " " in queryTime set theSfx to characters (Pos + 1) through end of queryTime as string if theSfx does not contain "pm" and theSfx does not contain "am" then if (theHour is less than 24) or (theHour is greater than or equal to 0) then if theHour is less than 12 then set theSfx to "am" else set theSfx to "pm" set theHour to theHour - 12 end if end if end if set queryTime to theHour & ":" & theMin & " " & theSfx as string end if (* calculate due date when "today" was entered. If time string is emptpy, add 3 hours from now *) if queryDay contains "today" then if queryTime is not equal to "" then set theDate to date string of (current date) set DueDate to date (theDate & " " & queryTime) else set DueDate to ((current date) + (3 * hours)) end if (* calculate tomorrow's due date. If time string is empty get current time *) else if queryDay contains "tomorrow" then if queryTime is equal to "" then set queryTime to time string of (current date) end if set theDate to date string of ((current date) + (1 * days)) set DueDate to date (theDate & " " & queryTime) (* Calculate next weeks due date. If time string is empty get current time *) else if queryDay contains "next week" then if queryTime is equal to "" then set queryTime to time string of (current date) end if set theDate to date string of ((current date) + (1 * weeks)) set DueDate to date (theDate & " " & queryTime) (* if weekday was provided set due date to the next weekday*) else if theDayArray contains queryDay then set theNext to list_position(queryDay, theDayArray) set theNextWeekday to date string of (nextWeekday(theNext)) set DueDate to date (theNextWeekday & " " & queryTime) (* if no day was provided set noDate flag to "yes" *) else if queryDay contains "none" then set noDate to "yes" (* In case date where provided check if only DD and MM was provided and add current year *) else if queryDay contains "." then if queryTime is equal to "" then set queryTime to time string of (current date) end if set expDate to explode(".", queryDay) if (count of expDate) is equal to 2 then set theYear to year of (current date) set queryDay to queryDay & "." & theYear end if set DueDate to date (queryDay & " " & queryTime) end if (* Create reminder without a DueDate *) if noDate is equal to "yes" then tell application "Reminders" -- get default list set in Reminders if queryList is equal to "" then set listName to name of default list else set listName to queryList end if set availList to name of lists if availList does not contain listName then set listName to name of default list end if tell list listName make new reminder with properties {name:theReminder} end tell end tell set output to theReminder & ", in List: " & listName (* Create reminder with DueDate *) else tell application "Reminders" -- get default list set in Reminders if queryList is equal to "" then set listName to name of default list else set listName to queryList end if set availList to name of lists if availList does not contain listName then set listName to name of default list end if tell list listName make new reminder with properties {name:theReminder, remind me date:DueDate} end tell end tell set output to theReminder & " Due: " & DueDate & ", in List: " & listName end if end alfred_script (* explode string with given delimter *) on explode(delimiter, input) -- explode(delimiter (String),input (String)) (Array) local delimiter, input, ASTID set ASTID to AppleScript's text item delimiters try -- save delimiters to restore old settings set oldDelimiters to AppleScript's text item delimiters set AppleScript's text item delimiters to delimiter set input to text items of input set AppleScript's text item delimiters to ASTID set AppleScript's text item delimiters to oldDelimiters return input --> list on error eMsg number eNum set oldDelimiters to AppleScript's text item delimiters set AppleScript's text item delimiters to ASTID set AppleScript's text item delimiters to oldDelimiters error "Can't explode: " & eMsg number eNum end try end explode (* calculates next weekday as an integer *) on nextWeekday(wd) set today to current date set twd to weekday of today if twd is wd then set d to 7 else set d to (7 + wd - twd) mod 7 end if return today + (d * days) end nextWeekday (* returns position in a list *) on list_position(this_item, this_list) repeat with i from 1 to the count of this_list if item i of this_list is this_item then return i end repeat return 0 end list_position (*Replaces a character witin a text*) on replace_chars(this_text, search_string, replacement_string) -- (string,char,char) set AppleScript's text item delimiters to the search_string set the item_list to every text item of this_text set AppleScript's text item delimiters to the replacement_string set this_text to the item_list as string set AppleScript's text item delimiters to "" return this_text end replace_chars on returnNumbersInString(inputString) set s to quoted form of inputString do shell script "sed s/[a-zA-Z\\']//g <<< " & s set dx to the result set numlist to {} repeat with i from 1 to count of words in dx set this_item to word i of dx try set this_item to this_item as number set the end of numlist to this_item end try end repeat return numlist end returnNumbersInString (* ======================================================= FUNTION: Trim trailing whitespaces from a string RETURNS: String ======================================================== *) on trim(str) set cmd to "echo \"" & str & "\" | sed 's/ //g'" set trimmed to do shell script cmd return trimmed end trim
  2. From my testing it only likes times in the format hh:mm:ss am/pm so 7 am, 7:00, 07:00, 07:00:00, all fail but 07:00:00 am or 7:00:00 am works. [20:53:20.854] Apple Reminders[Arg and Vars] Passing output 'title;tomorrow;07:00:00;' to Run NSAppleScript [20:53:30.351] Apple Reminders[Run NSAppleScript] Processing complete [20:53:30.418] Apple Reminders[Run NSAppleScript] Passing output 'title Due: Thursday, 13 February 2020 at 12:00:00 am, in List: Reminders' to Post Notification [20:54:21.657] Apple Reminders[Arg and Vars] Passing output 'test;tomorrow;07:00:00 am;' to Run NSAppleScript [20:54:31.144] Apple Reminders[Run NSAppleScript] Processing complete [20:54:31.165] Apple Reminders[Run NSAppleScript] Passing output 'fart Due: Thursday, 13 February 2020 at 7:00:00 am, in List: Reminders' to Post Notification
×
×
  • Create New...