Package: plover-run-applescript
Plover Run AppleScriptThisPloverextensionplugincontains acommandthat can load in
and run externalAppleScriptfiles.InstallIn the Plover application, open the Plugins Manager (either click the Plugins
Manager icon, or from theToolsmenu, selectPlugins Manager).From the list of plugins, findplover-run-applescriptClick "Install/Update"When it finishes installing, restart PloverAfter re-opening Plover, open the Configuration screen (either click the
Configuration icon, or from the main Plover application menu, selectPreferences...)Open the Plugins tabCheck the box next toplover_run_applescriptto activate the pluginHow To UseOne-LinersIf your AppleScript is one line long, then you can use it directly in your
dictionary entry:"{:COMMAND:APPLESCRIPT:activate application \"Google Chrome\"}"AppleScript FilesIn your dictionaries, create entry values that look like the following:"{:COMMAND:APPLESCRIPT:/path/to/your/applescript-file.scpt}"[!NOTE]
You can compile your.applescriptfiles into.scptfiles using theosacompiletool:osacompile -o my-file.scpt my-file.applescriptThe 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.zshrcfile:exportSTENO_DICTIONARIES="$HOME/steno/steno-dictionaries"You can use it in the command:"{:COMMAND:APPLESCRIPT:$STENO_DICTIONARIES/path/to/applescript-file.scpt}"[!WARNING]
Due to anissue 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{commanddown,shiftdown}You will have to re-write it out longhand to be able to use it with this
plugin, like so:keydowncommandkeydownshiftkeystroke"k"keyupshiftkeyupcommandOr, extract the code you have that uses lists out intoscript libraries.
I wrote about how I did this inSharing AppleScript Handlers.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.The ProblemThe following is an example of how I used to run AppleScripts frommy Plover
dictionariesto perform some kind of automation task that couldonlybe
done onmacOSusing AppleScript:"W-D":"{:COMMAND:SHELL:bash -ci 'osascript $STENO_DICTIONARIES/src/command/text/move-one-word-forward.scpt'}"This solution does the following:uses thePlover Run Shellplugin to run a shell command from Pythoncallsbashininteractive mode(-i) so that the command can seeenvironment variables($STENO_DICTIONARIESin this case) outside of the
Plover environmentgetsbashto use theosascriptcommand-line tool to load in and run
the target compiled AppleScript (.scptfile)Running AppleScripts is generallyslow, and constantly running one-off
commands that traverse a stack ofPython->Shell->osascriptmade themeven
slower.So, this plugin leveragesPyXAto 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 leastfeellike 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).DevelopmentClone from GitHub withgit:git clone git@github.com:paulfioravanti/plover-run-applescript.gitcd plover-run-applescriptPython VersionPlover's Python environment currently uses version 3.9 (see Plover'sworkflow_context.ymlto 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 VersionThis plugin depends onPyXAfor all Python-to-AppleScript interoperations.
The dependency is currently pinned atversion 0.0.9due to later versions
of PyXA using Python 3.10 syntax (match caseetc) that is too new for
Plover's Python version, and causes syntax errors.TestingPytestis used for testing in this plugin.Coverage.pyandpytest-covare used for test coverage, and to run
coverage within PytestPylintis used for code qualityMypyis used for static type checkingCurrently, the only parts able to be tested are ones that do not rely directly
on Plover or PyXA.Run tests, coverage, and linting with the following commands:pytest --cov --cov-report=term-missingpylint plover_run_applescriptmypy plover_run_applescriptTo get a HTML test coverage report:coverage run --module pytestcoverage htmlopen htmlcov/index.htmlIf you getModuleNotFoundError: No module named 'PyXA'errors, then run the
following command to get PyXA available in your test environment:pip install -e ".[test]"Deploying ChangesAfter making any code changes, install the plugin into Plover with the following
command:plover-splover_pluginsinstall.Whereploverin the command is a reference to your locally installed version
of Plover. See theInvoke Plover from the command linepage for details on
how to create that reference.