aty
aty
is a Node.js package that allows for automatic typing of strings into programs on Mac.
Installation
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
|
Demo
The demo below shows how one can use aty
to automatically type a program which they just wrote.
Programming like a hacker with aty |
![]() |
Table Of Contents
- Installation
- Demo
- Table Of Contents
- API
e(lines: string[])
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
code(code: string, commands?: string[]): string
- API Examples
- Key Code Reference Table
- CLI
- CLI Examples
- Copyright
API
The package is available by importing:
- its default function
aty
for generating an Apple Script code to be executed; - the
exec
command to execute it; - the
e
function which is a combination ofaty
andexec
; - the
w
function to pause execution of the script and wait for user input; - various tagged templates to compose code for the script (e.g., such as
type
,activateApplication
,delay
,code
, etc).
/* yarn example */ const s = `Hello World! \\This is a new application that can type strings.` ;async { const a = aty` ` await }
e(
lines: string[],
): void
Execute commands from the passed array. It is an alternative way to write scripts which does not require to import aty
and exec
.
/* yarn example/e.js */ async { await }
w(): void
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.
/* yarn example/e.js */ async { await await await }
aty(
code: string,
): string
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.
/* yarn example/hacker.js */ const f = const a = aty`` console
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 repeatend typeon typeInstant(the_string) tell application "System Events" keystroke the_string key code 36 end tellend typeactivate 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`${activateApp`Code`}${typeInstant`/* recorded with appshot */`}${type`${f}${15}${20}${true}`}${keystroke`${'option'}${'shift'}f`}${delay`2000`}${keystroke`${'command'}a`}${delay`1000`}${code`51`}` console.log(a)", 0.015, 0.02)tell application "System Events" to keystroke "f" using {option down, shift down}delay 2tell application "System Events" to keystroke "a" using command downdelay 1tell application "System Events" to key code 51
activateApp(
name: string,
): string
Activate an app with the given name.
/* yarn example/activate-app.js */ const app = 'Terminal' const a = activateApp``const b = activateApp`Code` consoleconsole
activate application "Terminal"activate application "Code"
type(
string: string,
delayFrom?: number = 50,
delayTo?: number = 100,
noIndent?: boolean = false,
): string
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.
/* yarn example/type.js */ const t = 'admin\\n' const a = type``const b = type`Though sympathy alone can't alter facts, it can help to make them more bearable. ` console
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 repeatend typetype ("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)
typeInstant(
string: string,
): string
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.
/* yarn example/type-instant.js */ const t = 'Type Instant Text' const a = typeInstant``const b = typeInstant`Do you believe in destiny?That even the powers of time can be altered for a single purpose?` console
on typeInstant(the_string) tell application "System Events" keystroke the_string key code 36 end tellend typetypeInstant ("Type Instant Text")typeInstant ("Do you believe in destiny?That even the powers of time can be altered for a single purpose?")
delay(
time: number,
): string
Delay execution by n
milliseconds.
/* yarn example/delay.js */ const d = 100 const a = delay``const b = delay`500` consoleconsole
delay 0.1delay 0.5
keystroke(
word: string,
commands?: string[],
): string
Send a key stroke. If commands are provided, they will be included.
/* yarn example/keystroke.js */ const a = keystroke`a`const b = keystroke`s` consoleconsole
tell application "System Events" to keystroke "a" using command downtell application "System Events" to keystroke "s" using {alt down, command down}
code(
code: string,
commands?: string[],
): string
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.
/* yarn example/code.js */ const a = code`36`const b = code`36` consoleconsole
tell application "System Events" to key code 36 using command downtell application "System Events" to key code 36 using {command down, shift down}
API Examples
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 |
---|---|
|
|
Example | |
Click to View |
Key Code Reference Table
| ||||||
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: |
CLI
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.
aty -h
|
Accepted Arguments
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 |
CLI Examples
Click to View: aty "echo test\n" -i date -d 100 -a Terminal
![]() |
Copyright
(c) Art Deco 2018