Skip to main content
Babymac08
Known Participant
November 28, 2016
Question

Script for Proofing Template

  • November 28, 2016
  • 13 replies
  • 3937 views

Was wondering if it would be possible to create a Script that would take a Selection or image and re-size it proportionally into an 11"w x 8.5" H document and place it Centered in a 10.5" x 6" box located at the top of the page. If possible if it can detect the size as being less than 10.5" x 6" then don't scale, but if larger scale down to fit Proportionately.... Then Provide a dialog box that would let me enter my job # and today's date in a pre-determined location. I've attached a sample of what I'm trying to accomplish for the finished look. I'm also Assuming the base elements will need to reference a file (or template) that would contain the proofing information if so we'll call it "Proof_Temp.ai"

Huge thanks in advance for anyone that can assist...

This topic has been closed for replies.

13 replies

Participant
May 10, 2020

I need something similar to allow my non-illustrator employees to produce proofs easily. I would be happy to hire someone to do this. Please contact me [Removed Email address by Moderator].

 

Please, Do not write any private information in public forum.

Participating Frequently
May 1, 2020

Hi read this and was wondering if it works? We do similar work but would like to create this in InDesign and with dimensions of image etc mentioned in inches.

Babymac08
Babymac08Author
Known Participant
December 13, 2016

I have finally managed to complete this task and it's wonderful... No I'm in the process of tweaking it to add additional functionality...

Thanks to so many for all your help....

Babymac08
Babymac08Author
Known Participant
December 1, 2016

Guys I really do appreciate your help... I'm trying the best I can on a limited budget of time and knowledge... I'm trying to figure this thing out... but still struggling to get this command to go... At this point can you just verify If I have the code placed in the correct place and  it's set to perform the basic task of opening and sizing the artboard to the size of the file it opened...

Thanks

// File Open Command for Template

var theFile = File('~/Desktop/Proof Sample.jpg');

app.open(theFile);

//Command to Set Artboard size to File

app.open(theFile);

var theImg = docRef.pageItems[0];

theImg.left = 0;

theImg.top = 0;

var docSize = [theImg.width,theImg.height];

artboards[0].artboardRect = [0,0,docSize[0],-(docSize[1])];

Disposition_Dev
Legend
December 1, 2016

assuming what you just pasted in is the exact code you're running, here are the issues.

You are running this command twice:

app.open(theFile).

Delete the second one.

on this line:

var theImg = docRef.pageItems[0];

the syntax is perfect, but you haven't defined "docRef". This line is going to fail because "docRef" is undefined.

Then the last line you posted:

artboards[0].artboardRect = [0,0,docSize[0],-(docSize[1])]

the variable "artboards" is undefined. this line will also give you an error.

here's how your snippet should look to do the basic task you're asking for:

var theFile = File('~/Desktop/Proof Sample.jpg');

app.open(theFile);

//set artboard size to image size

var docRef = app.activeDocument;

var artboards = docRef.artboards;

var theImg = docRef.pageItems[0];

theImg.left = 0;

theImg.top = 0;

artboards[0].artboardRect = [0,0,theImg.width,-(theImg.height)]

Babymac08
Babymac08Author
Known Participant
December 1, 2016

OK.. My apologies... I've tried it after the open dialog then in the sizing section... Can't get it to run... Where should it Put in the code to work correctly... and does anything need to come out...

Thanks

#target illustrator

#targetengine session

// script.name = PROOF Template.jsx;

// script.description = Opens Image and Places on Proof Template;

// File Open Command for Template

var theFile = File('~/Desktop/Proof Sample.jpg');

app.open(theFile);

//Command to Set Artboard size to File

var docRef = app.activeDocument;

var theImg = doc.pageItems[0];

theImg.left = 0;

theImg.top = 0;

var docSize = [theImg.width,theImg.height];

artboards[0].artboardRect = [0,0,docSize[0],-(docSize[1])];

//Place New Image

doc = app.activeDocument;

var image = File('~/Desktop/shutterstock_319903532.eps');

var doc = app.activeDocument; 

var newimage = doc.placedItems.add(); 

newimage.file = image; 

newimage.position = [0,-792]; 

//Command to Center on Artboard

var aDoc = app.activeDocument; 

app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM; 

var abIdx = aDoc.artboards.getActiveArtboardIndex(); 

var actAbBds = aDoc.artboards[abIdx].artboardRect; 

var obj2move = aDoc.selection[0]; 

obj2move.position = new Array ((actAbBds[2]-actAbBds[0])/2 - obj2move.width/2, (actAbBds[3]-actAbBds[1])/2  + obj2move.height/2);

Babymac08
Babymac08Author
Known Participant
December 1, 2016

OK I'm making some progress... but I'm missing something in the option of selecting an item.....

I'm getting an error on the line " var theImg = curr_file.selection.selectAll(); "  What I'm trying to do is select the image that was opened in the first steps... so that I can size the Artboard to the same size... (I currently have a specific file named there, but eventually want to be able to use it for various size files...

Secondly... Can you double check the final section about centering on the artboard...  I can't get that part ran yet because of the selection error... but want to just have you take a look to verify...

Thanks

*****Script Below ********

#target illustrator

#targetengine session

// script.name = PROOF Template.jsx;

// script.description = Opens Image and Places on Proof Template;

// File Open Command for Template

var theFile = File('~/Desktop/Proof Sample.jpg');

app.open(theFile);

//Command to Set Artboard size to File

var docRef = app.activeDocument;

var theImg = curr_file.selection.selectAll();

theImg.left = 0;

theImg.top = 0;

var docSize = [theImg.width,theImg.height];

artboards[0].artboardRect = [0,0,docSize[0],-(docSize[1])];

//Place New Image

doc = app.activeDocument;

var image = File('~/Desktop/shutterstock_319903532.eps');

var doc = app.activeDocument; 

var newimage = doc.placedItems.add(); 

newimage.file = image; 

newimage.position = [0,-792]; 

//Command to Center on Artboard

var aDoc = app.activeDocument; 

app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM; 

var abIdx = aDoc.artboards.getActiveArtboardIndex(); 

var actAbBds = aDoc.artboards[abIdx].artboardRect; 

var obj2move = aDoc.selection[0]; 

obj2move.position = new Array ((actAbBds[2]-actAbBds[0])/2 - obj2move.width/2, (actAbBds[3]-actAbBds[1])/2  + obj2move.height/2);

Silly-V
Legend
December 1, 2016

If you are opening an image and Illustrator makes a new document with just that image inside you can do this:

var theImg = doc.pageItems[0];

Babymac08
Babymac08Author
Known Participant
December 1, 2016

Sorry Missed that...

Thanks

Silly-V
Legend
December 1, 2016

Yea - sorry I was on my phone and in the middle of something - I should have mentioned something about how openDialog works. Probably would have helped

Babymac08
Babymac08Author
Known Participant
December 1, 2016

Thanks William... I've attempted the simple version that Silly-V mention... but It's not producing any results....  I've tried looking at other scripts to see if there is a missing end of command line of something I'm missing.. but have yet to gain success.... This is just the simple first step of opening a file...

#target illustrator

#targetengine session

// script.name = PROOF Template.jsx;

// script.description = Opens Image and Places on Proof Template;

File.openDialog()

Disposition_Dev
Legend
December 1, 2016

I addressed this question in my last response.

File.openDialog(); does not open a file. It simply returns a path to a file. You need to save the result as a variable and then use another command to open the file. like this:

var theFile = File.openDialog();

app.open(theFile);

it's the second line of that snippet that actually opens the file.

Babymac08
Babymac08Author
Known Participant
December 1, 2016

Silly-V

I tried your command... definitely much simpler and to the point... However.. It does bring the dialog up and allows me to select a file... but never actually opens the file....  Sorry to Ask... is there an end of command or something that's missing..

#target illustrator

#targetengine session

// script.name = PROOF Template.jsx;

// script.description = Opens Image and Places on Proof Template;

File.openDialog()

Also Williamadowling... I tried removing that line but obviously prompted further errors... Is there a way to "Define" what's missing?

Thanks to both of you for your help....

Disposition_Dev
Legend
December 1, 2016

As Silly-V said, much of this is probably over your head if you're just starting out with all this. I still struggle with ScriptUI from time to time and I've been using it for a while. That said, I'll still answer your questions if you want because the knowledge can't hurt.

File.openDialog() returns a path to the file you selected. It's not an "open" command in terms of opening files, it just opens a dialog. So you have to save the return value of "File.openDialog()" to a variable and then do something with the variable like this:

var theFile = File.openDialog();

app.open(theFile);

Back to your question regarding the ScriptUI and undefined variables..

The script can't tell you what's missing. If it could, there wouldn't really be much need for programmers. Computers can't really infer what you want to happen, It can only tell you when it tries to do something that is missing. but anything that's missing beyond that point won't be known until the script fails at that point. It's up to you to be able to read the code and make sure that the necessary elements are there.

removing that line creates more errors because the subsequent lines attempt to access the variable "btnGroup". So removing the line doesn't fix a problem so much as transfer the problem elsewhere.

a potential fix is to declare a variable called "grpPanel" and give it a value. Another potential fix is to define "btnGroup" as a child of something that already does exist. but the best fix is to start from scratch. that way you're only working with variables you yourself created, and thus it's far easier to know what you have access to.

Editing existing code to work for your needs can be a very difficult thing to do and requires a really solid grasp of how to read the code and figure out exactly what it's doing. Most often, you're better off writing the code from scratch, as it's much easier to understand while you're developing it. You can take it logically from step to step and make sure that you've covered your bases. Start out by giving us a detailed step by step of your process, and then we can break it down into chunks of code.

Babymac08
Babymac08Author
Known Participant
December 1, 2016

OK.. I've started by taking some elements of another script I found... and I've tried to modify to allow me to start by getting a dialog box that allows me to select what file to open... However I'm getting an error.. This was originally intended for opening a multi-page .pdf and I think I've changed it to select any file type...

Error 21: undefined is not an object.

Line: 18

-> var btnOK = btnGroup.add("button", undefined, "Open");

Thanks for any help... just trying to get step 1 of my project done...

*****************Script Below ***************************

#target illustrator

#targetengine session

// script.name = PROOF Template.jsx;

// script.description = Opens Image and Places on Proof Template;

//----------------------- START UI CODE, create user interface

var win = new Window ("dialog", "Open File for Proof Template");

var fileGroup = win.add("group"); // this is the group on the left, it holds the File button and the Font label note

var btnFile = fileGroup.add("button", undefined, "File..."); // button to select the PDF to open

var grpRight = win.add("group"); // group on the right, to hold everything else

var txtFile = grpRight.add("edittext",undefined);

var btnGroup = grpPanel.add("group");

var btnOk = btnGroup.add("button", undefined, "Open");

var btnCancel = btnGroup.add("button", undefined, "Cancel");

var lblStatus = grpRight.add("statictext",undefined,"Dynagraphics - PROOF Template");

win.orientation = pagesPanel.orientation = "row"; // two items fileGroup and grpRight

win.alignChildren = "right";

fileGroup.orientation = "column";

fileGroup.alignment = "top";

txtFile.alignment = ["fill","top"];

lblStatus.alignment = "left";

grpRight.orientation = "column";

btnGroup.orientation = "column";

btnOk.enabled = false; // disable this button until a valid file is supplied

txtFrom.characters = txtTo.characters = 3;

btnFile.active = true; // receive the first "Enter"

//------------------------ get the file

btnFile.onClick = function(){

  txtFile.text = ""; // clear previous File path if any

  btnOk.enabled = false; // disable the Ok button

  var fileRef = File.openDialog ("Select File...", "*.*"); // get the file

  fileRef = new File(fileRef.fsName.replace("file://",""));

  if(fileRef!= null && fileRef.exists) // check if it is valid file, it should be, unless after clicking a file, the name gets edited

  {

  txtFile.text = fileRef.fsName; // show the file Path here

  btnOk.enabled = true; // enable the Ok button

  txtTo.active = true; // move focus to change the last page to open

  }

}

//------------------------

btnOk.onClick = function(){

  doSomething(); // call main function.

  win.close(); // close when done

}

//------------------------ on leaving this text, check again if file exist, in case file path is typed instead of clicking the File...button

txtFile.onDeactivate = function(){

  //alert("on deactivate")

  var file = File(txtFile.text); // create a file based on the text edit control

  if (file.exists){ // and chekc for existance, if it does

  btnOk.enabled = true; // enable the Ok button

  }

  else { // if it does not exist

  btnOk.enabled = false; // disable the Ok button

  }

}

//------------------------

win.center ();

win.show();

//-------------------------END UI CODE

Silly-V
Legend
December 1, 2016

Don't start editing codes or messing with ScriptUI just yet- if you're  beginner: you can use a simpler File.openDialog() command to open a file or a Folder.selectDialog() to choose a folder.