Take the 2-minute tour ×
Graphic Design Stack Exchange is a question and answer site for Graphic Design professionals, students, and enthusiasts. It's 100% free, no registration required.

We're writing a bunch of .jsx scripts, and in each one I have to mock out some functions so I can use things like Array.map() and String.trim(), but I don't want to have to include that code at the top of every script.

Is there a way to "include" / reference other scripts or libraries inside of a .jsx script file?

share|improve this question
    
Also asked here: stackoverflow.com/questions/15746335/… –  Joonas Apr 1 '13 at 17:25

3 Answers 3

There was an answer here earlier today that seems to have disappeared, but it's what we're using now and it's working well. It's using the $ helper available in Illustrator, and the $.evalFile() method. Pass it a path and it will evaluate the file and return the result.

I created a little helper that I can include (minified, of course) at the top of my .jsx scripts so I can do Libraries.include("my-other-script") that will include, in my case, a file that's in my adobe_scripts root folder, in a directory called lib.

// indexOf polyfill from https://gist.github.com/atk/1034425
[].indexOf||(Array.prototype.indexOf=function(a,b,c){for(c=this.length,b=(c+~~b)%c;b<c&&(!(b in this)||this[b]!==a);b++);return b^c?b:-1;});

var Libraries = (function (libPath) {    
    return {
        include: function (path) {
            if (!path.match(/\.jsx$/i)) { 
                path = path + ".jsx"; 
            }
            return $.evalFile(libPath + path);
        }
    };
})($.fileName.split("/").splice(0, $.fileName.split("/").indexOf("adobe_scripts") + 1).join("/") + "/lib/");

Minified version that I include:

/**
 * Libraries.include(path) -- path must be relative to adobe_scripts/lib/
 * See: https://gist.github.com/jasonrhodes/5286526
 */
[].indexOf||(Array.prototype.indexOf=function(a,b,c){for(c=this.length,b=(c+~~b)%c;b<c&&(!(b in this)||this[b]!==a);b++);return b^c?b:-1;});var Libraries=function(a){return{include:function(b){return b.match(/\.jsx$/i)||(b+=".jsx"),$.evalFile(a+b)}}}($.fileName.split("/").splice(0,$.fileName.split("/").indexOf("adobe_scripts")+1).join("/")+"/lib/");

See gist here: https://gist.github.com/jasonrhodes/5286526

share|improve this answer
    
That was my answer. I accidentally answered how you can do it photoshop. When I realized that, I did some quick googling and I found zero examples of the same method being available in illustrator. So I deleted my answer. –  Joonas Apr 2 '13 at 5:40

Not sure if this will work for Illustrator, but in Photoshop I've had success using this method in a configurator panel:

#include "coreFunctions.jsx";
saveImage("jpg", "", "web", "", "");

The above code is the contents of a file called "jpeg@72.jsx", which calls the function "saveImage" (a user created function), declared in the coreFunctions.jsx file.

share|improve this answer

According to this thread in the Adobe Illustrator Scripting Community, it is possible.

Apparently the code you need is similar to this:

#target illustrator

var scriptToLoad = new File("S:/NEW SCRIPTS/JAVASCRIPTS IN USE NOW/02) Make/01b) Shape P - 16 UP.jsx");

var win = new Window('palette', 'Copy Objects');
var btnSelect = win.add('button', undefined, 'Copy');

btnSelect.onClick = function(){

   scriptToLoad.open("r");

   var bt = new BridgeTalk;
   bt.target = "illustrator";

   var script = scriptToLoad.read();
   scriptToLoad.close();

   bt.body = script;
   bt.send();
}// end function

win.center();
win.show();

Note: this is just what I found searching, not something I've personally tried.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.