PHP Action Plugin

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 ... :smile:

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).

Macro Example:

t.php from the macro:
t.php.zip (199 Bytes)

The PHP script plugin:
PHP Script.zip (11.4 KB)

Nice one. Thanks for sharing!

Nice plugin! But what’s the purpose of the ‘Temporary Folder’ variable?

That’s a very good question ! :slight_smile:

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.

Cheers

Alon
PHP Script.zip (10.4 KB)

ok, this is a new version (yet again), fixed a minor bug …

Cheers

Alon
PHP Script.zip (10.4 KB)

Ok, thank you for the clarification but the new version no longer works :sob:

Well, it took me a "bit" of time. I retested the script, I am not sure where the issue is.

Just in case, I will re-upload the plugin.

Also, please do not forget to run the following command on your keyboard maestro after re-applying the new plugin:

tell application "Keyboard Maestro" to reload
tell application "Keyboard Maestro Engine" to reload

It is described here: http://www.keyboardmaestro.com/documentation/6/pluginactions.html

Please let me know if you still find issues. If you do, please be specific with what you try to achieve.PHP Script.zip (9.6 KB)

Thank you very much. It works now. I probably forgot to reload Keyboard Maestro the first time. Great plugin!

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?

Yes, any variable defined in Keyboard Maestro should work with the plugin.

Did you get this sorted?
I’ve done a dump of get_defined_vars() and there is no reference in there to get the current clipboard.
How is this done?

Take a look at this example:

What I do in the example below in keyboard maestro, is to get the content of the clipboard, and assign it to a variable named "test".

In the PHP script, I am simply "echoing" the variable "$v_test".

Hopefully that makes more sense.

Let me know

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".)

I just tried it with very simple input (60 lines). Seems to work.
Maybe the limit is based on something else.

Is it ok for you to share the full content you try to push through, or even create some test input for me to try?

Also, where do you see the error message?

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.

According to https://mothereff.in/byte-counter, the breaking point seems to be 6,723 bytes. One byte more and I get "too large for environment".

Weird, isn't it?

This is the text:

test.txt.zip (1.9 KB)

I think I know what the problem is.

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.

That's great, good job! :slight_smile:

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 ...

1 Like

@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.

1 Like

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.

1 Like