Skip to main content
Inspiring
February 23, 2023
Answered

How to include .js file from enviroment variable

  • February 23, 2023
  • 2 replies
  • 2324 views

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?

Correct answer Marvin01

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 + "'");
}

 

2 replies

Marvin01AuthorCorrect answer
Inspiring
February 28, 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 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 + "'");
}

 

schroef
Inspiring
November 9, 2023

@Marvin01 

So your solution is not putting the rootPath call in the host script, but in a is file.

I'm trying to get folderPath of the extension in the hostscript.jsx

I can call for it in the is file and then pass it to hostscript but I'm not sure it will work properly since evalscript will pass it as a string and I'm not sure what it will do to the back or forward slashes

Marvin01Author
Inspiring
November 9, 2023

It's often trail and error when it comes to paths. You can use the fsName or fullName for example.

Backslashes can cause problems, but can work when escaped by double slashes. To make it easier to see what formats to use in what situation, try this script, you select a file and it shows all properties.

function showFile() {
    var file = File.openDialog("Select a file");
    
    if (file != null) {
        var fileInfo = "File Parameters...\n";
        fileInfo += "displayName: " + file.displayName + "\n";
        fileInfo += "name: " + file.name + "\n";
        fileInfo += "fsName: " + file.fsName + "\n";
        fileInfo += "fullName: " + file.fullName + "\n";
        fileInfo += "absoluteURI: " + file.absoluteURI + "\n";
        fileInfo += "relativeURI: " + file.relativeURI + "\n";
        fileInfo += "parent: " + file.parent + "\n";
        fileInfo += "path: " + file.path + "\n";
        fileInfo += "length: " + file.length + "\n";
        fileInfo += "alias: " + file.alias + "\n";
        fileInfo += "creator: " + file.creator + "\n";
        fileInfo += "eof: " + file.eof + "\n";
        fileInfo += "error: " + file.error + "\n";
        fileInfo += "exists: " + file.exists + "\n";
        fileInfo += "hidden: " + file.hidden + "\n";
        fileInfo += "lineFeed: " + file.lineFeed + "\n";
        fileInfo += "localizedName: " + file.localizedName + "\n";
        fileInfo += "readonly: " + file.readonly + "\n";
        fileInfo += "type: " + file.type + "\n";
        alert(fileInfo);
    }
}
showFile();
Marvin01Author
Inspiring
February 23, 2023

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.

Inspiring
February 23, 2023

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

Marvin01Author
Inspiring
February 23, 2023

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