• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to get the result of a Python script

New Here ,
Aug 30, 2018 Aug 30, 2018

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

TOPICS
Actions and scripting

Views

2.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Valorous Hero , Sep 04, 2018 Sep 04, 2018

// 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 =

...

Votes

Translate

Translate
Adobe
Valorous Hero ,
Aug 30, 2018 Aug 30, 2018

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 31, 2018 Aug 31, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Aug 31, 2018 Aug 31, 2018

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() ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 04, 2018 Sep 04, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 04, 2018 Sep 04, 2018

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); }

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 05, 2018 Sep 05, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 06, 2018 Sep 06, 2018

Copy link to clipboard

Copied

Just press "Escape".

Untitled-2.png

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 11, 2018 Sep 11, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Sep 11, 2018 Sep 11, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 14, 2018 Sep 14, 2018

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 14, 2018 Sep 14, 2018

Copy link to clipboard

Copied

Why don't you mark r-bin answer as correct solution?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 09, 2021 Aug 09, 2021

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 23, 2023 Apr 23, 2023

Copy link to clipboard

Copied

LATEST

@YahiaboudaH  there's no example on how to use your lib

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines