Has anyone here been able to enable their users to...
# compose-desktop
d
Has anyone here been able to enable their users to script their Compose Desktop app? I've imported all the relevant libraries:
Copy code
dependencies {
    implementation("org.jetbrains.kotlin:kotlin-scripting-common:2.0.0")
    implementation("org.jetbrains.kotlin:kotlin-scripting-jvm:2.0.0")
    implementation("org.jetbrains.kotlin:kotlin-scripting-jvm-host:2.0.0")
    implementation("org.jetbrains.kotlin:kotlin-script-runtime:2.0.0")
    implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:2.0.0")
    implementation("org.jetbrains.kotlin:kotlin-scripting-compiler-embeddable:2.0.0")
    implementation("org.jetbrains.kotlin:kotlin-main-kts:2.0.0")
}
Written a simple ScriptEngine:
Copy code
class ScriptEngine {
    companion object {
        val engine = ScriptEngineManager().getEngineByExtension("kts")
            ?: throw RuntimeException("Kotlin script engine not found")
    }

    fun execute(script: String): Result<Any?> = runCatching {
        engine.eval(script)
    }
}
...and tried to run it:
Copy code
fun main() {
    val engine = ScriptEngine()
    val result = engine.execute(
        """
        println("Hello from script!")
        2 + 2
        """.trimIndent()
    )
    println("Script result: ${result.getOrNull()}")
}
It always says it can't locate the engine. If I enumerate the engines I get an empty list.
👀 1
if you need more help there is #C0BT46EL8 as well
d
Thank you @Chrimaeon. I tried this, but I still get the same error. Does this work for you? Are you using Kotlin 2.0.0? Also, I think the scripting API is different than using the JSR223 lib. According to the docs (which are dated), and Claude, the API provides more features, but at this point I'll take any road to a working script engine for our users. It seems like JetBrains has stopped maintaining Kotlin scripts other than what is required for a few use cases such as the KTS version of Gradle. I hope I'm wrong.
c
I haven’t tried it. I was just curious what the Kotlin script engine provides and stumbled across the examples in the repo.