Skip to main content
Participant
July 6, 2021
Question

How can I pass a variable defined in Illustrator to Photoshop?

  • July 6, 2021
  • 4 replies
  • 301 views

Hello,

 

I am writing a script primarily in photoshop but would liketo do some of the inital work in illustrator. Ultimately this will be a script to save mockups for tshirts, looping through designs and color choices and saving as jpg. 

 

I have the full script working the way I want in photoshop currently but in order to make it better I would like to assign a selected color variable in illustrator where I am working with the designs and then pass to photoshop but I am not sure how to call that variable. 

Just learned about bridgeTalk but not sure how to do the onResult or onRecieve for what I want to do.

 

 

here is a small section of what I have so far:

 

var bt = new BridgeTalk();
var specifier = "illustrator";
bt.target = specifier;
bt.body = '\
var docRef = app.activeDocument;\
var ORGselectedcolors1 = "";\
\
//Identify Selected Colors\
\
for ( var j=0; j < docRef.layers.getByName("+Shirt_Color").groupItems.length; j++ ) {\
if(docRef.layers.getByName("+Shirt_Color").groupItems[j].hidden == false){\
ORGselectedcolors1 = ORGselectedcolors1 + j +";";\
};\
};\
alert("orgslectedcolors: " + ORGselectedcolors1);\
';

bt.send();

 

the alert shows me the colors I have selected as it is supposed to ORGselectedcolors1 = 0;4;5;6

 

how do I utilize this information for the remaining part of the script in photoshop?

 

 

#target photoshop

var selectedcolors = ORGselectedcolors1 (from illustrator bridge talk)

 

Will post the sample documents and code shortly if that will help

thank you 

This topic has been closed for replies.

4 replies

rob day
Community Expert
Community Expert
July 6, 2021

If the script is running from Illustrator and you want to open Photoshop, then pass in a string, and run a Photoshop function:

 

 

//Illustrator as the script application
#target illustrator

var docRef = app.activeDocument;
var ORGselectedcolors1 = "Foo";

for ( var j=0; j < docRef.layers.getByName("+Shirt_Color").groupItems.length; j++ ) {
    if(docRef.layers.getByName("+Shirt_Color").groupItems[j].hidden == false){
        ORGselectedcolors1 = ORGselectedcolors1 + j +";";
    };
};

runPhotoshop(ORGselectedcolors1);

/**
* Opens Photoshop via Bridgetalk and runs psScript function 
*  a string passed from Illustrator 
*  void 
* 
*/
function runPhotoshop(s){
    //open Photoshop and run psScript
    var bt = new BridgeTalk();  
    bt.target = "photoshop"; 
    //a string var. Note string single, double quote syntax '"+s+"'
    bt.body = "var s = '"+s+"';"
    //the function and the call
    bt.body += psScript.toString() + "psScript();";
    //$.writeln(bt.body)
    bt.onResult = function(resObj) { 
        //returned from the Photshop function
        alert(resObj.body);
    }  
    bt.onError = function( inBT ) { alert(inBT.body); };  
    bt.send(8);  
      
    function psScript() {  
        
        alert("Photoshop Running. Variable sent from Illustrator: " + s)
        
        //add commands here
        
        //this is returned after the fuction runs
        return "Message from Photoshop—Done!"
    }  
}

 

Kukurykus
Legend
July 6, 2021

 

if (!arguments.length) {
	function illstr(fileName) {
		function vbscriptFileContent(value) {
			return """Set app = CreateObject("Photoshop.Application")
			: app.BringToFront : fle = """" + File(fileName).fsName +
			"""" : call app.DoJavaScriptFile(fle, """ + value + """)
			: Set fso = createobject("scripting.filesystemobject")
			: fso.deletefile(wscript.scriptfullname)"""
		}

		/*regular ExtendScript Illustrator code*/ rslt = '0;4;5;6';
		vbs = vbscriptFileContent('Array(' + rslt.split(';') +')');
		with(fle = File(Folder.desktop + '/vbscriptName.vbs'))
			open('w'), write(vbs), close(), execute()
	}

	(bt = new BridgeTalk()).target = 'illustrator', bt.body = 'illstr = ' +
	illstr.toSource() + ', illstr(' + $.fileName.toSource() + ')', bt.send()
}
else alert([].slice.call(arguments))

 

Chuck Uebele
Community Expert
Community Expert
July 6, 2021

I haven't tried this out, but declaring the variable at the beginning of your script should make it global, and available to the entire script. Another option would be to write the info to a cssv file and then read back the file in the PS portion of the script.

Legend
July 6, 2021

 

var bt = new BridgeTalk();
var specifier = "bridge";
bt.target = specifier;
bt.body ='\
var docRef = app.activeDocument;\
var ORGselectedcolors1 = "";\
\
//Identify Selected Colors\
\
for ( var j=0; j < docRef.layers.getByName("+Shirt_Color").groupItems.length; j++ ) {\
if(docRef.layers.getByName("+Shirt_Color").groupItems[j].hidden == false){\
ORGselectedcolors1 = ORGselectedcolors1 + j +";";\
};\
};\
ORGselectedcolors1;'

bt.onResult = function (response) {alert ( response.body)}
bt.onError = function (err) {alert(err.body) }
bt.send(3000);