Copy link to clipboard
Copied
Hi,
I need to get some data by executing a Python script from a JS script that would be then used by that same JS script.
Any idea on how I could get the result of an executed Python script? Or any advice of what would be the best approach for doing this?
Thanks,
Jeremy
1 Correct answer
...// file = your bat file
file.execute();
//At the end of the Python script execution, you must remove the bat-file from the command in the bat-file itself or in the Python script.
var ok = false;
try {
while (1)
{
if (!file.exists) { ok = true; break; }
set_performance("stepByStep");
}
}
catch(e) { }
set_performance("accelerated");
if (!ok) throw("Error") ;
// in C:/PATH/result_file, your Python script should write some code in js format.
// for example
// var data1 =
Explore related tutorials & articles
Copy link to clipboard
Copied
How do you run a Python script from a JSX file? I do not know anything about Python scripts. If you can set some environment variable in Python, then you can try to read it in JSX script using $.getenv().
Copy link to clipboard
Copied
I write the command ('python python_file_path args') into a bat file that I run with the file.execute() method.
I already did that for other scripts and this works well but I'm looking for a way to get the result of the Python script.
I could try the environment variable approach but I'm not quite sure if I would be limited in the type of data I could set it with.
Copy link to clipboard
Copied
You first try to pass at least a string.
And the question. How do you find out in the script that Python completed the job if you use file.execute() ?
Copy link to clipboard
Copied
I'll definitely try to pass a string at first indeed to see if I can get the result at least.
But then, the idea is to get a dictionary of data I will request from a database, so I'll also need to find out how the file.execute() of the Python script is completed and that's why I created this thread hoping that someone could help on this.
Copy link to clipboard
Copied
// file = your bat file
file.execute();
//At the end of the Python script execution, you must remove the bat-file from the command in the bat-file itself or in the Python script.
var ok = false;
try {
while (1)
{
if (!file.exists) { ok = true; break; }
set_performance("stepByStep");
}
}
catch(e) { }
set_performance("accelerated");
if (!ok) throw("Error") ;
// in C:/PATH/result_file, your Python script should write some code in js format.
// for example
// var data1 = 1.234;
// var data2 = [1.234, 2345, 5,6];
// var data3 = ["string1", 2345, false];
$.evalFile("C:/PATH/result_file");
File("C:/PATH/result_file").remove();
// Now you can use data1, data2 and data3 in your jsx script
function set_performance(mode)
{
try {
var d1 = new ActionDescriptor();
var d2 = new ActionDescriptor();
var r1 = new ActionReference();
r1.putProperty( charIDToTypeID( "Prpr" ), charIDToTypeID( "PbkO" ) );
r1.putEnumerated( charIDToTypeID( "capp" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
d1.putReference( charIDToTypeID( "null" ), r1 );
d2.putEnumerated( stringIDToTypeID( "performance" ), stringIDToTypeID( "performance" ), stringIDToTypeID( mode ) );
d1.putObject( charIDToTypeID( "T " ), charIDToTypeID( "PbkO" ), d2 );
executeAction( charIDToTypeID( "setd" ), d1, DialogModes.NO );
}
catch (e) { throw(e); }
}
Copy link to clipboard
Copied
Removing the bat file from the Python script once the process is completed was a good idea.
Since I'm getting that data in a dictionary, I finally ended up writing it into a JSON file which I'm more familiar with and already done as well.
With all of this in place, I could show the window of my JS script after getting that data by using a while loop.
Everything works fine but I have one more thing I noticed while doing some tests that I wanted to ask about:
How can I break the while loop if there's an error with the Python script before the bat file is removed?
If this happens, the while loop won't stop and will freeze Photoshop.
And I actually don't quite understand how your line "21. if (!ok) throw("Error");" can be called if the "ok" variable is not switched to true.
Copy link to clipboard
Copied
Just press "Escape".
You can also insert a test to press a certain key.
var ok = false;
var f1 = false;
try {
while (1)
{
//if (!file.exists) { ok = true; break; }
if (ScriptUI.environment.keyboardState.keyName == "F1") { f1 = true; break; }
set_performance("stepByStep");
}
}
catch(e) { }
set_performance("accelerated");
if (f1) alert("F1 pressed");
if (!ok) throw("Error") ;
Copy link to clipboard
Copied
Pressing "Escape" does't break the while look for some reason, even tried with a simple while loop script and doesn't work either and I don't really know why.
Also, if I want to use the ScriptUI key pressed event method I would need to create and show the window to make it work, this is what I noticed while doing some tests and this is not really what I want as the idea is to get the data before generating the window.
I'm not quite sure how I could force break the while loop or find a way to catch any error from the python script.
Copy link to clipboard
Copied
Everything works in CS6 and CC2018. You must use the set_performance() function in the loop, rather than invent your own cycles. To use ScriptUI.environment, you do not need to create an interface or windows. This is a global object.
Copy link to clipboard
Copied
So I did use the set_performance() function and Photoshop no longer freezes when getting an error from the Python script process.
I still do check if the batch file doesn't exists to get me a statement of the that process going through and be able to continue the JScript execution.
I also ended up using the ScriptUI.environment.keyboardState.keyName with no specific name so that the user can press any key for breaking the loop, because the cmd window of the batch file that executes the Python script will display the message "Press any key to continue..." in case of getting an error from the Python script. This way, pressing any key will break the while loop and allow the user to quit the application properly which was not the case when the while loop was not broken.
Thanks again for your help, really appreciated.
Copy link to clipboard
Copied
Why don't you mark r-bin answer as correct solution?
Copy link to clipboard
Copied
I know that this is kinda 3 years late, but I developed a small library that takes care of all this headache.
And it's opensource. Take a look at this: PYJSX
Copy link to clipboard
Copied
@YahiaboudaH there's no example on how to use your lib

