Jump to content

Alfred Workflows in Swift Tutorial is available.


Recommended Posts

Hi,

 

A tutorial on writing Alfred Workflows using Swift is now available: https://computers.tutsplus.com/tutorials/alfred-workflows-in-swift--cms-21807

 

It goes along with the Text Case Converter in Swift workflow in the "Share Workflows" section. I also have a GitHub of the Alfred Library for Swift: https://github.com/raguay/AlfredSwiftLibrary

 

As ever, thank you so much for creating helpful and thorough tutorials :)

Link to comment
  • 6 months later...

Finally got around to checking this out.  Swift has been updated since this came out.  When I ran the 1st of the four compilation commands, I got the following:

%xcrun swiftc -emit-library -emit-object Alfred.swift -sdk $(xcrun --show-sdk-path --sdk macosx) -module-name Alfred
Alfred.swift:58:105: error: 'countElements' has been renamed to count
        let matches = self.internalExpression.matchesInString(input, options: nil, range:NSMakeRange(0, countElements(input)))
                                                                                                        ^~~~~~~~~~~~~
                                                                                                        count
Swift.countElements:1:36: note: 'countElements' has been explicitly marked unavailable here
@availability(*, unavailable) func countElements<T : _CollectionType>(x: T) -> T.Index.Distance
                                   ^
Alfred.swift:111:30: error: 'AnyObject?' is not convertible to 'String'; did you mean to use 'as!' to force downcast?
        home = edict["HOME"] as String
               ~~~~~~~~~~~~~~^~~~~~~~~
                             as!
Alfred.swift:158:41: error: 'AnyObject?' is not convertible to 'String'; did you mean to use 'as!' to force downcast?
            bundleId = dict["bundleid"] as String
                       ~~~~~~~~~~~~~~~~~^~~~~~~~~
                                        as!

After making the suggested changes, it all worked.

Edited by dfay
Link to comment

Also....I forget to write this up in my comment late last night -- there's another section which I believe needs correction.

 

After the first two screenshots under "The Workflow" you instruct the user to

 

"Copy the Alfred library file created earlier to this directory, rename it to tcconverter.swift, and add this code to the bottom of the file:"

 

When I followed these instructions, I got a compiler error on the 1st of the four calls to the compiler (I didn't record the exact details).  It looked to me like that command is actually looking for the file Alfred.swift .  So rather than renaming that file as the instructions specify, I left it in place and created a new file called tcconverter.swift with just the code for the tcconverter program.  After running the compile commands (and making the changes I noted above), my workflow directory ended up with the following files:

 

Alfred.o

Alfred.swift

Alfred.swiftdoc

Alfred.swiftmodule

info.plist

libAlfred.a

tcconverter

tcconverter.swift

 

And it worked.

 

Great tutorial overall, by the way, even with these hitches -- thanks again for posting it.  If you have a chance, I'd love to see a further explanation of what is going on with the compilation commands and the files that they output.

Edited by dfay
Link to comment

Oops. I thought I had changed that line. I just fixed the tutorial to copy the library file and put the tcconverter.swift file as it's own file. Thanks for spotting that. From XCode 6 beta to XCode 6.3, there has been a lot of changes to Swift!

 

The first compile line creates .o files. The .o file is the object code unlinked. It is the executable code that has placeholders instead of addressress for branching statements. Relative addresses are compiled in on the link stage.

 

The second line creates the library file that other programs can dynamically load the library instead of statically linking them.

 

The third line creates the .swiftmodule and . swiftdoc files needed by the linker to link the library to the final program. These are mostly used by XCode itself and not so much with the 4th stage.

 

The last line compiles our tcconverter code, links it to the Alfred library, and creates the final execuatable, tcconverter.

 

I hope this de-mystifies it some for you. Compiling is complicated up front, but runs so much faster than interpreting. The Swift compiler is much easier to use than like the gcc compiler for C code! You can really get bogged down in options for some compilers.

 

But, if your system is fast enough, I much prefer interpreters!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...