Skip to content

Commit

Permalink
Merge pull request #18 from aivanovski/feature/add-keyboard-emulation…
Browse files Browse the repository at this point in the history
…-via-apple-script

Add keyboard emulation via AppleScript
  • Loading branch information
aivanovski committed Jun 30, 2022
2 parents 6df0191 + 1b1ddd4 commit abc4140
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/main/java/com/github/ai/autokpass/di/KoinModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.github.ai.autokpass.domain.autotype.AutotypePatternFormatter
import com.github.ai.autokpass.domain.autotype.AutotypePatternParser
import com.github.ai.autokpass.domain.autotype.AutotypeSequenceFactory
import com.github.ai.autokpass.domain.autotype.CliclickAutotypeExecutor
import com.github.ai.autokpass.domain.autotype.OsaScriptAutotypeExecutor
import com.github.ai.autokpass.domain.autotype.ThreadThrottler
import com.github.ai.autokpass.domain.autotype.XdotoolAutotypeExecutor
import com.github.ai.autokpass.domain.formatter.DefaultEntryFormatter
Expand Down Expand Up @@ -67,7 +68,8 @@ object KoinModule {
single(named(AUTOTYPE_EXECUTORS_MAP)) {
mapOf(
AutotypeExecutorType.XDOTOOL to XdotoolAutotypeExecutor(get(), get()),
AutotypeExecutorType.CLICLICK to CliclickAutotypeExecutor(get(), get())
AutotypeExecutorType.CLICLICK to CliclickAutotypeExecutor(get(), get()),
AutotypeExecutorType.OSA_SCRIPT to OsaScriptAutotypeExecutor(get(), get())
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ enum class Argument(
AUTOTYPE(
fullName = "autotype",
shortName = "a",
description = "Program responsible for emulation of keyboard pressing (xdotool - for Linux, cliclick - for macOS)"
description = """Program responsible for emulation of keyboard pressing, available options:
xdotool - default for Linux (xdotool should be installed on the host machine)
cliclick - default for macOS (cliclick should be installed on the host machine)
osascript - optional for macOS (keyboard pressing will be emulated via AppleScript)
"""
),

INPUT(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.ai.autokpass.domain.autotype

import com.github.ai.autokpass.model.AutotypeSequence
import com.github.ai.autokpass.model.AutotypeSequenceItem
import com.github.ai.autokpass.presentation.process.ProcessExecutor

class OsaScriptAutotypeExecutor(
private val processExecutor: ProcessExecutor,
private val threadThrottler: ThreadThrottler
) : AutotypeExecutor {

override fun execute(sequence: AutotypeSequence) {
sequence.items.forEach { item ->
when (item) {
is AutotypeSequenceItem.Enter -> {
processExecutor.executeWithBash(
"echo \"tell application \\\"System Events\\\" to key code 36\" | osascript"
)
}
is AutotypeSequenceItem.Tab -> {
processExecutor.executeWithBash(
"echo \"tell application \\\"System Events\\\" to key code 48\" | osascript"
)
}
is AutotypeSequenceItem.Text -> {
processExecutor.executeWithBash(
"echo \"tell application \\\"System Events\\\" to keystroke \\\"${item.text}\\\"\" | osascript"
)
}
is AutotypeSequenceItem.Delay -> {
threadThrottler.sleep(item.millis)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package com.github.ai.autokpass.model

enum class AutotypeExecutorType(val cliName: String) {
XDOTOOL(cliName = "xdotool"),
CLICLICK(cliName = "cliclick")
CLICLICK(cliName = "cliclick"),
OSA_SCRIPT(cliName = "osascript")
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class JprocProcessExecutor : ProcessExecutor {
.outputString
}

override fun executeWithBash(command: String): String {
return ProcBuilder("bash", "-c", command)
.run()
.outputString
}

override fun execute(input: String, command: String): String {
val (com, args) = command.splitIntoCommandAndArgs()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ interface ProcessExecutor {

fun execute(command: String): String

fun executeWithBash(command: String): String

fun execute(input: String, command: String): String

fun execute(input: ByteArray, command: String): String
Expand Down

0 comments on commit abc4140

Please sign in to comment.