I found keyboard maestro extremely useful, and I think it is quite amazing.
The one action type I did not find was to run an arbitrary PHP script, so I made one.
I chose to convert the parameters and variables to $p{camel case param name} and ${camel case variable name} for convenience (I keep forgetting KMPARAM_ and KMVAR_ , I find them too long).
eg:
A prompt for the user with "User Name" becomes: $userName in the script.
The action can accept either a path to a PHP file, or a PHP script , or both .. or non if you find it useful ...
I've attached my zip file, as well as an example for usage I hope that others will find this useful.
Please let me know if you have any questions about this (or if it does not work for some reason).
I took a closer look at my code, and I actually hard coded /var/tmp in some places , so there is no reason to have that field on the plugin side of things (it will just confuse some people).
I attached a new version that does not need it.
Essentially the script writes a temporary PHP file when entering raw PHP to the text box, and than it executes it, and cleans it when it is done.
Is it possible to assign the content of the system clipboard to a variable and process this one via PHP within your plugin? Or are only variables accepted that are entered via a prompt?
I'm getting "Too Large For Environment" as output as soon as KM input variable exceeds 49 lines.
(I tested it. 49 lines will still work fine. But as soon as it's 50 lines or more, I get this error, no matter the actual content of my script - even if it's just a single "echo".)
Below is the text. If I remove a single character, the output works. For instance, if I remove the "h" (last character of the last word), PHP will run.
KM sends all variables to PHP as environment variables.
When I run the PHP command
var_dump($_ENV);
I see the contents of all my KM variables.
And apparently, there is a maximum size that the sum of these environment variables can have:
All environment variables must live together in a single environment block, which itself has a limit of 32767 characters
It seems to me that one way to fix this would be to delete all KM vars not relevant to the current script. However, I don't want to do that, because these variables serve a purpose in other macros.
A better solution for me would be if I could run PHP and only send those variables to PHP which I actually need for the current script. I just don't know how to do that.
Update: Seems like I fixed it. Used the Macro posted here to display, sort and clear variables. Deleted variables until I was well below the 32k character count. Then it worked.
If anyone from Keyboard Maestro is monitoring, It might be a good feature to add to send only the variables the macro is using to help with this limitation ...
@peternlewis, I agree. This should apply to ALL Execute Script Actions.
IMO, it would be best to provide an option on these actions dealing with which KM Variables to send:
Only those variables used in the Macro.
Only those variables provided in a list
All variables
The default could be "All variables" so that it would not break any existing Macros.
KM could actually pre-parse the script to see which of the KM variables are used in it, and only afterwards pass it on to PHP, with only those variables provided as environment variables. No human intervention needed then.