This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
action:Execute_a_Shell_Script [2018/04/13 16:48] JMichaelTX [Path in Shell Scripts] |
action:Execute_a_Shell_Script [2019/05/03 13:13] JMichaelTX ADD example of Bash quoting |
||
---|---|---|---|
Line 11: | Line 11: | ||
==== Using Keyboard Maestro Variables ==== | ==== Using Keyboard Maestro Variables ==== | ||
+ | |||
+ | === Most Languages like bash, perl, ruby === | ||
In your script, use this format: `$KMVAR_[variable_name]` | In your script, use this format: `$KMVAR_[variable_name]` | ||
Line 16: | Line 18: | ||
where //[variable‗name]// is the name of your Keyboard Maestro Variable, but with spaces in the name replaced with underscores (`‗`). | where //[variable‗name]// is the name of your Keyboard Maestro Variable, but with spaces in the name replaced with underscores (`‗`). | ||
- | For example: `File Name` would be formated as `$KMVAR‗File‗Name`. | + | For example: `File Name` would be formatted as `$KMVAR‗File‗Name`. |
If your variable already has underscores, you can use it as is. | If your variable already has underscores, you can use it as is. | ||
{{:action:km7-shell-script.png?nolink|}} | {{:action:km7-shell-script.png?nolink|}} | ||
+ | |||
+ | === Python === | ||
+ | |||
+ | Accessing Keyboard Maestro Variables in Python is somewhat different. Here is an example: | ||
+ | |||
+ | ```python | ||
+ | #!/usr/bin/env python | ||
+ | import os | ||
+ | print os.environ['KMVAR_local_DataStr'].count(os.environ['KMVAR_local_CharToCount']) | ||
+ | ``` | ||
+ | |||
+ | See the [[#Forum|Forum]] section below for more Python examples. | ||
+ | |||
+ | --- | ||
Note that you can only read these environment variables. You cannot write to them (or more accurately, you can write to them but that will not change the Keyboard Maestro variables that they were created from). | Note that you can only read these environment variables. You cannot write to them (or more accurately, you can write to them but that will not change the Keyboard Maestro variables that they were created from). | ||
Line 31: | Line 47: | ||
``` | ``` | ||
- | This must be a full path. **If the path starts with a tilde (~)**, then you must first convert it to a full path using the [[action:Filter | Filter, Expand tilde (~) paths]] Action BEFORE the Execute Shell Script Action. | + | **Consider These Guidelines** |
+ | |||
+ | * This must be a full path. **If the path starts with a tilde (~)**, then you must first convert it to a full path using the [[action:Filter | Filter, Expand tilde (~) paths]] Action BEFORE the Execute Shell Script Action. | ||
+ | * Like all Bash variables, the Keyboard Maestro Variable must _not_ be between single quotes (`'`) in order for the variable to be expanded to / replaced by its value. So, for example, **this will _not_ work**: | ||
+ | |||
+ | ```bash | ||
+ | # 🚫 Does NOT Work | ||
+ | /usr/local/bin/emacsclient -e '(w3m-browse-url "$KMVAR_SafariURL")' | ||
+ | ``` | ||
+ | because although the Variable is between double-quotes, it is in a string that is between single-quotes. | ||
+ | |||
+ | * So, to handle this case, you can use something like this: | ||
+ | |||
+ | ```bash | ||
+ | # ✅ This WORKS | ||
+ | /usr/local/bin/emacsclient -e '(w3m-browse-url "'"$KMVAR_SafariURL"'")' | ||
+ | ``` | ||
+ | One key Bash feature that makes this work is that when two quoted strings are adjacent, they will be concatenated. So after the Variable is expanded, the command string might look like this: | ||
+ | |||
+ | ```bash | ||
+ | /usr/local/bin/emacsclient -e '(w3m-browse-url "https://www.apple.com")' | ||
+ | ``` | ||
+ | |||
+ | For more info, see these discussions in the Keyboard Maestro Forum: | ||
+ | * [[https://forum.keyboardmaestro.com/t/png-metadata-comment/6375/29| How To Quote Paths by @ccstone]]. | ||
+ | * [[https://forum.keyboardmaestro.com/t/trouble-expanding-variable-in-shell-script/13664/2?u=jmichaeltx | Trouble Expanding Variable in Shell Script by @PeterNLewis]] | ||
+ | |||
+ | --- | ||
+ | |||
+ | ==== UTF-8 and Non-ASCII Characters ==== | ||
- | For more info, see an excellent, detailed discussion of [[https://forum.keyboardmaestro.com/t/png-metadata-comment/6375/29| How To Quote Paths by @ccstone]]. | + | If you are dealing with non-ASCII characters, you probably want to set the `LC_ALL` environment variable to UTF8, which you can do by setting the Keyboard Maestro variable `ENV_LC_ALL` to “en_US.UTF-8”. |
==== Action Gear ==== | ==== Action Gear ==== |