Task Automation in Swift: From Boredom to the Magic of a Single Command

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Why Swift is Not Just About Apps

\n\n

When you hear "Swift," the first association is iOS development, Xcode, storyboards, and endless constraints. But there is another side to this language that not everyone knows about. Swift is a powerful tool for task automation on macOS. And I'm not talking about compiling applications, but about ordinary, everyday things: renaming a hundred files, parsing a CSV report, sending a notification at 6:00 PM, or collecting logs from a server.

\n\n

Why Swift instead of Python or Bash? Because Swift:

\n
    \n
  • works natively with macOS (access to Foundation, Dispatch, UserNotifications);
  • \n
  • does not require a virtual environment or dependencies (if you don't dive into SPM);
  • \n
  • provides clear compilation errors — you won't spend an hour looking for an extra space like in Bash;
  • \n
  • can be run as a script with a single command: swift myScript.swift.
  • \n
\n\n

In this article, I will show you several real-world automation scenarios in Swift. You can copy the code, run it on your machine, and most importantly, understand how to write your own utilities. Let's go!

\n\n

How a Swift Script Works: Minimum Theory

\n\n

To write automation in Swift, you don't need Xcode. Just a terminal and any text editor. Create a file script.swift and run it with the command:

\n\n
swift script.swift
\n\n

Swift will compile and execute the code itself. If the script is large and you want to speed up subsequent runs, you can pre-compile it: swiftc -O script.swift -o script and then run the binary.

\n\n

Everything we will write from here on works out of the box — no third-party libraries. Only Foundation, which is imported automatically.

\n\n

Scenario #1: Batch File Renaming

\n\n

Imagine you have a folder with photos IMG_001.jpg, IMG_002.jpg … IMG_100.jpg. You want to rename them to Vacation_1.jpg, Vacation_2.jpg, and so on. Manually — 100 operations. With a script — 0.5 seconds.

\n\n
import Foundation\n\nlet fileManager = FileManager.default\nlet currentPath = fileManager.currentDirectoryPath\n\nguard let files = try? fileManager.contentsOfDirectory(atPath: currentPath) else {\n    print("Failed to read folder")\n    exit(1)\n}\n\nlet imageFiles = files.filter { $0.hasSuffix(".jpg") || $0.hasSuffix(".png") }\n\nfor (index, file) in imageFiles.enumerated() {\n    let newName = "Vacation_\\(index + 1)." + (file.hasSuffix(".jpg") ? "jpg" : "png")\n    let oldURL = URL(fileURLWithPath: currentPath).appendingPathComponent(file)\n    let newURL = URL(fileURLWithPath: currentPath).appendingPathComponent(newName)\n    \n    do {\n        try fileManager.moveItem(at: oldURL, to: newURL)\n        print("✅ \\(file) → \\(newName)")\n    } catch {\n        print("❌ Error with file \\(file): \\(error.localizedDescription)")\n    }\n}
\n\n

How it works:

\n
    \n
  • FileManager.default — access to the file system;
  • \n
  • contentsOfDirectory — get the list of files in the current folder;
  • \n
  • filter only images;
  • \n
  • rename using moveItem (yes, in Swift renaming is moving).
  • \n
\n\n

You can easily adapt the script: change the prefix, extensions, add a date to the name. This is already a useful Swift script that saves time every day.

\n\n

Extra Feature: Suffix with Date

\n\n
let dateFormatter = DateFormatter()\ndateFormatter.dateFormat = "yyyy-MM-dd"\nlet dateString = dateFormatter.string(from: Date())\n\nfor (index, file) in imageFiles.enumerated() {\n    let newName = "\\(dateString)_\\(index + 1).jpg"\n    // ... continue as in the example above\n}
\n\n

Scenario #2: Par

Blogs

Book Recommendations