# script

### apple script

{% hint style="info" %}
> reference:
>
> * [How to automate your keyboard in Mac OS X with AppleScript](https://eastmanreference.com/how-to-automate-your-keyboard-in-mac-os-x-with-applescript)
> * [AppleScript Command Sendkeys](https://apple.stackexchange.com/a/314650/254265)
> * [How can I get rid of this osascript output?](https://stackoverflow.com/a/4147769/2940319)
> * [Running a command in a new Mac OS X Terminal window](https://stackoverflow.com/q/989349)
{% endhint %}

*   sample code:

    ```applescript
    tell application "System Events"
      key code 124 using {shift down, command down} --> Right arrow
      key code 8 using command down --> ⌘-C
      key code {4, 14, 37, 37, 31, 47} --> H, e, l, l, o, .

      keystroke "v" using command down --> ⌘-V
      keystroke "Hello." --> H, e, l, l, o, .
    end tell
    ```

### key code

> \[!NOTE|label:references:]
>
> * [\* Complete list of AppleScript key codes](https://eastmanreference.com/complete-list-of-applescript-key-codes)
> * [Key Codes for Function and Special Keys in Applescript](https://macbiblioblog.blogspot.com/2014/12/key-codes-for-function-and-special-keys.html)

|     KEY     | KEY CODE |   |  KEY | KEY CODE |   | KEY | KEY CODE |
| :---------: | :------: | - | :--: | :------: | - | :-: | :------: |
|     `!1`    |   `18`   |   | `"'` |   `39`   |   | `I` |   `34`   |
|     `@2`    |   `19`   |   | `_-` |   `27`   |   | `O` |   `31`   |
|     `#3`    |   `20`   |   | `+=` |   `24`   |   | `P` |   `35`   |
|     `$4`    |   `21`   |   | `<,` |   `43`   |   | `K` |   `40`   |
|     `%5`    |   `23`   |   | `>.` |   `47`   |   | `L` |   `37`   |
|     `^6`    |   `22`   |   | `?/` |   `44`   |   | `Z` |    `6`   |
|     `&7`    |   `26`   |   |  `N` |   `45`   |   | `X` |    `7`   |
|     `*8`    |   `28`   |   |  `M` |   `46`   |   | `C` |    `8`   |
|     `(9`    |   `25`   |   |  `Q` |   `12`   |   | `V` |    `9`   |
|     `)0`    |   `29`   |   |  `W` |   `13`   |   | `B` |   `11`   |
|   `` ~` ``  |   `50`   |   |  `E` |   `14`   |   | `A` |    `0`   |
|     `{[`    |   `33`   |   |  `R` |   `15`   |   | `S` |    `1`   |
|     `}]`    |   `30`   |   |  `T` |   `17`   |   | `D` |    `2`   |
| `\|</code>` |   `42`   |   |  `Y` |   `16`   |   | `F` |    `3`   |
|     `:;`    |   `41`   |   |  `U` |   `32`   |   | `G` |    `5`   |
|             |          |   |  `H` |    `4`   |   | `J` |   `38`   |

#### function keys

|                  KEY                 | KEY CODE |   |                   KEY                  | KEY CODE |   |                   KEY                  | KEY CODE |
| :----------------------------------: | :------: | - | :------------------------------------: | :------: | - | :------------------------------------: | :------: |
|    <p><code>␣</code><br>space</p>    |   `49`   |   |      <p><code>⇥</code><br>tab</p>      |   `48`   |   |     <p><code>⌫</code><br>delete</p>    |   `51`   |
|    <p><code>⏎</code><br>return</p>   |   `36`   |   | <p><code>⌦</code><br>forwarddelete</p> |   `117`  |   |    <p><code>␊</code><br>linefeed</p>   |   `52`   |
|     <p><code>⎋</code><br>esc</p>     |   `53`   |   |    <p><code>⌘</code><br>command</p>    |   `55`   |   |     <p><code>⇧</code><br>shift</p>     |   `56`   |
|   <p><code>⇪</code><br>capslock</p>  |   `57`   |   |     <p><code>⌥</code><br>option</p>    |   `58`   |   |    <p><code>⌃</code><br>control</p>    |   `59`   |
| <p><code>⇧</code><br>right shift</p> |   `60`   |   |  <p><code>⌥</code><br>right option</p> |   `61`   |   | <p><code>⌃</code><br>right control</p> |   `62`   |
|     <p><code>⇱</code><br>home</p>    |    115   |   |      <p><code>⇲</code><br>end</p>      |    119   |   |    <p><code>⇞</code><br>page up</p>    |    116   |
|  <p><code>⇟</code><br>page down</p>  |    121   |   |   <p><code>←</code><br>left arrow</p>  |    123   |   |  <p><code>→</code><br>right arrow</p>  |    124   |
|  <p><code>↓</code><br>down arrow</p> |    125   |   |    <p><code>↑</code><br>up arrow</p>   |    126   |   |                    -                   |     -    |

#### Fn keys

|  KEY  | KEY CODE |   |  KEY  | KEY CODE |   |  KEY  | KEY CODE |
| :---: | :------: | - | :---: | :------: | - | :---: | :------: |
|  `F1` |   `122`  |   |  `F2` |   `120`  |   |  `F3` |   `99`   |
|  `F4` |   `118`  |   |  `F5` |   `96`   |   |  `F6` |   `97`   |
|  `F7` |   `98`   |   |  `F8` |   `100`  |   |  `F9` |   `101`  |
| `F10` |   `109`  |   | `F11` |   `103`  |   | `F12` |   `111`  |

#### Number pad

| KEY | KEY CODE |   |   KEY   | KEY CODE |   | KEY | KEY CODE |
| :-: | :------: | - | :-----: | :------: | - | :-: | :------: |
| `1` |   `83`   |   |   `2`   |   `84`   |   | `3` |   `85`   |
| `4` |   `86`   |   |   `5`   |   `87`   |   | `6` |   `88`   |
| `7` |   `89`   |   |   `8`   |   `91`   |   | `9` |   `92`   |
| `0` |   `82`   |   |   `*`   |   `67`   |   | `/` |   `75`   |
| `+` |   `69`   |   |   `-`   |   `78`   |   | `=` |   `81`   |
| `.` |   `65`   |   | `clear` |   `71`   |   |  -  |     -    |
