Lokyr
Documentation

Automation

On this page

macOS · Since 1.6 · on by default · the automation plugin · [plugins.automation] enabled

Scripts and shortcuts can drive Lokyr: open windows, tabs and splits in a folder, run a command in them, send them text, switch sessions, and read what a terminal shows. AppleScript sees Lokyr's windows, their tabs and each tab's terminals, and the Shortcuts app has Lokyr's actions, which Spotlight can run too.

Try it

Run this in a terminal:

osascript -e 'tell application "Lokyr" to create window in folder "/tmp" with command "ls"'

A window opens in /tmp, and its shell runs ls. The first time, macOS may ask whether the app where you ran osascript may control Lokyr: click OK.

Then open the Shortcuts app, make a new shortcut, and type Lokyr in the search field of its actions. Add New Tab, type ls in its Command field, and click the run button: a tab opens in Lokyr's window in front and runs ls.

What it gives you

Script Lokyr with AppleScript

Lokyr's dictionary has three kinds of objects, each inside the one before:

ObjectPropertiesInside
windowid, namethe application, the one in front first
tabid, nameits window, in their order
terminalid, name, working directory, contents, selected textits tab: the tab's panes

Its commands:

CommandWhat it doesResult
create window [with command text] [in folder text] [with environment list]Opens a windowthe window
create tab [in window] [with command text] [in folder text] [with environment list]Opens a tab, in the window in front unless you give onethe tab
split [terminal] [direction right/left/down/up] [with command text] [in folder text] [with environment list]Splits a terminal, the focused one unless you give one, on the right unless you give a directionthe new terminal
input text text [to terminal]Sends text to a terminal, the focused one unless you give one, as if you typed it
execute text [to terminal]Runs a command line in a terminal's shell, the focused one unless you give one, at its prompt
send key text [to terminal]Presses a key in a terminal, the focused one unless you give one, as you press it in Lokyr, such as "ctrl+c" or "cmd+a"
focus window/tab/terminalBrings it forward
close window/tab/terminalCloses it
perform action text [with arguments record]Runs one of Lokyr's commands by its name, with its arguments

A folder is a path from / or ~. A command line runs in the shell, so it can hold pipes and variables, and it shows in the shell's history. with environment takes a list of environment variables, each NAME=value, which the new shell starts with:

tell application "Lokyr"
    create tab with environment {"AWS_PROFILE=staging", "EDITOR=vim"}
end tell

This script opens a window, splits it, runs a command in the new terminal and reads what it shows:

tell application "Lokyr"
    set w to create window in folder "/tmp"
    set t to split terminal 1 of tab 1 of w direction down
    delay 1
    execute "echo hello" to t
    delay 1
    get contents of t
end tell

perform action takes the names and arguments of the commands' reference. It runs the command as if you chose it in Lokyr, on the window with the focus, so bring Lokyr forward first with activate:

tell application "Lokyr"
    activate
    perform action "splits.split" with arguments {direction:"right"}
    perform action "tabs.goto" with arguments {index:1}
end tell

A field named after one of AppleScript's own words, such as to, goes between bars: {|to|:"bottom"}.

AppleScript's count and exists work on them too, such as count terminals of tab 1 of window 1 or if exists window 2.

Script Editor shows the whole dictionary: choose File › Open Dictionary… and choose Lokyr.

Use Lokyr's actions in Shortcuts

ActionParametersWhat it does
New WindowFolder, Command, Environment VariablesOpens a window
New TabFolder, Command, Environment VariablesOpens a tab in Lokyr's window in front
Split PaneTerminal, Direction, Folder, Command, Environment VariablesSplits a terminal, the focused one unless you choose one
Run Command LineCommand Line, TerminalRuns a command line, such as ls -la, in a terminal's shell, at its prompt
Send TextText, TerminalSends text to a terminal, as if you typed it
Send Mouse Button EventAction (Click, Press, Release), Button, Column, Row, Modifiers, TerminalClicks, presses or releases a mouse button over a cell of a terminal
Send Mouse Position EventColumn, Row, Button Held, Modifiers, TerminalMoves the mouse's pointer over a cell of a terminal
Send Mouse Scroll EventDirection, Steps, Column, Row, Modifiers, TerminalTurns the mouse's wheel over a terminal
Send Keyboard EventKey, Modifiers, TerminalPresses a key in a terminal as you press it in Lokyr, such as A with Command to select all, or C with Control to interrupt a program
Focus TerminalTerminalBrings a terminal forward, with its window and its tab
Close TerminalTerminalCloses a terminal
Find Terminalfilters on Title, Folder, Tab ID and Window IDReturns the terminals that match, such as those whose folder contains src
Get Details of TerminalDetail (Title, Folder, Tab ID, Window ID), TerminalReturns a detail of a terminal: its title, its folder, its tab's ID or its window's ID
Open Quick TerminalShows the quick terminal, opening it first
Close Quick TerminalHides the quick terminal, its shell still running
Run CommandCommand, ArgumentsRuns one of Lokyr's commands by its name, such as tabs.next
Run Palette CommandCommand, ArgumentsRuns an entry of the command palette, chosen from its list, such as Split Right, with the entry's arguments
Switch SessionSessionSwitches to the session of that name, and opens a new session of that name when there is none
Open LayoutLayoutOpens a saved layout as a new session
Get Terminal TextTerminalReturns the text that a terminal shows
Get Selected TextTerminalReturns the text of a terminal's selection

Allow an app to control Lokyr

An app that controls Lokyr can type into your terminals, run commands there, press keys and read what they show, which may hold passwords and other secrets. The first time a script or a shortcut sends Lokyr a request, of any kind, Lokyr asks you whether to allow the app that runs it:

Lokyr tells apps apart by their code signature, not by the names that they give, so another app can't take an allowed app's answer. It remembers your answer for each app, from one start to the next.

While the question waits for your answer, a script's request fails with "Lokyr asks whether … may control it": answer, then run the script again. A shortcut waits for your answer for close to two minutes, then fails, and your later answer doesn't run it: run the shortcut again.

Important

Allow only the apps you trust: an app that controls Lokyr can run any command as you. For an app of another developer's, macOS asks you too before its first command, and System Settings › Privacy & Security › Automation lists the apps you allowed there.

Turn it off

Turn the automation plugin off, in the settings window's Plugins page, or in your config:

[plugins.automation]
enabled = false   # every script and shortcut fails, with a message that says why

Settings

How it works

Limits

Troubleshooting

"Not authorized to send Apple events to Lokyr. (-1743)"

Why: you clicked Don't Allow when macOS asked whether the app that runs the script may control Lokyr.

Fix: open System Settings › Privacy & Security › Automation, find the app that runs the script, such as Terminal or Script Editor, and turn Lokyr on under it.

Check: run the script again: Lokyr opens a window.

osascript -e 'tell application "Lokyr" to create window'

"may not control Lokyr"

Why: you clicked Don't Allow when Lokyr asked whether the app may control it, and Lokyr remembers it.

Fix: quit Lokyr, then remove the file where it keeps the answers. Lokyr asks again about every app.

rm ~/Library/Application\ Support/lokyr/state/plugins/automation.json

"the shell of the pane … is not at its prompt"

Why: execute and Run Command Line run a line at the shell's prompt alone, and the shell was running a program, or shell integration is off, so that Lokyr can't see its prompt.

Fix: wait for the program to end, or turn shell integration on. To type into a program, use input text or Send Text, with a return character at the end.

perform action does nothing

Why: Lokyr was behind another app, so no window had the focus for the command to act on. Or the command failed as it ran.

Fix: add activate before perform action. When it still does nothing, read the card in Lokyr's window, or the end of its log:

tail ~/Library/Logs/Lokyr/lokyr.log

"Lokyr's automation did not answer in time"

Why: Lokyr waits 10 seconds at most for each request, and a shortcut's request almost two minutes while Lokyr's question waits for your answer.

Fix: run the script or the shortcut again, and answer the question in time.

"Lokyr's automation is off"

Why: the automation plugin is turned off.

Fix: turn it on in the settings window's Plugins page, or remove enabled = false from your config's [plugins.automation].

Shortcuts doesn't list Lokyr's actions

Why: Shortcuts reads the actions of the copy of Lokyr that macOS registered, when that copy first opens. Another copy, such as an older one in your Downloads folder, may hold the registration.

Fix: quit Lokyr, remove the other copies, and open Lokyr from your Applications folder.

Check: type Lokyr in the search field of the Shortcuts app's actions: the list shows New Window.

See also