So today's date would be 19/1/2018. I want to make a macro that would increment or decrement this date by 1 day. So turn 19/1/2018 to 20/1/2018 or 18/1/2018 if decremented. Furthermore it should also be able to increment/decrement paying attention to the months. So going from 31/1/2018 and incrementing it would give 1/2/2018.
I need to use this to decrement and increment dates in Trello date picker similar to how Fantastical does it. This is the date picker:
I searched on this forum and found this Perl script that @peternlewis once shared here:
perl -e 'print ++$ENV{KMVAR_Count}'
But I can't seem to find a solution that would work for such a case. I can try to write a solution to this in Python but wondering if there is a better way to solve this.
I am sure I am mishandling the JS script. It doesn't actually return me the modified date and I am not sure how to change it as I am not very familiar with JS.
I capture the string from the Trello view. I also tested it by passing the actual date instead of System Clipboard and it fails too. In Trello it actually erases what was written and doesn't insert anything.
AppleScript version of an adjustedDateString function
-- adjustedDateString :: Int -> String -> String
on adjustedDateString(intDays, strSlashedDayMonthYear)
set {d, m, y} to splitOn("/", strSlashedDayMonthYear)
set dteIn to current date
tell (dteIn)
set its year to y
set its month to m
set its day to d
end tell
tell (dteIn + (intDays * days))
my intercalate("/", {its day as string, its month as integer, its year})
end tell
end adjustedDateString
on run
tell application "Keyboard Maestro Engine"
set strDelta to getvariable "testDelta"
set strDate to getvariable "testBaseDate"
end tell
set intDelta to strDelta as integer
adjustedDateString(intDelta, strDate)
end run
-- GENERIC FUNCTIONS --------------------------------------
-- concat :: [[a]] -> [a]
-- concat :: [String] -> String
on concat(xs)
if length of xs > 0 and class of (item 1 of xs) is string then
set acc to ""
else
set acc to {}
end if
repeat with i from 1 to length of xs
set acc to acc & item i of xs
end repeat
acc
end concat
-- intercalate :: [a] -> [[a]] -> [a]
-- intercalate :: String -> [String] -> String
on intercalate(sep, xs)
concat(intersperse(sep, xs))
end intercalate
-- intersperse(0, [1,2,3]) -> [1, 0, 2, 0, 3]
-- intersperse :: Char -> String -> String
-- intersperse :: a -> [a] -> [a]
on intersperse(sep, xs)
set lng to length of xs
if lng > 1 then
set acc to {item 1 of xs}
repeat with i from 2 to lng
set acc to acc & {sep, item i of xs}
end repeat
if class of xs is string then
concat(acc)
else
acc
end if
else
xs
end if
end intersperse
-- splitOn :: String -> String -> [String]
on splitOn(strDelim, strMain)
set {dlm, my text item delimiters} to {my text item delimiters, strDelim}
set xs to text items of strMain
set my text item delimiters to dlm
return xs
end splitOn
Also as I was playing with your scripts I realise Trello is not so dumb after all. If you just insert a number like 23 it would assume the date to be of 23rd of current month. This is super handy as the above solution is not as fast as I would of hoped. I don’t think you can get it to be as fast as Fantastical has it with up and down arrows. Trello interface is a bit slow.
The Shell Script function date -v+1d +'%d-%m-%Y' will increment the date by 1 day (+1d), but you could decrement or even change the target to m or y to adjust the month or the year.
Additionally, the date function will allow you to arbitrarily adjust to, say, next Sunday by using date -v+Sunday +'%d-%m-%Y'
It's extremely versatile. Feed it the variables you need it to process and then save to the variables you need to continue your functions.
Thanks for sharing.
How would you change a date other than the current date?
Say, for example, I had a KM Variable named “MyDate” whose value is “2018-05-23”.
the shell script to manhandle particular dates on Mac are as follows:
date -j -f "%Y-%m-%d" -v+1d "2018-05-23" +"%Y-%m-%d"
This, as an example, will give you your example date incremented by 1 day (-v+1d)
If you plug properly formatted variables into the statement, you would end up with something like this:
date -j -f "%Y-%m-%d" -v+1d "$KMVAR_MyDate" +"%Y-%m-%d"
This assumes a static 1 day increment, though you could extend a variable to control that as well. Note that in a shell script you need to use the $KMVAR_MyVarName format to get it to parse.
Oh, and to clarify the unix, the -j lets the command know you’re supplying a date; the -f tells the command the format in which you’re supplying it; the -v sets the increment (look into this part. it’s wicked powerful); and the final date formatting code specifies the output format, which in your case is the same as the input format.