• 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 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.6K

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
Adobe
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
LEGEND ,
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

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
Advocate ,
Nov 08, 2023 Nov 08, 2023

Copy link to clipboard

Copied

@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

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 ,
Nov 09, 2023 Nov 09, 2023

Copy link to clipboard

Copied

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

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
Advocate ,
Nov 10, 2023 Nov 10, 2023

Copy link to clipboard

Copied

LATEST

@Marvin01 

Thanks for the fast reply. I will try these. Though at this moment I solved my need for this case.

 

I was looking for a method to send data from one panel to another panel within the same extension. Found out, it can be done using csevent(). It was not really clear for, I'm non developer, how to exactly implement it. I could not find a real example of this. Finally found an old article from 2014, which sort of had good info. So that solved my issue.

 

I wanted to rootpath of the extension so I could save a JSON file with the data and then read it from the other panel. My current solution is much easier and probably works smoother and faster.

 

Thanks again for the effort!

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