aty

1.1.2 • Public • Published

aty

npm version

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

yarn add -DE aty
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
Harry you're a hacker - writing a program with aty.

Table Of Contents

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 of aty and exec;
  • 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).
import aty, {
  exec, e, w, activateApp, type, typeInstant, delay, keystroke, code,
} from 'aty'
/* 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`
${activateApp`Code`}
${typeInstant`Welcome.`}
${type`${s}${10}${20}`}
  `
  await exec(a)
})()

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 */
import { e, activateApp, type } from 'aty'
 
(async () => {
  await e([
    activateApp`Terminal`,
    type`echo hello world\n`,
  ])
})()

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 */
import { e, w, activateApp, type } from 'aty'
 
(async () => {
  await e([
    activateApp`Terminal`,
    type`echo hello world\n`,
  ])
  await w()
  await e([
    activateApp`Terminal`,
    type`date\n`,
  ])
})()

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 */
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)
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`
${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 2
tell application "System Events" to keystroke "a" using command down
delay 1
tell application "System Events" to key code 51

activateApp(
  name: string,
): string

Activate an app with the given name.

/* yarn example/activate-app.js */
import { activateApp } from 'aty'
 
const app = 'Terminal'
 
const a = activateApp`${app}`
const b = activateApp`Code`
 
console.log(a)
console.log(b)
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 */
import aty, { type } from 'aty'
 
const t = 'admin\\n'
 
const a = type`${t}`
const b = type`Though sympathy alone can't alter facts,
  it can help to make them more bearable.
 
${50}${100}${true}`
 
console.log(aty`
${a}
${b}
`)
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)

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 */
import aty, { typeInstant } from 'aty'
 
const t = 'Type Instant Text'
 
const a = typeInstant`${t}`
const b = typeInstant`Do you believe in destiny?
That even the powers of time can be altered for a single purpose?`
 
console.log(aty`
${a}
${b}
`)
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?")

delay(
  time: number,
): string

Delay execution by n milliseconds.

/* yarn example/delay.js */
import { delay } from 'aty'
 
const d = 100
 
const a = delay`${d}`
const b = delay`500`
 
console.log(a)
console.log(b)
delay 0.1
delay 0.5

keystroke(
  word: string,
  commands?: string[],
): string

Send a key stroke. If commands are provided, they will be included.

/* yarn example/keystroke.js */
import { keystroke } from 'aty'
 
const a = keystroke`a${'command'}`
const b = keystroke`s${'alt'}${'command'}`
 
console.log(a)
console.log(b)
tell application "System Events" to keystroke "a" using command down
tell 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 */
import { code } from 'aty'
 
const a = code`36${'command'}`
const b = code`36${'command'}${'shift'}`
 
console.log(a)
console.log(b)
tell application "System Events" to key code 36 using command down
tell 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 CodeGenerated Code
/* yarn scripts/alamode.js */
import { e, w, activateApp,
  type } from '../src'
 
(async () => {
  await e([
    activateApp`Terminal`,
    type`mnp alamode -c\n`,
  ])
  await w()
  await e([
    activateApp`Terminal`,
    type`expensive alamode\n`,
  ])
  await w()
  await e([
    activateApp`Terminal`,
    type`expensive alamode.app -r\n`,
  ])
  await w()
  await e([
    activateApp`Terminal`,
    type`y\n`,
  ])
})()
# mnp-expensive.scpt
 
set type to "aty:Type.scpt" as alias
 
 
 
activate application "Terminal"
type ("mnp alamode -c\n", 0.05, 0.1)
 
 
# waits for enter
activate application "Terminal"
type ("expensive alamode\n", 0.05, 0.1)
 
 
# waits for enter
activate application "Terminal"
type ("expensive alamode.app -r\n", 0.05, 0.1)
 
 
# waits for enter
activate application "Terminal"
type ("y\n", 0.05, 0.1)
 
#
Example
Click to View

Quick package and domain names check, registering a domain.

Key Code Reference Table

This table can be used for a quick look up of a key code.
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
Activate an app with AppleScript and type some text there.
If -i (--instant) is given, it will be instantly typed  before
the actual text. 
 
  aty "test text" -a appToActivate -i "instant text" [-d 10] [-m 50] [-hv]
 
    text           The text to type.
    -a, --app      The app which to open.
    -i, --instant  Text to type instantly, before text
    -d, --minDelay The minimum delay with which to type.
    -m, --maxDelay The maximum delay with which to type.
    -h, --help     Display help and quit.
    -v, --version  Show version.
 
  Example:
 
    aty "echo test\n" -i "date" -a iTerm

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
Alt: Running aty executable.

Copyright

(c) Art Deco 2018

Package Sidebar

Install

npm i aty

Weekly Downloads

8

Version

1.1.2

License

MIT

Unpacked Size

43 kB

Total Files

16

Last publish

Collaborators

  • zvr