Skip to main content

Run AppleScripts from Plover

Project description

Plover Run AppleScript

Build Status linting: pylint

This Plover extension plugin contains a command that can load in and run external AppleScript files.

The Problem

The reason this plugin exists is because of pain. The following is an example of how I used to run AppleScripts from my Plover dictionaries to perform some kind of automation task that could only be done on macOS using AppleScript:

"W-D": "{:COMMAND:SHELL:bash -ci 'osascript $STENO_DICTIONARIES/src/command/text/move-one-word-forward.scpt'}"

This solution does the following:

  • uses the Plover Run Shell plugin to run a shell command from Python
  • calls bash in interactive mode (-i) so that the command can see environment variables ($STENO_DICTIONARIES in this case) outside of the Plover environment
  • gets bash to use the osascript command-line tool to load in and run the target compiled AppleScript (.scpt file)

Running AppleScripts is generally slow, and constantly running one-off commands that traverse a stack of Python->Shell->osascript made them even slower.

So, this plugin leverages PyXA to talk directly to Apple's APIs from Python, and keeps a local cache of loaded scripts to avoid needing to re-read in AppleScript files every time a command is run.

The above command now looks like this:

"W-D": "{:COMMAND:APPLESCRIPT:$STENO_DICTIONARIES/src/command/text/move-one-word-forward.scpt}"

The result is that commands at least feel like they run significantly faster, and I'm pretty sure it's because they actually are (but I don't have any hard benchmarks to objectively prove this).

Install

  1. In the Plover application, open the Plugins Manager (either click the Plugins Manager icon, or from the Tools menu, select Plugins Manager).
  2. From the list of plugins, find plover-run-applescript
  3. Click "Install/Update"
  4. When it finishes installing, restart Plover
  5. After re-opening Plover, open the Configuration screen (either click the Configuration icon, or from the main Plover application menu, select Preferences...)
  6. Open the Plugins tab
  7. Check the box next to plover_run_applescript to activate the plugin

How To Use

One-Liners

If your AppleScript is one line long, then you can use it directly in your dictionary entry:

"{:COMMAND:APPLESCRIPT:activate application \"Google Chrome\"}"

AppleScript Files

In your dictionaries, create entry values that look like the following:

"{:COMMAND:APPLESCRIPT:/path/to/your/applescript-file.scpt}"

[!NOTE] You can compile your .applescript files into .scpt files using the osacompile tool:

osacompile -o my-file.scpt my-file.applescript

The path to your AppleScript file can contain a local $ENVIRONMENT_VARIABLE, which will get expanded. For example, if you have a line like the following in your .zshrc file:

export STENO_DICTIONARIES="$HOME/steno/steno-dictionaries"

You can use it in the command:

"{:COMMAND:APPLESCRIPT:$STENO_DICTIONARIES/path/to/applescript-file.scpt}"

[!WARNING] Due to an issue with PyXA, which this plugin relies on to talk to Apple's APIs, your AppleScript files cannot use lists (denoted by curly braces; e.g. {"one", "two"}).

So, if you have code that looks like this:

keystroke "k" using {command down, shift down}

You will have to re-write it out longhand to be able to use it with this plugin, like so:

key down command
key down shift
keystroke "k"
key up shift
key up command

If/when the issue gets fixed, you should be able to use lists again...

Pressing the "Disconnect and reconnect the machine" button on the Plover UI resets the AppleScript script cache. If you make any changes to any AppleScript files, make sure to press it so the file will be re-read in again.

Development

Clone from GitHub with git:

git clone git@github.com:paulfioravanti/plover-run-applescript.git
cd plover-run-applescript

Python Version

Plover's Python environment currently uses version 3.9 (see Plover's workflow_context.yml to confirm the current version).

So, in order to avoid unexpected issues, use your runtime version manager to make sure your local development environment also uses Python 3.9.x.

PyXA Version

This plugin depends on PyXA for all Python-to-AppleScript interoperations. The dependency is currently pinned at version 0.0.9 due to later versions of PyXA using Python 3.10 syntax (match case etc) that is too new for Plover's Python version, and causes syntax errors.

Testing

Tests in this plugin were created with Pytest. Run them with the following command:

pytest

Currently, the only parts able to be tested are ones that do not rely directly on Plover or PyXA.

Linting

Attempts have been made to have at least some kind of code quality baseline with Pylint. Run the linter over the codebase with the following command:

pylint plover_run_applescript

Testing Changes

After making any code changes, install the plugin into Plover with the following command:

plover -s plover_plugins install .

Where plover in the command is a reference to your locally installed version of Plover. See the Invoke Plover from the command line page for details on how to create that reference.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plover_run_applescript-0.2.2.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

plover_run_applescript-0.2.2-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file plover_run_applescript-0.2.2.tar.gz.

File metadata

File hashes

Hashes for plover_run_applescript-0.2.2.tar.gz
Algorithm Hash digest
SHA256 78c01175bf3148c97b751c35844dd1cd81c3dddc6f6ef2f2027bc20fe58d30df
MD5 ea20f728c89b3c8497a42c5b431a662d
BLAKE2b-256 c6fb855d0938ca8e36cc7ea58af2f45d48203ac67c0aa58e940db2d0576f674a

See more details on using hashes here.

File details

Details for the file plover_run_applescript-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for plover_run_applescript-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9237741a4e932491dbf0b213d643d5f019d2cf6f37bfc883856e3901153257a7
MD5 45a2ab61097da66646922f2127eb87d9
BLAKE2b-256 7b3f469ca6c45d7ade44af80d088c1a2647ebfcf12e2039351f2827819dc3c32

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page