Hi Chris,
Thanks for considering it.
I had not thought of it before, but after seeing your reply, I did some tests.
Pass KM Variables to PHP
As far as I know, there are three ways to pass KM variables to PHP.
-
Pass variables through command line.
I've posted the method here: Pass KM Variables to PHP in Command Line - Tips & Tutorials - Keyboard Maestro Discourse -
use
$_ENV, as shown in the OP here.
$phpVar = $_ENV["KMVAR_VarName"];
- use getenv() function.
$phpVar = getenv("KMVAR_VarName");
- use osascript.
$phpVar = exec('osascript -e \'tell application "Keyboard Maestro Engine" to getvariable "VarName"\'');
Methods 2-4 are used inside the PHP code.
Set KM Variables in PHP
Now, setting KM variables in PHP are tricky.
I'll give my conclusion here first: If we want to set KM variables, the proper way is to use osascript.
To demonstrate the point, I've made a PHP script:
#!/usr/bin/php
<?php
//###########################################//
// This PHP code demonstrates how to get variables from KM in PHP
//-----------------------------------------------------------------------//
// Beside passing via Command Line arguments,
// there are three ways to get the value of KM variable:
// 1. use $_ENV["KMVAR_VarName"]
// 2. use getenv("KMVAR_VarName")
// 3. use osascript
//-----------------------------------------------------------------------//
// Initially, all three should get the same value.
// However, they are actually independent from each other.
// We will see it clearly once we set new value to any of them.
//###########################################//
//###########################################//
// assume the value of VarName in KM is "initial value";
//###########################################//
echo "Before setting any variables in PHP \n";
echo "\n".'-Variable value via $_ENV:'."\n";
echo $_ENV["KMVAR_VarName"];
echo "\n".'-Variable value var getenv():'."\n";
echo getenv("KMVAR_VarName");
echo "\n".'-Variable value via osascript:'."\n";
echo exec('osascript -e \'tell application "Keyboard Maestro Engine" to getvariable "VarName"\'');
echo "\n\nEND\n\n\n";
// All will output "initial value"
//###########################################//
$_ENV["KMVAR_VarName"] = "Value set via ENV";
//-----------------------------------------------------------------------//
// this will NOT change the value of VarName in KM.
//-----------------------------------------------------------------------//
echo "After setting variable via '\$_ENV' \n";
echo "\n".'-Variable value via $_ENV:'."\n";
echo $_ENV["KMVAR_VarName"];
echo "\n".'-Variable value var getenv():'."\n";
echo getenv("KMVAR_VarName");
echo "\n".'-Variable value via osascript:'."\n";
echo exec('osascript -e \'tell application "Keyboard Maestro Engine" to getvariable "VarName"\'');
echo "\n\nEND\n\n\n";
//###########################################//
// Only $_ENV["KMVAR_VarName"] is affected.
//###########################################//
$myVar = "Value set via putenv";
putenv("KMVAR_VarName=$myVar");
//-----------------------------------------------------------------------//
// this will NOT change the value of VarName in KM either.
//-----------------------------------------------------------------------//
echo "After setting variable via 'putenv()' \n";
echo "\n".'-Variable value via $_ENV:'."\n";
echo $_ENV["KMVAR_VarName"];
echo "\n".'-Variable value var getenv():'."\n";
echo getenv("KMVAR_VarName");
echo "\n".'-Variable value via osascript:'."\n";
echo exec('osascript -e \'tell application "Keyboard Maestro Engine" to getvariable "VarName"\'');
echo "\n\nEND\n\n\n";
//###########################################//
// Only getenv("KMVAR_VarName") is affected.
//###########################################//
// use osascript to set KM variables.
exec('osascript -e \'tell application "Keyboard Maestro Engine" to setvariable "VarName" to "new value"\'');
//-----------------------------------------------------------------------//
// This will change the variable value in KM.
// But $ENV and getenv remain the same values as before the osascript
//###########################################//
echo "After setting variable via osascript \n";
echo "\n".'-Variable value via $_ENV:'."\n";
echo $_ENV["KMVAR_VarName"];
echo "\n".'-Variable value var getenv():'."\n";
echo getenv("KMVAR_VarName");
echo "\n".'-Variable value via osascript:'."\n";
echo exec('osascript -e \'tell application "Keyboard Maestro Engine" to getvariable "VarName"\'');
echo "\n\nEND\n\n";
// Only osascript getvariable is affected.
//###########################################//
// Conclusion on $_ENV, getenv(), and osascript in PHP:
// Before changing the variable values, all three methods will get the KM variable values.
// But they are all independent from each other.
// Changing variable values of one will not change that of the other two.
// If we want to set KM variables, the proper way is to use osascript.
//###########################################//
?>
Output:
