How to use Execute Swift action with #include

I tried using the Swift action this weekend. The first line of my action was (without quotes): “#include ~/phidget22.h”

This failed upon execution with the error:

error: expected expression
#include ~/phidget22.h
^

It almost looks like it doesn’t accept header files. How do I get the Swift action to include a header file? That is the path to my device header file. I’ve tried a variety of variations to that statement such as using quotes. No success.

I even tried by replacing the include statement with the contents of the .h file, and KBM seems to fail on all directive lines. Maybe the Swift action does not support directives? I did contact Peter N Lewis and he said I should ask here because he doesn’t know much about Swift.

The file phidget22.h is a header file required to control my Phidget servo controller. If Swift doesn’t work with header files, can anyone advise me what direction I should go to control my servos from within KBM? For more info: https://www.phidgets.com/docs/Language_-_Swift

I’m afraid I don’t know much Swift.

A quick reading looks like you use

import modulename

And the module must be in the import path. The only way you can affect the import search path is by setting the Keyboard Maestro preference for the swift command:

defaults write com.stairways.keyboardmaestro.engine SwiftCommand '/usr/bin/swift -I "/dir/to/search/for/modules"'

Perhaps someone who knows more swift can give you more direction.

Your “defaults” command gave me an error: (where xxxx was my username).

Could not parse: /usr/bin/swift -I “/Users/xxxx”. Try single-quoting it.

Since you already have single quotes in the command, I didn’t think nesting them would work, so I just dropped the double quotes since there are no spaces in the path. The command was accepted, but then when I tried the import command as you suggested I got:

error: no such module 'phidgets22’
import phidgets22

And I got the same error when I tried it as phidgets22.h

Perhaps there is someone else who can help, but perhaps you can consider why your defaults command gave an error.

I tried reversing the double and single quotes. That got rid of the syntax error with the command, but didn’t fix the problem.

I guessed that maybe the KBM engine had to be restarted after each defaults command, but that didn’t improve my symptoms.

It’s time for me to sleep, since I live near the antipode of where you live. Thanks.

I don’t know what’s going on with the defaults, but regardless, I could not figure out any way to get the swift command line interpreter tool to deal with modules.

I could build a swift app with a module without too much trouble, and then I could run the command easily enough, but not with the swift interpreter.

I don’t know enough about swift to get any further I’m afraid.

Interesting idea. I think I could live with building a swift app externally and executing it from the command line. Basically it would be a simple program that took a couple of parameters, something like this:

Servo

Now I have some homework to do since I’ve never done that before. Thanks. If I get it working I will probably update this thread.

Your KBM language is awesome and your support is excellent.

I got this example to work as a Swift application:

With a few minor changes (I had to change println to print, logger.log("Hello") to logger.log(object:"Hello"). And use d

And used some commands like this

cd Logger
swiftc -emit-library -olibLogger.dylib -emit-object Logger.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name Logger
swiftc -emit-module Logger.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name Logger
cd ../Test
swiftc -I ../Logger  main.swift ../Logger/Logger.o
./main

But again, I really don’t know what I’m doing with Swift.