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

Script losing path when run from another script

Participant ,
Feb 22, 2021 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

434

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 2 Correct answers

Valorous Hero , Feb 22, 2021 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 = (

...

Votes

Translate

Translate
Valorous Hero , Feb 23, 2021 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;

 

 

 

Votes

Translate

Translate
Adobe
Community Expert ,
Feb 22, 2021 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

 

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 22, 2021 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();
}

 

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 ,
Feb 22, 2021 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.

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 22, 2021 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.

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, 2021 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);

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 ,
Feb 23, 2021 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;

 

 

 

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, 2021 Feb 23, 2021

Copy link to clipboard

Copied

LATEST

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;

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