How to make a script run when a file is opened in Illustrator?
Copy link to clipboard
Copied
Hello!
I have the below script that I'd like to run when I open a file. I tried saving it in the "Startup Scripts CC" folder instead of the scripts folder in Presers; however, I get the error window (attached) whenever I start up Illustrator. I can click on the OK button to continue, and the script works fine but would like to know if there's any way to prevent it from popping up. Can I please get help?
___________________________________________________________________
docRef = app.activeDocument;
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
/////////////////////////////////////////////////////////////////////////////////////////////////
function fileInfo(){
var pagesnr = docRef.artboards.length; // number of artboards
for (var i = 0; i < pagesnr; i++) // loop thru all artboards
{
//Get sizes of the artboard
var artboard = docRef.artboards[i];
var aRect = artboard.artboardRect;
//Artboard parameters as variables
var aLeft = aRect[0];//left site of a page
var aTop = aRect[1];//top of a page
var aRight = aRect[2];//right site of a page
var aBottom = aRect[3];//bottom of a page
var aWidth = aLeft + aRight; //width of a page
var aCenter = aWidth/2; //center of a page
//Text Justification
var center = Justification.CENTER;//Justification center
var left = Justification.LEFT;//Justification left
var right = Justification.RIGHT;//Justification right
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
//FILE NAME
var fileName = docRef.textFrames.add();
fileName.contents = app.activeDocument.name;//File name
fileName.position = [aLeft + 0, aTop + 15];//position in a page
//FILE NAME Style
var nameStyle = fileName.textRange.characterAttributes;
nameStyle.size = 12;//size in punkt
//nameStyle.textFont = textFonts.getByName("Avenir-Book");//the font
nameStyle.capitalization = FontCapsOption.ALLCAPS;//ALL CAPITALS
//COLOR
//nameStyle.fillColor = makeColorCMYK (20,77,36,8);//color in CMYK
nameStyle.fillColor = makeColorRGB (255,255,255,8);//color in RGB
//JUSTIFICATION
//var nameparagStyle = fileName.textRange.paragraphAttributes;
//nameparagStyle.justification = left; //Justification
}
}
fileInfo();
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////
//FUNCTIONS
//RGBColor
function makeColorRGB(r,g,b){
var c = new RGBColor();
c.red = r;
c.green = g;
c.blue = b;
return c;
}
// CMYKColor
function makeColorCMYK(c,m,y,k){
var ink = new CMYKColor();
ink.cyan = c;
ink.magenta = m;
ink.yellow = y;
ink.black = k;
return ink;
}
//Get date
function getDate(){
var today = new Date();
var date = today.getDate() + ' / ' + (today.getMonth()+1)+ ' / ' +today.getFullYear();
return date;
}
___________________________________________________________________
Explore related tutorials & articles
Copy link to clipboard
Copied
Hi @michikoy96591877, I have bad news I'm afraid. Firstly the error you are seeing is because when the script runs (at Illustrator startup) there are no documents. And, worse, I.m pretty sure we don't have access to any document events via scripting in Illustrator. It is different with CEP or SDK.
- Mark
Copy link to clipboard
Copied
I understand... I wish there was a way, but good to know! 🙂
Thank you for letting me know, Mark!

