Automation in Kotlin: Why It's Worth Your Attention
Have you ever spent an hour renaming a hundred files, manually parsing logs, or copying data from one API to another? A familiar pain. Usually, for such tasks, we reach for Python or Bash, but Kotlin is a surprisingly powerful and elegant tool for automation in Kotlin. It combines the conciseness of a modern language with JVM performance and full access to the Java ecosystem.
In this article, we won't write another "Hello, World". We'll get straight to the point: we'll look at Kotlin code examples that are truly useful in the daily work of a developer, sysadmin, or tester. You'll learn how to write Kotlin scripts for working with files, sending HTTP requests, parsing data, and running system commands. Let's go!
Why Kotlin, not Python or Bash?
Every language has its niche. Bash is ideal for quick one-liners, Python for complex logic and data science. But Kotlin for automation offers several unique advantages:
- Type safety. You won't get a "string vs int" error at runtime — the compiler catches it at the start.
- Full Java compatibility. Any Java library (Apache POI, OkHttp, Jackson) is available out of the box.
- Kotlin Script (.kts). You can write scripts without compilation — run them like a regular .sh file.
- Concise syntax. Less boilerplate code, more readability.
Setting Up the Environment for Kotlin Scripts
To run Kotlin scripts, you don't need to install Android Studio. It's enough to:
- Install the Kotlin compiler (or SDKMAN).
- Create a file with the
.ktsextension. - Run it with the command:
kotlinc -script script.kts
Done. Now let's write our first truly useful script.
Example 1: Bulk File Renaming
Imagine: you have a folder with photos IMG_001.jpg, IMG_002.jpg ... IMG_100.jpg. You need to rename them to holiday_01.jpg, holiday_02.jpg .... By hand — 10 minutes. With a script — 2 seconds.
import java.io.File
fun main() { val folder = File("/path/to/photos") val files = folder.listFiles()?.filter { it.extension == "jpg" } ?: return
files.sorted().forEachIndexed { index, file -> val newName = "holiday_${index.toString().padStart(2, '0')}.jpg" val newFile = File(folder, newName) file.renameTo(newFile) println("Renamed: ${file.name} -> $newName") }}
// Run: kotlinc -script rename.ktsThis Kotlin code example does three things: filters only JPG files, sorts by name (to preserve order), and renames with numbering. Note the padStart — an elegant Kotlin feature for number formatting.
Example 2: HTTP Request and JSON Parsing
Often you need to automate data collection from a web API. Let's say we fetch a list of users and save them to CSV. In Kotlin for automation, this is done in a few lines using the ktor-client library (or the good old java.net.HttpURLConnection).
But for a script, it's easier to add an external library via @file:DependsOn:
@file:DependsOn("com.google.code.gson:gson:2.10.1")@file:DependsOn("io.ktor:ktor-client-core:2.3.7")@file:DependsOn("io.ktor:ktor-client-java:2.3.7")
import io.ktor.client.*import io.ktor.client.request.*import io.ktor.client.statement.*import com.google.gson.Gsonimport com.google.gson.reflect.TypeTokenimport java.io.File\