Skip to main content
Participant
August 6, 2020
Answered

Passing a $.filename path variable via bridgetalk

  • August 6, 2020
  • 1 reply
  • 2329 views

Hello there! I'm pretty new to this whole coding/scripting thing, so forgive me...

 

I'm trying to pass a variable to get the relative path to a .jsx file via bridgetalk (From Illustrator to Photoshop if that is of any matter!)

 

Here's my code:

var scriptPath = (new File($.fileName)).path 

//Bridgetalk stuff
var scriptToLoad = new File (scriptPath + "/psScript.jsx"// laddar den rätta skriptfilen
try {
    if (!scriptToLoad.exists) { throw new Error("script not found!"); }
    scriptToLoad.open ("r"); // read only
    var message = scriptToLoad.read();
    scriptToLoad.close()
catch (error) {
    alert("Error parsing the file: " + error.description);
}
#target photoshop-13
var bt = new BridgeTalk();
bt.target = "photoshop";
bt.body = message + scriptPath.toSource();
bt.send();
 
And on the other end in psScript.jsx I'm just trying to get it to alert me the path in a dialog box:
alert('Script path is: ' + scriptPath);
 
 
This topic has been closed for replies.
Correct answer CarlosCanto

one way of doing what you need with minimal changes to your code is by

 

- wrapping your psScript in a function

- adding the function call to your bt.body. In other words, right now message refers to the psScript (which is just the function now), so we need to add the function call including the path parameter.

 

function main() {
    var scriptPath = (new File($.fileName)).path 

    //Bridgetalk stuff
    var scriptToLoad = new File (scriptPath + "/psScript.jsx") // laddar den rätta skriptfilen
    try {
        if (!scriptToLoad.exists) { throw new Error("script not found!"); }
        scriptToLoad.open ("r"); // read only
        var message = scriptToLoad.read();
        scriptToLoad.close()
    } catch (error) {
        alert("Error parsing the file: " + error.description);
    }
    //#target photoshop-13
    var bt = new BridgeTalk();
    bt.target = "photoshop";
    bt.body = message + "\nreadPath('" + scriptPath + "')";
    bt.send();
     
    //And on the other end in psScript.jsx I'm just trying to get it to alert me the path in a dialog box:
    //function readPath(scriptPath) {
    //    alert('Script path is: ' + scriptPath);
    //}
}

main();

1 reply

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
August 6, 2020

one way of doing what you need with minimal changes to your code is by

 

- wrapping your psScript in a function

- adding the function call to your bt.body. In other words, right now message refers to the psScript (which is just the function now), so we need to add the function call including the path parameter.

 

function main() {
    var scriptPath = (new File($.fileName)).path 

    //Bridgetalk stuff
    var scriptToLoad = new File (scriptPath + "/psScript.jsx") // laddar den rätta skriptfilen
    try {
        if (!scriptToLoad.exists) { throw new Error("script not found!"); }
        scriptToLoad.open ("r"); // read only
        var message = scriptToLoad.read();
        scriptToLoad.close()
    } catch (error) {
        alert("Error parsing the file: " + error.description);
    }
    //#target photoshop-13
    var bt = new BridgeTalk();
    bt.target = "photoshop";
    bt.body = message + "\nreadPath('" + scriptPath + "')";
    bt.send();
     
    //And on the other end in psScript.jsx I'm just trying to get it to alert me the path in a dialog box:
    //function readPath(scriptPath) {
    //    alert('Script path is: ' + scriptPath);
    //}
}

main();
Participant
August 7, 2020

A thousand thanks mate, this helped! 🙂