Skip to main content
Inspiring
February 25, 2020
Answered

Issue getting height and width of selection

  • February 25, 2020
  • 2 replies
  • 646 views

Hi,

 

I manage to get a script working on a basic simulation but not in real case.

My goal is to get the selection (everything in the document) height and width and copy it to a csv file.

I got this to work when I placed multiple shapes in the document and ran the script. I divide the result by 72 to get inches.

Now when I run it in a real case I get the size of one single shape.

Here's my script:

 

main();
function main(){

var doc = app.activeDocument;
var docTitle = doc.name;
var docTitle = docTitle.substring(0, docTitle.length - 4);
var textFile = File("C:/Galvo/" + docTitle + ".csv");
var docText1 ="";
var docText2 = "";
var docTextMid = ';';
var sel = doc.groupItems[0];

app.executeMenuCommand("selectall");
app.executeMenuCommand("ungroup");
app.executeMenuCommand("group");
var point1 = sel.width;
var point2 = sel.height;
docText1 = point1/72;
docText2 = point2/72;   


textFile.open("w");
textFile.write(docText1 + docTextMid + docText2);
textFile.close();
}

I group them as trying to get selection width gave me NaN (not a number). Figured that groups may work... I thought it was because I had multiple groups in the scene so I try to select all and ungroup. Looked at my document after the operation and I end up with only 1 group but I can't find it at the 0 index. Does groupItems get an item within the group? If so - How can I get the total document selection lenght and width?

 

Thank you.

 

This topic has been closed for replies.
Correct answer pixxxelschubser

I don't know, why you go this way. But try that:

 

 

main();
function main(){

var doc = app.activeDocument;
var docTitle = doc.name;
var docTitle = docTitle.substring(0, docTitle.length - 4);
//var textFile = File("~/desktop/" + docTitle + ".csv");
var docText1 ="";
var docText2 = "";
var docTextMid = ';';
//var sel = doc.groupItems[0]; // not necessary

app.executeMenuCommand("selectall");
app.executeMenuCommand("ungroup");
app.executeMenuCommand("group");

var sel = doc.selection[0];

var point1 = sel.width;
var point2 = sel.height;
docText1 = point1/72;
docText2 = point2/72;   

alert(docText1 + docTextMid + docText2);
  //  textFile.open("w");
 //   textFile.write(docText1 + docTextMid + docText2);
  //  textFile.close();
}

 

 

2 replies

pixxxelschubser
Community Expert
Community Expert
February 25, 2020

Of course.

I've comment out the csv part only for easier "debugging".

 

Glad I could help

😉

Inspiring
February 25, 2020

it was easier that way yeah! 🙂 

Big thanks to you.

pixxxelschubser
Community Expert
pixxxelschubserCommunity ExpertCorrect answer
Community Expert
February 25, 2020

I don't know, why you go this way. But try that:

 

 

main();
function main(){

var doc = app.activeDocument;
var docTitle = doc.name;
var docTitle = docTitle.substring(0, docTitle.length - 4);
//var textFile = File("~/desktop/" + docTitle + ".csv");
var docText1 ="";
var docText2 = "";
var docTextMid = ';';
//var sel = doc.groupItems[0]; // not necessary

app.executeMenuCommand("selectall");
app.executeMenuCommand("ungroup");
app.executeMenuCommand("group");

var sel = doc.selection[0];

var point1 = sel.width;
var point2 = sel.height;
docText1 = point1/72;
docText2 = point2/72;   

alert(docText1 + docTextMid + docText2);
  //  textFile.open("w");
 //   textFile.write(docText1 + docTextMid + docText2);
  //  textFile.close();
}

 

 

Inspiring
February 25, 2020

Thank you for the solution. I started from there and changed a few things as I need to write in a CSV file the information for each pannels. An external software then access the csv file generate automatically from the .ai files to get the size information to send a request to prepare a panel of given the size.

main();
function main(){

var doc = app.activeDocument;
var docTitle = doc.name;
var docTitle = docTitle.substring(0, docTitle.length - 4);
var textFile = File("C:/Galvo/" + docTitle + ".csv");
var docText1 ="";
var docText2 = "";
var docTextMid = ';';

app.executeMenuCommand("selectall");
app.executeMenuCommand("ungroup");
app.executeMenuCommand("group");

var sel = doc.selection[0];

var point1 = sel.width;
var point2 = sel.height;
docText1 = point1/72;
docText2 = point2/72;   

alert(docText1 + docTextMid + docText2);
textFile.open("w");
textFile.write(docText1 + docTextMid + docText2);
textFile.close();
}