How to compare a variable (greater than) to another variable in maestro 7

I want to compare a variable ‘x’ to another variable ‘y’ to see if one is greater than the other.

I’ve started just trying to compare variable ‘x’ to the value 5, but cannot figure it.

I thought to use the IF / ELSE action, however this only has things like ‘exists’ ‘does not exist’ ‘isempty’ and not a way to go a greater than, or less than comparison.

How to do greater than (or equals) and less than (or equals) comparison?

Use the Calculation condition.

More generally, comparison is, of course, only defined for particular types.

  • Numeric comparison is probably the simplest to define, and usually available directly in the form of some operators, but numeric strings don’t always sort to the same sequence as numeric values – their length may differ, and they may have leading "0"s
  • String comparison involves decisions both about case-sensitivity and the locale-specific mapping of diacritics,
  • Record comparison requires a function that specifies first a field or sequence of fields, and then any specifics for the type of each field
  • Boolean comparison may not be defined – is truth more or less than falsehood ?

in short, sometimes you may need to use a script action to get the form of comparison that works for your purpose

PS In their raw form all KM variables (even things that look like numbers) are stored as strings. The calculation condition reads a numeric string as a numeric value for you, so it can give you a numeric comparison rather than a string comparison:

Numerically 5 > 4, but as unconverted strings:

“5” > “400”

Is there a function or utility or smarter means to compare two text variables that are dates (e.g. 20-Jun-17 vs. 15-May-17 ) for which is greater than the other without comparing each part (i.e. date, month & year)?

AFAIK, nothing built-in. I think you're going to need to convert the date parts to numbers and then use to calculate TIME function (KM Wiki) for each date. Since TIME() returns a number, you can use it to compare.

JMichaelTX,

Yeah, ok.

Thanks

I happened to have a JXA script which already did most of the work, so here is a draft Macro example you can use to build on.

##example Results

##Macro Library   @Date Calculate Difference Between Dates @Example


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/7/74d34ac6f0ee7e805eb6e2f8b718ad93d976d5bd.kmmacros">@Date Calculate Difference Between Dates @Example.kmmacros</a> (4.9 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---


<img src="/uploads/default/original/2X/a/ab7bd01e0d4ce82e569d76e4c0a400e1b03135fb.png" width="553" height="1140">

---

###JXA Script
```javascript
'use strict';
(function run() {      // this will auto-run when script is executed

/*

PURPOSE: Calculate Diff Between Two Dates
VER: 1.0 2017-06-20
AUTHOR: @JMichaelTX

Seems to work with these date formats:
myDates.iso = createDate("2017-02-08");
myDates.us = createDate("2/08/2017");
myDates.us2 = createDate("Feb 8, 2017");
myDates.eu = createDate("8-Feb-17");

*/

var kmeApp = Application("Keyboard Maestro Engine");
var date1Str = kmeApp.getvariable('SCPT__Date1');
var date2Str = kmeApp.getvariable('SCPT__Date2');


var scriptResults = (createDate(date1Str) - createDate(date2Str))/(24*3600*1000);

return scriptResults;

//~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~

function createDate(pDateStr) {
  
  var dateFromStr = new Date(pDateStr);
  
  //--- ADD TIME ZONE OFFSET IF DATE STRING IS ISO FORMAT ---
  //    (like "2016-02-10")
  //    When a date object is created from a string in ISO format,
  //    JavaScript assumes you intend this to be in GMT time zone,
  //    and so subtracts the time zone offset for your locale.
  
  if (RegExp(/^\d{4}[\/\.-]\d\d[\/\.-]\d\d/).test(pDateStr)) {
    dateFromStr.setMinutes(dateFromStr.getMinutes() + dateFromStr.getTimezoneOffset());
    //console.log("converted ISO Date: " + pDateStr);
    }

  return dateFromStr;
}

function logObj(pObject) {
  for (key in pObject){
    console.log(key + ": " + pObject[key]);
  }
}

}  // END of function run()
)();

```