Difficulty saving Apple result into KM as text

Hiya,

  1. How can I set an IF Statement where the result was more than X number of days ago (say 60 days ago) to start using Logic?

The name of the KM Variable: "form create date"
The result stored in the variable above: "Tuesday, 16 March 2021 at 12:31:08"

If KM Variable "form create date" value is more than 60 days ago (from today) then perform X KM Actions.

Thanks for the help.
Ali

What else is your AppleScript doing? You can get both file creation/modification dates and the current date/time in KM, so you may not need the AppleScript step at all!

One question to ask yourself though is "What is '60 days ago'?" Is that 60 complete days, or 60 days starting from this time in the day? The answer will determine how you do your calculation -- and, possibly, where it would be easiest to do.

Hi Nige,

I am grabbing the creation date of something (Form) and if that date is more than 60 days old I then want to perform Actions in KM.

How do I set an if statement for the result?
If this result is more than 60 days ago from 'today'

"Tuesday, 16 March 2021 at 12:31:08"

Does anyone know how to configure the KM IF action accordingly?

`tell application "Keyboard Maestro Engine"
set formID to getvariable "SingleLine"
end tell

tell application "Daylite"
set formCreateDate to eval "
formID := objectContext objectForEntityNamed:'CustomRecordSet' wherePrimaryKeyValueIs:" & formID & ".
clientNameField := formID createDate.
clientNameField.
"
end tell

tell application "Keyboard Maestro Engine"
setvariable "form Create Date" to formCreateDate as text
end tell`

could be complete day or half day it's doesn't matter how you calculate as long as it is X number of days ago. Either calculation is fine.

Thanks

Assuming Daylite is returning an AppleScript "date" object and you don't need the actual date later in your macro, a quick way to return values based on whether "formCreateDate" is more than 60 days before the start of "today" is:

if ((date (date string of (get current date))) - formCreateDate) / days > 60 then
	return 1
else
	return 0
end if

...with the "Execute AppleScript" action set to "save results to variable" and "Save to variable" pointing at your "form Create Date" (you might want to rename that to more obviously reflect its purpose, eg "isMoreThan60DaysAgo") -- you don't need the last "tell block in your AppleScript.

If you want to use the current time of "today" it's even simpler:

if (get (current date) - theDate) / days > 60 then
	return 1
else
	return 0
end if

You can then use "if 'form Create Date' is 1" for your ">60 days old" test.

If you do want to use the returned date in KM -- maybe as part of a file name -- you might be better off returning the date as a text string that you can then manipulate in KM.

1 Like

Looks like @Nige_S is way ahead of me. I tried to get some of the way there using KM actions, but got stuck.

I started out by setting today's date and the date 60 days ago to variables. I wasn't sure where I'd go with that, so I've disabled those actions for now.

So the first enabled action is the variable date being set. After that, I figured out how to isolate "16 March 2021" using Regex, and then split the day, month and year into variables using the Split Text plugin.

Then I tried to copy the logic from this post, which I think is meant to give you the number of days, but no luck.

Then I figured it was just a case of using that number in an if/else.

So, I don't know if I'm close or not, but hey, I tried!

If Older Than 60 Days....kmmacros (22 KB)

Macro screenshot

1 Like

Without trying it, I think the easiest way to do it in KM would be to return the AppleScript-derived date as "seconds since epoch" -- and the quick way to do that in AS is with eg:

set epochStart to date "Thursday, 1 January 1970 at 00:00:00"
set anyDate to (get current date)
return anyDate - epochStart

You could then do direct calculations in KM, use KM's date and time tokens, etc.

2 Likes

@Nige_S has a good answer, basically this:

tell application "Keyboard Maestro Engine"
	set ds to getvariable "form create date"
end tell
set d to date ds
((current date) - d) / 24 / 3600 > 60
2 Likes

Thank you @peternlewis and @Nige_S for providing two great methods to solving my query.