Basically the macros were just an experiment to see how well Keyboard Maestro works with Swift scripts. (Running Swift scripts is a new feature of Keyboard Maestro 7.) And it works quite well, I find.
The first macro gives also a nice example of conditional and value-remembering pop-up menus in the user prompt. (I discovered this only recently.)
[tl;dr: go to the download at the bottom of the post.]
A. Timestamp Creator
Creates timestamps either as…
- Unix Time: The traditional and most common timestamp in computing.1 Counts the seconds since January 1, 1970 and looks like this: 1446416013. Due to its 32-bit nature it is said to overflow in 2038. Isn’t that sad⁈
- Mac OS X Cocoa Time: A more modern incarnation. Being a 64-bit double it counts not only seconds, but fractions of seconds and will not overflow until the last Mac will have disappeared from this universe. (Or will it?) As an integer it looks like this: 468109356. It started counting January 1, 2001.2
Each timestamp can be represented as…
- Decimal (base-10): For example 1446416811. Not very compact, but widely used and immediately identifiable as timestamp. Almost human-readable, it allows a rough visual comparison of timestamps, and it is sortable.
- Hexadecimal (base-16): 1be6cafb A bit more compact than the decimal representation, at the cost of human-readability. However, hex timestamps usually are accepted by date and time tools 3 or can be converted with every calculator.
- Hexatrigesimal (base-36): 7qp8fx This elegant encoding uses all 10 numerals (0–9) and all 26 letters of the alphabet (A–Z), hence it produces a very compact timestamp.
- Binary (base-2): 1010110001101101001010110111101 Very bulky and actually not too useful. Just to add some nerdiness 😉
Set your preferred launch method (palette, hotkey, …) and launch the macro. Select timestamp type and representation from the user prompt. Your selection will be remembered, the timestamp will be copied to the clipboard and displayed in a notification.
B. Timestamp Reader
This one complements the first macro: Select a timestamp in any text or copy a timestamp to the clipboard4 and launch the macro. The timestamp will be displayed as readable ISO date and time.
The macro recognizes Unix and Cocoa timestamps in different representations (base–2, base–10, base–16 and base–36 encoding, see above.)
The fun part is that the Timestamp Reader is (or tries to be) smart: you don’t need to tell him type or encoding, he will show you the most plausible interpretation of the timestamp or — in case of doubt — both.5
Download (1.2.2 / 2016-08-27)
- The main scripts of these macros are Swift scripts. That means the macros will only run on Keyboard Maestro 7 or newer.
- If you get Swift-related errors make sure you have installed at least the current version of Swift.6 You can test for the installed Swift version with
xcrun swift --version. To update Swift just install Xcode from the Mac App Store.
- Update 2016-08-10: Compatibel with Swift 3 (Xcode 8).
Sources, Credits, More Info
- Also known as POSIX Time, Epoch Time, Unix Epoch.
- 2001: Begin of the 3rd millennium and birth of Mac OS X. Apple refers to it as “the system’s absolute reference date”.
- For example the date command-line tool.
- Selection has preference over clipboard content.
- However, I guess the script can’t be smarter than its author (me). So don’t expect too much…
- As of August 2016: Swift 2.2