Timestamp Macros (with Swift scripts)

Since KM now supports Swift scripts, I thought why not use Swift instead of AppleScript or Shell scripts? (ahem… and mainly to practice Swift, which I’m trying to learn…)

It turned out that Swift as script language works nicely with KM! In this case, I think, it may be even a very good way to go.

These are mainly example macros. But if you are using timestamps they may indeed be useful :blush:

The first macro, Timestamp Creator, creates a timestamp and copies it to the clipboard. You can choose from…

  • Unix Time (1970) or Apple’s Cocoa Time (2001) and…
  • one of these representations: Decimal, Hex, Base-36, Binary

This is the macro structure:

It comes with an accompanying macro Timestamp Reader, which – you guessed it – will convert a timestamp to a readable date format. It tries to be smart and detect automatically format (Unix/Cocoa) and encoding (Decimal, Hex, Base-36, Binary):

You find more screenshots, documentation and the download on my blog post.

Note: Works only with Keyboard Maestro 7.



Update (2016-08-27)

The script now tests for the installed Swift version, und thus should run fine with any of these versions:

  • Swift 2.2 (current release version, Xcode 7 / macOS El Capitan)
  • Swift 2.3 (next release version, Xcode 8 / macOS Sierra)
  • Swift 3.0 (current beta version, Xcode 8)

Other changes: Prevented that some base-36 timestamps are misinterpreted as base-16 by the Timestamp Reader.

Timestamp Reader and Creator.kmmacros (78.2 KB) — 2016-08-27

Update (2016-08-10)

Both macros are now compatible with Swift 3:

Timestamp Reader and Creator-Swift 3.kmmacros (81.1 KB) — 2016-08-10

Other changes: The Reader macro now checks for empty selection and uses the clipboard if nothing is selected. So you can run the macro either on selected text, or on existing clipboard content.

Update for the Reader macro (2018-07-07 12:52Z)

Here a little update to get rid of the Deprecated warning when reading time stamps (strings have changed in Swift 4):

Timestamp Reader (1.2.3 - 2018-07-07).kmmacros (28.9 KB)

The Timestamp Creator macro is not affected. Here the current version:
Timestamp Creator.kmmacros (51.3 KB)

Strings in Swift 4

A couple of '2 yrs later' questions

  • Does Swift still feel like a bit of a moving target, for these purposes ?
  • Are there any usable app automation interfaces for it, now ?

I’m the wrong addressé for that question. I’m still learning Swift (yeah, still, months of doing nothing in respect), and, so, for me it it is a moving target. But I guess, while learning a language, that language is always a “moving target”, by definition. And so, I don’t really care about changes. I’m happy that the language is evolving, and I’m applying the changes to my production programs (currently just 1; so, no major efforts)

Not exactly sure what you mean. As mentioned, I’m not the best source for the latest news on Swift.
What I know is the function for AppleScripts: NSAppleScript. This is wikified.

1 Like