Automatic typing of strings into programs on Mac.
npm install aty
aty is a Node.js package that allows for automatic typing of strings into programs on Mac.
aty can be either installed globally, or as a library.
| Command | Use case | Example |
|---|---|---|
npm i -g aty | Install as a global binary from CLI and use to activate an application and type text into it. | aty "echo hello world\n" -a Terminal |
`` | Install as a dependency and use API to run scripts. | node atyscript |
The demo below shows how one can use aty to automatically type a program which they just wrote.
| Programming like a hacker with aty |
![]() |
- Installation
- Demo
* Programming like a hacker with aty
- Table Of Contents
- API
* [e(lines: string[])](#elines-string-void)w()
* aty(code: string): string
* activateApp(name: string): string
* type(string: string, delayFrom?: number = 50, delayTo?: number = 100, noIndent?: boolean = false): string
* typeInstant(string: string): string
* delay(time: number): string
* keystroke(word: string, commands?: string[]): string
* [](#keystrokeword-stringcommands-string-string)code(code: string, commands?: string[]): string
* [](#codecode-stringcommands-string-string)
- API Examples
* Register Domain For Package: mnp-expensive
- Key Code Reference Table
- CLI
* Accepted Arguments
-t, --text
-a, --app
* -i, --instant
* -d, --minDelay
* -m, --maxDelay
* -h, --help
* -v, --version
- CLI Examples
* aty "echo test\n" -i date -d 100 -a Terminal
- Copyright
The package is available by importing:
- its default function aty for generating an Apple Script code to be executed;exec
- the command to execute it;e
- the function which is a combination of aty and exec;w
- the function to pause execution of the script and wait for user input;type
- various tagged templates to compose code for the script (e.g., such as , activateApplication, delay, code, _etc_).
`js`
import aty, {
exec, e, w, activateApp, type, typeInstant, delay, keystroke, code,
} from 'aty'
`js
/ yarn example /
import aty, { exec, type, typeInstant, activateApp } from 'aty'
const s = Hello World! \\
This is a new application that can type strings.
;(async () => {
const a = aty
${activateAppCode}
${typeInstantWelcome.}
${type${s}${10}${20}}
`
await exec(a)
})()
Execute commands from the passed array. It is an alternative way to write scripts which does not require to import aty and exec.
`javascript
/ yarn example/e.js /
import { e, activateApp, type } from 'aty'
(async () => {
await e([
activateAppTerminal,echo hello world\n
type,`
])
})()
Wait for user to enter something via stdin. This command allows to pause executing a script, e.g., when it is necessary to wait for the result of typing, however how long to wait is unknown.
`javascript
/ yarn example/e.js /
import { e, w, activateApp, type } from 'aty'
(async () => {
await e([
activateAppTerminal,echo hello world\n
type,Terminal
])
await w()
await e([
activateApp,date\n
type,`
])
})()
This tagged template is used to generate the total code to be executed. It will include necessary types, and put all other blocks together. It should be used to generate a string to pass to the exec method.
`js
/ yarn example/hacker.js /
import { readFileSync } from 'fs'
import { resolve } from 'path'
import aty, {
type, typeInstant, activateApp, code, keystroke, delay,
} from 'aty'
const f = readFileSync(resolve(__dirname, __filename))
const a = aty
${activateAppCode}
${typeInstant/ recorded with appshot /}
${type${f}${15}${20}${true}}
${keystroke${'option'}${'shift'}f}
${delay2000}
${keystroke${'command'}a}
${delay1000}
${code51}
console.log(a)
`
`applescript
on type(the_string, delay_from, delay_to)
set theList to paragraphs of the_string
set listCount to count of theList
repeat with i from 1 to listCount
tell application "System Events"
repeat with c in item i of theList
keystroke c
delay (random number from delay_from to delay_to)
end repeat
if i is not listCount then key code 36
end tell
end repeat
end type
on typeInstant(the_string)
tell application "System Events"
keystroke the_string
key code 36
end tell
end type
activate application "Code"
typeInstant ("/ recorded with appshot /")
type ("/ yarn example/hacker.js /
import { readFileSync } from 'fs'
import { resolve } from 'path'
import aty, {
type, typeInstant, activateApp, code, keystroke, delay,
} from 'aty'
const f = readFileSync(resolve(__dirname, __filename))
const a = aty
${activateAppCode}
${typeInstant/ recorded with appshot /}
${type${f}${15}${20}${true}}
${keystroke${'option'}${'shift'}f}
${delay2000}
${keystroke${'command'}a}
${delay1000}
${code51}
console.log(a)", 0.015, 0.02)
tell application "System Events" to keystroke "f" using {option down, shift down}
delay 2
tell application "System Events" to keystroke "a" using command down
delay 1
tell application "System Events" to key code 51
`
Activate an app with the given name.
`js
/ yarn example/activate-app.js /
import { activateApp } from 'aty'
const app = 'Terminal'
const a = activateApp${app}Code
const b = activateApp
console.log(a)
console.log(b)
`
`applescript`
activate application "Terminal"
activate application "Code"
The type template is used to generate a command to type a text into an application. It can be used after activateApp command to send text to a particular application. aty will automatically include the type function when type was used in the script. noIndent can be used to replace white spaces in the beginning of each line.
`js
/ yarn example/type.js /
import aty, { type } from 'aty'
const t = 'admin\\n'
const a = type${t}Though sympathy alone can't alter facts,
const b = type
it can help to make them more bearable.
${50}${100}${true}
console.log(aty
${a}
${b})`
`applescript
on type(the_string, delay_from, delay_to)
set theList to paragraphs of the_string
set listCount to count of theList
repeat with i from 1 to listCount
tell application "System Events"
repeat with c in item i of theList
keystroke c
delay (random number from delay_from to delay_to)
end repeat
if i is not listCount then key code 36
end tell
end repeat
end type
type ("admin\n", 0.05, 0.1)
type ("Though sympathy alone can't alter facts,
it can help to make them more bearable.
", 0.05, 0.1)
`
Same as type, but will insert text immediately without a delay. When aty sees a string which begins with typeInstant, it will automatically include the typeInstant function.
`js
/ yarn example/type-instant.js /
import aty, { typeInstant } from 'aty'
const t = 'Type Instant Text'
const a = typeInstant${t}Do you believe in destiny?
const b = typeInstant
That even the powers of time can be altered for a single purpose?
console.log(aty
${a}
${b})`
`applescript`
on typeInstant(the_string)
tell application "System Events"
keystroke the_string
key code 36
end tell
end type
typeInstant ("Type Instant Text")
typeInstant ("Do you believe in destiny?
That even the powers of time can be altered for a single purpose?")$3
Delay execution by n milliseconds.
`js
/ yarn example/delay.js /
import { delay } from 'aty'
const d = 100
const a = delay${d}500
const b = delay
console.log(a)
console.log(b)
`
`applescript`
delay 0.1
delay 0.5
Send a key stroke. If commands are provided, they will be included.
`js
/ yarn example/keystroke.js /
import { keystroke } from 'aty'
const a = keystrokea${'command'}s${'alt'}${'command'}
const b = keystroke
console.log(a)
console.log(b)
`
`applescript`
tell application "System Events" to keystroke "a" using command down
tell application "System Events" to keystroke "s" using {alt down, command down}
Send a code. If commands are provided, they will be included with using keyword. Some codes can be found in the Key Code Reference Table below.
`js
/ yarn example/code.js /
import { code } from 'aty'
const a = code36${'command'}36${'command'}${'shift'}
const b = code
console.log(a)
console.log(b)
`
`applescript`
tell application "System Events" to key code 36 using command down
tell application "System Events" to key code 36 using {command down, shift down}
Register Domain For Package: mnp-expensive: This example shows how to activate the Terminal app, and execute 4 commands, waiting for user-input to begin each, i.e., when any key rather than n is entered, aty will continue execution.
| JS Code | Generated Code |
|---|---|
` (async () => { | ` set type to "aty:Type.scpt" as alias activate application "Terminal" waits for enteractivate application "Terminal" type ("expensive alamode\n", 0.05, 0.1) waits for enteractivate application "Terminal" type ("expensive alamode.app -r\n", 0.05, 0.1) waits for enteractivate application "Terminal" type ("y\n", 0.05, 0.1) # |
| Example | |
!Quick package and domain names check, registering a domain. | |
` | ||||||
| Key | Name | Code | Key | Name | Code | |
|---|---|---|---|---|---|---|
⎋ | escape | 53 | ! 1 | key 1 | 18 | |
⇥ | tab | 48 | spacebar | 49 | ||
↩ | return | 36 | ⇞ | page up | 116 | |
⌤ | enter | 76 | ⇟ | page down | 121 | |
⌃ | left ctrl | 59 | ⇧ | left shift | 57 | |
⌥ | left option | 58 | → | right arrow | 124 | |
⌘ | left command | 55 | ⇪ | capslock | 57 | |
Find more at:- Complete list of AppleScript key codes | ||||||
The package can be used from the CLI if installed globally. It can generate an apple script to activate an app specified with -a argument, and type text into it.
`sh`
aty -h
` aty "test text" -a appToActivate -i "instant text" [-d 10] [-m 50] [-hv] text The text to type. Example: aty "echo test\n" -i "date" -a iTerm |
Arguments can be passed by specifying their value -arg value, e.g., -a app`.
| Property | Type | Description | Example |
|---|---|---|---|
-t, --text* | string | The text to type. New line (\n) chars will be used to press Enter key. | echo hello world\n |
-a, --app* | string | Application to activate before typing. | Terminal |
-i, --instant | string | Text to enter without a delay after activating the app but before typing the text. | date |
-d, --minDelay | number | The minimum delay with which to type. Default 50ms. | 200 for 200 ms. |
-m, --maxDelay | number | The maximum delay with which to type. Default 100ms. | 500 for 500 ms. |
-h, --help | boolean | Show help information. | aty -h |
-v, --version | boolean | Show version. | aty -v |
(c) [Art Deco][1] 2018
[1]: https://artdeco.bz