Last active
December 16, 2015 10:09
-
-
Save ROldford/5418445 to your computer and use it in GitHub Desktop.
A TaskPaper script that can be used to increment or decrement datecodes in any desired way (any tag, either direction, any number, any time unit of days or bigger). Go to http://www.hogbaysoftware.com/wiki/UniversalDatecodeChanger for more information.
This file contains 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
(* | |
Universal Datecode Changer script | |
by Ryan Oldford | |
handlers taken from andyferra (see below for full credits) | |
This script is used with TaskPaper to increment or decrement datecode-type values of tags. | |
The script settings can be edited to allow for incrementing or decrementing | |
of any tag by any amount of time. | |
You can make multiple renamed versions of this script with different filenames | |
to allow for different ways of changing a datecode. | |
Settings: | |
tagName - the name of the tag whose datecode should be changed | |
incrementDate - set to true to increment dates, set to false to decrement dates | |
changeAmount - how many units should the date change by | |
changeUnit - the unit of change, must be one of the following exactly: | |
- days | |
- weeks | |
- months | |
- years | |
*) | |
-- START - Settings properties | |
property tagName : "due" | |
property incrementDate : true | |
property changeAmount : 1 | |
property changeUnit : days | |
-- END | |
-- START - Initialize important variables | |
-- END | |
tell application "TaskPaper" | |
tell selected entry | |
-- START - Check if tag exists in selected entry | |
set theTags to every tag | |
set wantedTag to null | |
repeat with aTag in theTags | |
if (name of aTag is tagName) then | |
set wantedTag to aTag | |
exit repeat | |
end if | |
end repeat | |
-- END | |
if wantedTag is not null then | |
-- START - Get tag value | |
set wantedTagValue to (value of wantedTag) | |
-- END | |
-- START - Check if it's a datecode, convert to date object | |
set tagDate to my datecodeToDate(wantedTagValue) | |
-- END | |
if tagDate is not null then | |
-- START - Change date object's value | |
if incrementDate is true then | |
set tagDate to tagDate + changeAmount * changeUnit | |
else | |
set tagDate to tagDate - changeAmount * changeUnit | |
end if | |
-- END | |
-- START - Change date back into datecode | |
set newDatecode to my dateToDatecode(tagDate) | |
-- END | |
-- START - Update tag's datecode | |
set value of wantedTag to newDatecode | |
-- END | |
end if | |
end if | |
end tell | |
end tell | |
on datecodeToDate(dateText) | |
-- handler to turn datecodes (YYYY-MM-DD format) to Applescript dates | |
-- returns null if datecode can't be turned itno a valid date | |
-- original handler (getDateFromText) by andyferra | |
-- https://gist.github.com/andyferra/64842 | |
-- slightly modified to respect text item delimiters and deal with coersion problems properly | |
set vDate to null | |
set oldASTIDs to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to {"-"} | |
if (count of text item of dateText) is 3 then | |
try | |
set vYear to text item 1 of dateText | |
set vMonth to text item 2 of dateText | |
set vDay to text item 3 of dateText | |
on error | |
return null | |
end try | |
if (vYear > 1000) and (vMonth > 0 and vMonth < 13) and (vDay > 0 and vDay < 32) then | |
set vDate to current date | |
set year of vDate to (vYear as integer) | |
set month of vDate to vMonth as integer | |
set day of vDate to vDay as integer | |
set time of vDate to 0 | |
end if | |
end if | |
set AppleScript's text item delimiters to oldASTIDs | |
return vDate | |
end datecodeToDate | |
on dateToDatecode(vDate) | |
-- handler to turn Applescript dates into datecodes (YYYY-MM-DD format) | |
-- original handler (getTextFromDate) by andyferra | |
-- https://gist.github.com/andyferra/64842 | |
set dText to ((year of vDate) as text) & "-" | |
set dayText to (month of vDate as number) as text | |
if length of dayText is 1 then | |
set dayText to "0" & dayText | |
end if | |
set dText to dText & dayText & "-" | |
set dayText to (day of vDate as number) as text | |
if length of dayText is 1 then | |
set dayText to "0" & dayText | |
end if | |
return dText & dayText | |
end dateToDatecode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment