Using Swift Script To Create Newsletter For iOS Cookies


If you read my last post, you know I’ve launched iOS Cookies, collection of libraries written in Swift. It’s a side project, and besides improving it and working on new features, it needs a lot of attention. Adding new libraries(many suggested by some of you), posting them on twitter and probably most time consuming – creating newsletter, which I’ve failed to do few times. Those tasks are very boring and mundane, so I thought there must be a better way.

Let’s automate boring tasks!

iOS Cookies are built on flat-file CMS Grav. All libraries are stored in .yaml file, so if I want to add a new one, I need to write something like this:

Whenever I’ve added new library, I’d tweet about it like this:

Simple pattern to follow, even simpler to automate.

At the beginning, I opened XCode playground, created simple struct:

and started to playing with strings, parsing, etc.

Parsing .yaml file

This is the biggest function in my script. Go through each line in file and store data to Library struct. Finally return Array<Library>. But hold on! Description and category are not in shape to be tweeted yet.

Each category needs better look, so let’s improve above struct.

Now just push it through switch-statement:

What about description? Well sometimes it can contain a link, which in .yaml looks for example like this:

inspired by the popular [SDWebImage](

Combination of regular expressions and stringByReplacing OccurrencesOfString()will help use to loose those “[]” and “(link)”, leaving just a title.

Can I get a tweet please?

So far we have array of parsed libraries, what now? Ready-to-use string would be fine.

Didn’t I promised newsletter?

Having all this done, I realised that I can write newsletter directly in HTML and so this could be automated as well. Parsing is already done, all we need is string representation, which can be done in similar manner.

Every library has this HTML body:

What about category? Well I group all new libraries by category, so having a script which would go through all new libraries and grouped them for me would be awesome! Using some for-loop, it’s a very easy to do.

That’s final look of generated HTML, when put into my mailchimp template:

Scrap of iOS Cookies Newsletter

How to actually make a Swift script and write result to file?

There’s a great article about Swift scripting, written by @ayanonagon, which was big help.Basically you need to add below line at the top of your script:

And now you can run it from command line:

Here’s my method to write output to file:

For tweets I call:

And for newsletter:

That’s all! Writing this script was fun, saves me a lot of time and I don’t need to do those boring tasks manually anymore. You can check the script on GitHub.