Java Script in Illustrator

I am interested in performing a JavaScript in Adobe illustrator. I can import this directly into the program and activate there, however I want to create a pop-up palette with KM to perform this action/s. I tired "execute JavaScript for Automation" and it does not seem to work. Can this be done directly through KM?

> /////////////////////////////////////////////////////////////////
> //Zoom and Center to Selection v2. -- CS, CS2
> //>=--------------------------------------
> //
> //	Zooms active view to selected object(s).
> //  
> //  New in v.2:
> //  If nothing is selected; sets view to 100% at current location.
> //
> //	Simple but REALLY cool!
> //
> //>=--------------------------------------
> // JS code (c) copyright: John Wundes ( john@wundes.com ) www.wundes.com
> //copyright full text here:  http://www.wundes.com/js4ai/copyright.txt
> ////////////////////////////////////////////////////////////////// 
> 
> if ( documents.length > 0)
> {
> 	if(activeDocument.selection.length >0){
> 		mySelection = activeDocument.selection;
> 		//if object is a (collection of) object(s) not a text field.
> 		if (mySelection instanceof Array) {
> 			//initialize vars
> 			initBounds = mySelection[0].visibleBounds;
> 			ul_x = initBounds[0];
> 			ul_y = initBounds[1];
> 			lr_x = initBounds[2];
> 			lr_y = initBounds[3];
> 			//check rest of group if any
> 			for (i=1; i<mySelection.length; i++) {
> 			groupBounds = mySelection[i].visibleBounds;
> 			if (groupBounds[0]<ul_x){ul_x=groupBounds[0]}
> 			if (groupBounds[1]>ul_y){ul_y=groupBounds[1]}
> 			if (groupBounds[2]>lr_x){lr_x=groupBounds[2]}
> 			if (groupBounds[3]<lr_y){lr_y=groupBounds[3]}
> 			}
> 
>  
> 		}
> 	 
> 		//get x,y/x,y Matrix for 100% view
>  
> 		activeDocument.views[0].zoom = 1;
> 		ScreenSize = activeDocument.views[0].bounds;
> 		ScreenWidth= ScreenSize[2] - ScreenSize[0];
> 		ScreenHeight=ScreenSize[1] - ScreenSize[3];
> 		screenProportion =ScreenHeight/ScreenWidth;
> 
> 		//Determine upperLeft position of object(s)
> 		 cntrPos = [ul_x,ul_y];
> 		 
> 			 //mySelection[0].position;
> 		//cntrPos[0] += (mySelection[0].width /2);
> 		//cntrPos[1] -= (mySelection[0].height /2);
> 		//offset by half width and height
> 		mySelWidth=(lr_x-ul_x);
> 		mySelHeight=(ul_y-lr_y);
> 		cntrPos[0] = ul_x + (mySelWidth/2);
> 		cntrPos[1] = ul_y - (mySelHeight/2);
> 		//alert("ul point is "+cntrPos);
> 		//center to screen to the object
> 		activeDocument.views[0].centerPoint =  cntrPos;
> 		//alert("objWidth="+mySelection[0].width+", actualWidth="+ActualWidth);
> 		//alert("objHeight="+mySelection[0].height+", actualHeight="+ActualHeight);
> 
> 		//set zoom for height and width
> 		zoomFactorW = ScreenWidth/mySelWidth;
> 		zoomFactorH = ScreenHeight/mySelHeight;
> 		//alert("zoomFactorW = "+zoomFactorW+"\r zoomFactorH = "+zoomFactorH);
> 
> 		//decide which proportion is larger...
> 	if((mySelWidth*screenProportion) >= mySelHeight){
> 		zF = zoomFactorW;
> 		//alert("zoomFactorW = "+zoomFactorW);
> 	}else{
> 		zF = zoomFactorH;
> 		 //alert("zoomFactorH = "+zoomFactorH);
> 	}
> 
> 	//and scale to that proportion minus a little bit.
> 	activeDocument.views[0].zoom = zF *.85;
>  
> 	}else{
> 		//alert("Please select an object on the page.");
> 		activeDocument.activeView.zoom=1;
> 	}
> }

JavaScript is an embedded programming language, and the objects and methods available to it depend on the environment in which it is embedded.

KM Execute's JavaScript actions evaluate code in the JXA context, which has access to a special Automation object, with similar scope to AppleScript, but has no access to the other (entirely separate and unrelated) JS contexts of the various embedding in web browsers, Omni Apps, TaskPaper, QuarkXpress, or Adobe apps etc etc

The Illustrator activeDocument object and its various methods and properties are only available to code evaluated by the JS interpreter embedded in Illustrator. They are not available to the JXA JS interpreter.

What you can do, however, is pass the code string to the Illustrator.doScript method, which will then pass the code to Illustrator's own JS context, and execute it there.

See, for example, this thread:

2 Likes

Thank you - makes sense and very helpful!

1 Like