Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
4

How to make a script run when a file is opened in Illustrator?

New Here ,
Nov 17, 2023 Nov 17, 2023

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;

}

___________________________________________________________________

Screenshot 2023-11-17 at 7.21.57 PM.pngexpand image

TOPICS
Scripting
181
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 17, 2023 Nov 17, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 18, 2023 Nov 18, 2023
LATEST

I understand... I wish there was a way, but good to know! 🙂

Thank you for letting me know, Mark!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines