Script losing path when run from another script

Explorer ,
Feb 22, 2021

Copy link to clipboard

Copied

Hi,

 

I am running one script from another and using the below code in the second script. This works correctly if the second script is run by itself but seems to lose the script path if called from the first script.

Am I missing something obvious or is there a way to get the 'scriptLoc' variable across from the first script as it is the same location.

Many thanks in advance.

var scriptLoc = File($.fileName).path;
      var file1 = File(scriptLoc + "/Portrait_Lockup.ai/");
      file1 = open(file1);
TOPICS
Scripting

Views

97

Likes

Translate

Translate

Report

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

2 Correct Answers

Adobe Community Professional , Feb 23, 2021
Silly-V Adobe Community Professional , Feb 23, 2021
Yes, you need to remove this line:   var scriptLoc = File($.fileName).path;   Also I may have not escaped my nextline properly, so just remove it or add an extra backslash to see if it works: "var SCRIPT_LOCATION = \"" + scriptPath + "\"\\n" + message;      

Likes

Translate

Translate
Jump to answer Jump to answer
Adobe Community Professional , Feb 22, 2021
Silly-V Adobe Community Professional , Feb 22, 2021
This is an obscure issue that is very annoying when one gets to the point of discovering it, but here is how you deal with it: store your path to the script in a variable that will be spliced into the beginning of your bridgetalk script message:bt.body = "var SCRIPT_LOCATION = \"" + scriptPath + "\"\n" + message;   In your included script, have a condition that checks typeof(SCRIPT_LOCATION) == "string" or typeof(SCRIPT_LOCATION) != "undefined" to have logical branch like so:var fileLocation = (...

Likes

Translate

Translate
Jump to answer Jump to answer
Adobe Community Professional ,
Feb 22, 2021

Copy link to clipboard

Copied

How are you calling your code in the second file? I created a small test and it worked fine. I wrote the following code is my testing

source.jsx

#include abc.jsx
main()

abc.jsx

function main()
{
	var scriptLoc = File($.fileName).path;
	alert(scriptLoc)
}

I get the path displayed in the alert

-Manan

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Feb 22, 2021

Copy link to clipboard

Copied

I am calling the second script from a pallette wind as per the below, I ma using the same variable in both scripts though, not sure if that makes any difference? I will take a look at what you suggested to see if I can adapt it for use.

CreatePortraitLockup.onClick = function(){
aiscriptC1 ();
}


function aiscriptC1() {
  var scriptLoc = File($.fileName).path;
var scriptToLoad = new File (scriptLoc + "/PortraitCreateLockup.jsx/")

    scriptToLoad.open ("r");
    var message = scriptToLoad.read();
    scriptToLoad.close()

var bt = new BridgeTalk();
bt.target = "Illustrator";
bt.body = message;
bt.send();
}

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Feb 22, 2021

Copy link to clipboard

Copied

This is an obscure issue that is very annoying when one gets to the point of discovering it, but here is how you deal with it: store your path to the script in a variable that will be spliced into the beginning of your bridgetalk script message:
bt.body = "var SCRIPT_LOCATION = \"" + scriptPath + "\"\n" + message;

 

In your included script, have a condition that checks typeof(SCRIPT_LOCATION) == "string" or typeof(SCRIPT_LOCATION) != "undefined" to have logical branch like so:
var fileLocation = (typeof(SCRIPT_LOCATION) == "string")? SCRIPT_LOCATION : $.fileName;

Additionally you may wish to check if typeof($.fileName) == "number" because when you call scripts via BT, the property turns into some number. But if you ensure that you never actually set "SCRIPT_LOCATION" inside the included script, this additional check should be not necessary.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Feb 22, 2021

Copy link to clipboard

Copied

Thanks for the advice. I will take a look and see if I can get it to work.

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Explorer ,
Feb 23, 2021

Copy link to clipboard

Copied

Hi, I have tried this but does not seem to be working. I have done it as below, is this the correct way to do it:

 

Script 1:

CreatePortraitLockup.onClick = function(){
aiscriptC1 ();
}


function aiscriptC1() {
  var scriptPath = File($.fileName).path;
var scriptToLoad = new File (scriptPath + "/PortraitCreateLockup.jsx/")

    scriptToLoad.open ("r");
    var message = scriptToLoad.read();
    scriptToLoad.close()

var scriptPath = File($.fileName).path  + "/AldiSpeciallySelectedPortraitCreateLockup.jsx/";
var bt = new BridgeTalk();
bt.target = "Illustrator";
bt.body = "var SCRIPT_LOCATION = \"" + scriptPath + "\"\n" + message;
bt.send();
}

 

 

Script 2:

    CreateSpeciallySelectedLockupSize.onClick = function(){
    aiscriptA1 ();
    }

    function aiscriptA1() {
      var fileLocation = (typeof(SCRIPT_LOCATION) == "string")? SCRIPT_LOCATION : $.fileName;
          	var scriptLoc = File($.fileName).path;
      var address1 = fileLocation + "/Portrait_Lockup.ai";
      var file1 = File(address1);
      file1 = open(file1);

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Feb 23, 2021

Copy link to clipboard

Copied

Yes, you need to remove this line:

 

          	var scriptLoc = File($.fileName).path;

 

Also I may have not escaped my nextline properly, so just remove it or add an extra backslash to see if it works:

"var SCRIPT_LOCATION = \"" + scriptPath + "\"\\n" + message;

 

 

 

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
glibshaft AUTHOR LATEST
Explorer ,
Feb 23, 2021

Copy link to clipboard

Copied

Got it, that works, I just needed to remove the backslashes and then remove the file name from the 'scriptPath' variable in the first script.

 

Yep, I realised I had left in the below by accident, I had cancelled it out right after sending the message.

 

Thanks once again, you sir, are a genius!!!

var scriptLoc = File($.fileName).path;

Likes

Translate

Translate

Report

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