A Little Help Using “IF THEN” Action with a Time Variable

For example if %theTime% is after 1:30 PM, I’ve tried use “The Variable: theTime is > 1:30 PM” and “The Variable: theTime is after 1:30 PM”

Only attempted using the time format above but the
the format goal for thisTime varies a little. Examples: 1:30 PM, 01:30 PM, 13:30

If this helps, I get thisTime variable most often but not every time from a selected Mail Message via AppleScript getting the date received.

Thanks for any ideas or resolutions!

Hey Josh,

Keyboard Maestro doesn't grok natural language dates/times, so you can't compare that way.

You have to convert a date/time into a Unix Date (Numeric String).

My macro uses Apple's Data Detectors to convert any given time format into a date like so:

Start time	:	13:30
Becomes		:	2019-06-17 18:30:00 +0000 (date object)
Ends Up As	: 	1560776760 (Unix Date)

It then feeds the Unix Date String into a Keyboard Maestro variable, which you can compare to another date/time (converted into a Unix Date String).

It's a bit painful, but it can be done.

** Note that a bare time string will be considered to be TODAY at whatever time.

I think I've accounted for any pitfalls from using Data-Detectors with a bare time string, but no bets.

I don't mess with dates much in Keyboard Maestro, so I may have missed an easier method...

-Chris


Time Calculation Using AppleScript Data-Detectors v1.00.kmmacros (9.6 KB)

1 Like

Thanks! I will give that a shot today.

@JD_Faust, Chris @ccstone has given you a great solution using AppleScript , but I wanted to show how this might be done using non-scripting KM Actions.

Here is an example macro that you will need to modify to suit your needs, but should give you a good idea of how to approach this problem.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Date/Time Comparisions [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/4/5/45961fa211f03281a0d4dd85f30b7ebff008d90c.kmmacros">Date-Time Comparisions [Example].kmmacros</a> (19 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

Author.@JMichaelTX 

**PURPOSE:**

* **Show How to Compare Time String with Current Time**
   * Allow for multiple formats of time string

**HOW TO USE**

1. First, make sure you have followed instructions in the _Macro Setup_ below.
2. Trigger this macro.

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  ??
.
1. Assign a Trigger to this maro.
2. Move this macro to a Macro Group that is only Active when you need this Macro.
3. ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
   * Set Variable Local__MsgTimeStr 
     * Source time string from EMail or elsewhere

**REQUIRES:**

1. **KM 8.2+**
2. **macOS 10.11.6 (El Capitan)**

TAGS:  @Date @Time @RegEx @Example

USER SETTINGS:

* Any Action in _magenta color_ is designed to be changed by end-user

ACTION COLOR CODES

* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions


**==USE AT YOUR OWN RISK==**

* While I have given this a modest amout of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

---

![image|463x2000](upload://rQoK47nCWhJSlk5zBWDtRMBGGrD.png)

-------------------------------------------------------------------------------------------

Tags: »Example »Date »Time »RegEx

-------------------------------------------------------------------------------------------
1 Like

Just to help you take the next step, I've revised the Macro to get the time string from a selected Mail message, using this simple script:

tell application "Mail"
  set selectedMessageList to selection
  if selectedMessageList ≠ {} then
    set selectedMessage to item 1 of selectedMessageList
    tell selectedMessage
      set msgSub to subject
      set msgDate to date received
    end tell
  end if
end tell

set msgTime to time string of msgDate

return msgTime

It also required a small change in the RegEx:
(\d+):(\d+):?(\d*)\h?(.*)

Note that this RegEx allows for both the Min and AM/PM to be there, or not be there.

Here's the revised Macro:

MACRO:   Date/Time Comparisions [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/f/7/f755b31cec70c0a546598e48636610b40402da6f.kmmacros">Date-Time Comparisions [Example].kmmacros</a> (22 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

### ReleaseNotes

Author.@JMichaelTX 

**PURPOSE:**

* **Show How to Compare Time String with Current Time**
   * Allow for multiple formats of time string

**HOW TO USE**

1. First, make sure you have followed instructions in the _Macro Setup_ below.
2. Select an EMail Message in Apple Mail
3. Trigger this macro.

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  ??
.
1. Assign a Trigger to this maro.
2. Move this macro to a Macro Group that is only Active when you need this Macro.
3. ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
   * Set Variable Local__MsgTimeStr 
     * Source time string from EMail or elsewhere

**REQUIRES:**

1. **KM 8.2+**
2. **macOS 10.11.6 (El Capitan)**

TAGS:  @Date @Time @RegEx @Example

USER SETTINGS:

* Any Action in _magenta color_ is designed to be changed by end-user

ACTION COLOR CODES

* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions


**==USE AT YOUR OWN RISK==**

* While I have given this a modest amout of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

---

![image|564x2000](upload://x9ZnulS1gG7LoUEwc3OKLlrbMl.png)

-------------------------------------------------------------------------------------------

Tags: »Example »Date »Time »RegEx »AppleMail

-------------------------------------------------------------------------------------------
1 Like