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

How to include .js file from enviroment variable

Participant ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

I try to switch from dev mode to live mode by changing a system enviroment variable with the root path of the scripts. Both for the extension jsx file and local PS scripts I use the...

#include (anotherJsFile.js);

syntax to include js files on the network, this works as expected.

But when I want to use a system enviroment variable to create the script path, and insert it into the include, it's undefined. I think the PS JS engine first executes the includes, before any variable is set.

So I don't see how I could load a script based on a path where any logic is requered first.

 

Any ideas how I can first build the path, and then include it?

TOPICS
Actions and scripting , SDK , Windows

Views

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

Participant , Feb 27, 2023 Feb 27, 2023

I managed to get both scenario's working...

 

1) If you want a script being included from within a jsx file that is dependend on any logic for path generation, you can use this methode instead of the "# include" methode...

var rootPath= $.getenv("rootPath");
var scriptPath = rootPath + "Photoshop/script.jsx";

$.evalFile(File(scriptPath));

This seems to work identical to the "# include" methode, except it isn't executed bofore any logic is executed.

 

2) If you want a logic dependent jsx file path inc

...

Votes

Translate

Translate
Participant ,
Feb 22, 2023 Feb 22, 2023

Copy link to clipboard

Copied

What I had working was...

#include "/X/Tools/Photoshop/Remote Scripts/core.jsx"

What I was hoping for to work...

var toolsRoot = $.getenv("ToolsRoot");
var coreScript = toolsRoot + "Photoshop/Remote Scripts/core.jsx";

#include coreScript

It results in a failure at the include line, coreScript is undefined, but when I remove the include line, and place an alert to see the file path, it's correct. It just tries to run the include before any logic is done.

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
Contributor ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Just a guess... Have you tried wrapping those lines in an if-statement so they are forced to be executed in order?

var stepOne, stepTwo = true;

if (stepOne) {
    var toolsRoot = $.getenv("ToolsRoot");
    if (stepTwo) {
        var coreScript = toolsRoot + "Photoshop/Remote Scripts/core.jsx";
        #include coreScript
    };
};

 

Alternatively, try adding a delay by using this in between the steps:

var timeInMilliSeconds = 100;


var toolsRoot = $.getenv("ToolsRoot");
$.sleep(timeInMilliSeconds);
var coreScript = toolsRoot + "Photoshop/Remote Scripts/core.jsx";
$.sleep(timeInMilliSeconds);
#include coreScript

 

 

They might not be pretty or good practise, but worth a shot. 🙂

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
Participant ,
Feb 23, 2023 Feb 23, 2023

Copy link to clipboard

Copied

Thanks for your suggestions! Inside a function didn't work, I didn't try the delay yet, but I don't think that will work. I did figure out an alternative.. Leaving the jsx file empty, and from the CEP panel use...

csInterface.evalScript("#include " + coreScript);

 It works, except that I know need to know how to get an enviroment variable from CEP js, $.getenv() doesn't work inside the CEP js file. process.env.NAME also is not working, process is undifined.. I'm not using NodeJs in my CEP panel. I'm sure there is a syntax to get an enviroment variable from CEP, but I have no idea...

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
Contributor ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

I played around with it but couldn't find anything either.

 

Can you use a global for the environment variable?

 

Or (dirty) ... use prototyping to add it to $ and hope it piggybacks.

$.prototype.aNameYouChoose = theThingYouNeed;

 

Let us know if you find anything, I'll be watching. 🙂 Best of luck!

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 Expert ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

Write your values to a temp file and read from that. I'm not sure if CEP can access Photoshop prefs, if so you can store persistent values in those as well.

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
Participant ,
Feb 27, 2023 Feb 27, 2023

Copy link to clipboard

Copied

LATEST

I managed to get both scenario's working...

 

1) If you want a script being included from within a jsx file that is dependend on any logic for path generation, you can use this methode instead of the "# include" methode...

var rootPath= $.getenv("rootPath");
var scriptPath = rootPath + "Photoshop/script.jsx";

$.evalFile(File(scriptPath));

This seems to work identical to the "# include" methode, except it isn't executed bofore any logic is executed.

 

2) If you want a logic dependent jsx file path included in your jsx hostscript for your CEP extension. In my case I wasn't able to read the enviroment variable from within CEP's js, I used this methode...

var csInterface = new CSInterface();
csInterface.evalScript("$.getenv('rootPath')", function (result){
    var rootPath = result;
    var scriptPath = rootPath + "Photoshop/script.jsx";
    csInterface.evalScript("#include '" + scriptPath + "'");
}

 

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