This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
action:Execute_a_Shell_Script [2021/10/14 05:08] peternlewis [Execute a Shell Script] |
action:Execute_a_Shell_Script [2023/09/18 02:35] peternlewis [Output of Results] |
||
---|---|---|---|
Line 41: | Line 41: | ||
==== Using Keyboard Maestro Variables ==== | ==== Using Keyboard Maestro Variables ==== | ||
- | === Most Languages like bash, perl, ruby === | + | Keyboard Maestro variables are included in the environment of the script, with the prefix `KMVAR_` and spaces in the name replaced with underscores (‗), |
+ | |||
+ | By default, all variables are included, but you can select No Variables, or specific variables as desired using the popup menu next to the script (v11.0+). | ||
+ | |||
+ | === Most Shell Languages like bash === | ||
In your script, use this format $KMVAR‗*VARIABLE‗NAME* where *VARIABLE‗NAME* is the name of your Keyboard Maestro Variable, but with spaces in the name replaced with underscores (‗). | In your script, use this format $KMVAR‗*VARIABLE‗NAME* where *VARIABLE‗NAME* is the name of your Keyboard Maestro Variable, but with spaces in the name replaced with underscores (‗). | ||
Line 131: | Line 135: | ||
The results of a shell script can be: | The results of a shell script can be: | ||
- | * Ignored. | + | {{page>include:ScriptOutput#}} |
- | * Displayed in a floating window. | + | |
- | * Displayed briefly in a Notification. | + | |
- | * Displayed large across the screen. | + | |
- | * Typed in to the current selection. | + | |
- | * Pasted in to the current selection. | + | |
- | * Saved to a variable. | + | |
- | * Appended to a variable (v9.0+). | + | |
- | * Saved to the system or a Named Clipboard. | + | |
- | * Asynchronously ignored — the action runs while the macro continues on. | + | |
- | If you want to return multiple values from a script, then you can either use AppleScript (via `osascript`) to [[manual:Scripting#AppleScript|set Keyboard Maestro variables]]. | + | If the output is going to a clipboard or a file, the downloaded data can be an image (v11.0+). |
+ | |||
+ | If you want to return multiple values from a script, then you can use AppleScript (via `osascript`) to [[manual:Scripting#AppleScript|set Keyboard Maestro variables]]. | ||
===== Examples ===== | ===== Examples ===== | ||
Line 247: | Line 244: | ||
Any Keyboard Maestro variable that starts with `ENV_` will automatically be set as the as the corresponding environment variable (without the usual `KMVAR_` prefix). So if you set your path in the `ENV_PATH` Keyboard Maestro variable, the `PATH` environment variable will be set from it. | Any Keyboard Maestro variable that starts with `ENV_` will automatically be set as the as the corresponding environment variable (without the usual `KMVAR_` prefix). So if you set your path in the `ENV_PATH` Keyboard Maestro variable, the `PATH` environment variable will be set from it. | ||
- | You can set this Variable manually in the [[manual:manual/Preferences#Variables_Preferences|Variable Preferences Panel]] in Keyboard Maestro. Once the `ENV_PATH` variable is set, it will remain in your Keyboard Maestro variable until/unless you delete it, and remain available to every Execute a Shell Script action that you might use in the future. So you don’t need to set it in every Macro. | + | You can set this Variable manually in the [[manual:Preferences#Variables_Preferences|Variable Preferences Panel]] in Keyboard Maestro. Once the `ENV_PATH` variable is set, it will remain in your Keyboard Maestro variable until/unless you delete it, and remain available to every Execute a Shell Script action that you might use in the future. So you don’t need to set it in every Macro. |
If your tool requires other environment variables to be set you can set them as well by creating an appropriate Keyboard Maestro variable with `ENV_` at the front. | If your tool requires other environment variables to be set you can set them as well by creating an appropriate Keyboard Maestro variable with `ENV_` at the front. | ||
For example, if you want to use [[https://www.perl.org/ | Perl]] with a custom library search path in the Execute a Shell Script action, you can also create a Keyboard Maestro variable `ENV_PERL5LIB` that will set the [[http://perldoc.perl.org/perlrun.html#ENVIRONMENT|PERL5LIB environment variable]]. | For example, if you want to use [[https://www.perl.org/ | Perl]] with a custom library search path in the Execute a Shell Script action, you can also create a Keyboard Maestro variable `ENV_PERL5LIB` that will set the [[http://perldoc.perl.org/perlrun.html#ENVIRONMENT|PERL5LIB environment variable]]. | ||
+ | |||
+ | ===== Working Directory ===== | ||
+ | |||
+ | The working directory for any executed scripts will be set from the Keyboard Maestro `ENV_PWD` directory (v10.0+), or default to the root directory (`/`). | ||
+ | |||
===== Execute Script From Other Apps ===== | ===== Execute Script From Other Apps ===== | ||
Line 294: | Line 296: | ||
=== Actions === | === Actions === | ||
+ | * [[action:Execute_a_Swift_Script|Execute a Swift Script]] | ||
* [[action:Execute an AppleScript|Execute an AppleScript]] | * [[action:Execute an AppleScript|Execute an AppleScript]] | ||
* [[action:Execute an Automator Workflow|Execute an Automator Workflow]] | * [[action:Execute an Automator Workflow|Execute an Automator Workflow]] |