Member-only story
How To Use Swift Scripts To Modify Files In a Folder
Automate repetitive stuff
Swift is a wonderful language. It can be used for everything: iOS apps, libraries, macOS apps, and command line interfaces (CLI) apps. You can also use it as a scripting language.
This week, I had to do repetitive work, and I didn’t want to do it manually. I decided to write a script for it, and I wrote it using Swift.
Why Swift?
Using Swift as a scripting language lets you use all the nice Swift features. You can use Xcode code completion and its debugger. You can write scripts using all the powerful Swift features: enums, protocols, and above all else, scripting, types, and type checking. Type safety is something that most common scripting languages (Python, Javascript, Ruby, and more) usually don’t have.
If you are an iOS engineer, using Swift in your daily workflow does not require any extra setup: Xcode comes with the compiler, and we can run Swift scripts by using the following command:
swift <path-to-your-script>.swift [args]
How to Use Swift as Scripting Language?
Using Swift as a scripting language comes with some rules and caveats. There are just two important rules: