Keyboard Maestro supports Swift?!

OK, you guys have been holding out on me. I’ve been complaining about having trouble understanding various Apple languages, and here I stumble across Swift. WTF? How could you not mention this? I’m kidding, of course, but I’m also not.

Just started reading about Swift. It looks awesome! Somewhat similar to C#, but with some cool advantages.

Any caveats I need to know about KM/Swift interactions?

The Execute Swift actions are an excellent addition - a particularly good vehicle for calls to ObjC functions.

It’s a better language than JavaScript ES5 (let statements, proper typing etc, and the playgrounds are fun).

My only reason for not using it much personally (I probably should) is just a mild and entirely irrational feeling of claustrophobia about single platform (or singlish platform, to be fair, in this case) languages. I can forgive JavaScript a lot for being such a grubbily well-worn and undiscriminating market currency :slight_smile:

Have fun with Swift ! I look forward to seeing some examples.

(The Apple documentation is very well done, but this looks particularly interesting, I think, at least to me:

https://www.objc.io/books/functional-swift/ )

So far, I feel right at home. I’ll do some timing tests to see if this is particularly slower or faster than AS, but the one thing I got working in KM seemed like it ran pretty fast.

I’m loving “The Swift Programming Language” guide from Apple, because it’s written for developers. Of course I’m still trying to figure everything out, but come on - Generics support! Awesome!

This is a good page – enough pre-built collection methods to structure most things pretty quickly and painlessly:

https://developer.apple.com/library/mac/documentation/Swift/Reference/Swift_CollectionType_Protocol/index.html#//apple_ref/swift/intf/s:Ps14CollectionType

Interesting. Does Swift have a good code editor or IDE?

XCode playgrounds

https://developer.apple.com/swift/blog/?id=24

1 Like

Thanks - that is good.

There’s a concept I’m still struggling with. It’s shown up in many of the various ways I’ve tried to deal with clipboard data from KM. Eventually I find a solution, then I kind of understand, but now when I run up to it in Swift, it’s thrown me for a loop.

After I use my stolen, ahem, borrowed code to get a KM macro off the clipboard, it needs to be converted to a native format. In AppleScript this is easy (once you know where to steal the code from):

You deserialize it with something like this:

set {_object, _error} to current application's NSPropertyListSerialization's propertyListWithData:_data options:0 format:(missing value) |error|:(reference)

Assuming you have a valid result, you convert it to an AS object like this:

return _object as list

OK, I understand that now that I’ve seen it.

So then I move to Swift, and I get an object pretty much the same way. It’s an AnyObject?, and I want to convert it to a native Array of Dictionary<string, string> with children, etc. If it’s so easy in AS, by using “as list”, wouldn’t you think there’d be a similarly easy way in Swift?

Not that I can find.

And this is what continues to happen to me in whatever language I choose. Sigh. So frustrating, again, to be so close. I guess I’ll resort to parsing it into Swift objects through iteration. But you watch - about the time I’m done with that, I’ll stumble across the simple answer.

Rant warning on.

Not really about KM, but we all have so much fun talking about other things that I couldn’t resist, and I feel extremely strongly about Swift. I hope this post doesn’t get me banned from the KM forums; t is very nearly shouting, but in exasperation.

I have no idea what you guys are talking about. Swift is repulsive It is endlessly complex. It seems to me to be incomprehensible to ordinary mortals, and even extraordinary developers will struggle with it. It is as if C++ reference values had escaped and proliferated wildly, mutating frequently.

I wrote machine-level programs (as in raw numbers) and a lot in assembly languages. I have worked extensively with Pascal, Lisp, Smalltalk, AppleScript, various shells, C, C++, Java, JavaScript, Emacs Lisp, and Python. I’ve done some in APL,PL/1, Algol, Perl, and Objective C. (Strangely, I skipped Basic.) I wrote one of the first C++ Data Structures books and Bioinformatics Programming Using Python for O’Reilly. I am pretty much an expert in every aspect of object technologies. (Oh, this is sounding awful — I never brag, but occasionally I feel I really need to establish my credentials when making strong statements.) I have taught programming, data structures, databases, and various aspects of object technologies at universities and businesses. I have read a huge number of books programming languages (and just about all aspects of software development).

I have read Apple’s Swift book (which given the challenge the language presents is a truly fine and impressive work). I have read various online introductions. I have worked through tutorials. And now I find myself struggling with a Swift iOS project after what I had thought was sufficient preperation.

Swift seems to incorporate every language feature there ever was, with particular emphasis on what even experienced programmers get tripped up by. It is a huge language. And it gets promoted as fun and easy to learn! I must be missing an awful lot here — I just can’t see how more than a very small minority of programmers will ever master it, and not many will use more than its basic features. But what am I talking about? How would you extract a small subset of the language to keep things simple? The demands in just declaring and using constants and variables are scary. I It sure is fun trying to absorb all that complexity and detail — makes me nostalgic for every language I ever used. I mean there’s enough there to keep learning forever, which is promising in a perverted way.

That all said, I have neither taught nor consulted with people who are learning or using the language, so I am only going by my own reactions not any kind of real-life observations. And I wish everyone all the best with this mess.

1 Like

Xcode and Swift are very closely tied together and support each other. I don’t know why anyone else would bother supporting Swift in an IDE. A good way for a project or company to dig themselves under the ground.

I think the main limitation of Swift for the KM type of context is the limited support for application automation – it looks a bit tricky to fiddle with NSAppleScript and NSTask, and those parts of the the code are effectively going to be AppleScript (or possibly JavaScript for Automation) anyway.

Not easy to beat the support for conversion of ObjC types with $() and ObjC.unwrap() / .deepUnwrap() in JavaScript for Automation …

1 Like

You know, I’ve probably read one or more of your books. Way cool!

I get where you’re coming from. I totally do. Apple is seriously wrong to promote Swift was easy to learn. To me, it looks like a serious developers language.

Why do I say that? Coming from the Windows/MS/C# world, the similarities between Swift and C# are way too numerous to ignore. I have to believe that Swift is Apple’s answer to C#.

It looks like they took the best of C#, changed a little of the syntax to be more Apple-like, enhanced it in some very good ways (Enumerations can have methods, for instance), and re-branded it. Seriously. I’m at home in it instantly, except when dealing with “NS” stuff.

On the Windows side, there is a HUGE divide between pro- and con- C# people. I think I know which side you’d be on. :slight_smile:

Rob, many thanks for your post. You must have been ready my mind!

So, IOW, Swift is not a replacement for JXA or AppleScript, for doing application automation, correct?

I wonder, is it possible, to create a nice GUI form using Swift that can be used with JXA/AS?

You can also use XCode to create regular Swift applications. I'm using a console application to test code right now, because you can't do breakpoints in playgrounds. Works well enough.

Dan, what you do see as the primary use case for Swift WRT KM?
Do you see it replacing other tools you have been using? If so, which?

Excellent questions. I don’t know yet.

Honestly, my hope is to use Swift for everything, because I’m so at home in it. I’m working on converting the AS in the “Renumber” macro I uploaded yesterday, to see how it compares.

OK, thanks for sharing.

I’m potentially interested in Swift, but I’m so loaded up right now I don’t have time to take on a new language. But I will look forward to your reports on Swift.

If they look highly favorable, then I may have to make time.

Right now, my main learning focus is on JXA and JavaScript.
The biggest drawback, from my perspective, of JXA (and AppleScript) is the lack of support for nice, complex, GUI forms/windows.

1 Like

GUI you can do in XCode with JavaScript for Automation or even AS if you want to, but for simplicity and speed (after all a script is not a space program) I would personally prefer to use Pashua.

I don't know . . . after all, the computer used to land man on the moon was far less powerful than even the first Macs/PCs. So my script just might . . .

Or, maybe not. :wink:

I have tried Pashua, and did like it, but found two deal-breakers (for me):

  1. Window/form design is 100% in code, requiring a lot of iteration (for me anyway) to get it right
  2. Limited number of form controls -- while more than native JXA/AS, still very limiting, IMO

Currently, my main interest in GUI forms for scripting are:

  1. KM HTML Prompt
  2. Xojo -- general purpose app development, but with good support for scripting (both using, and being used by).

I am particularly excited by the opportunities offered by the KM HTML Prompt, since it opens up window/form design to almost all JavaScript/CSS libraries, and I do the design in a GUI web page editor.

Then it's HTML and JavaScript for you, right?

and JXA. I have posted a macro/script which does a proof-of-concept demo of calling a KM HTML Prompt from JXA.

Demo Using KM Custom HTML Prompt with AppleScript and JXA