Copy link to clipboard
Copied
Hello everyone.
I do some very repetitive work which involves dimensions. Let's say I have a simple 100x100mm rectangle. Instead of always writing 100x100mm in a text box (which is always fixed in the same place by the way), is there a way to link the text box to the rectangle's dimensions?
For example, if I changed the rectangle's size to 95x45mm the text would automatically change to 95x45mm.
This sounds very simple in my head, but I can't find information about this anywhere.
If I found a way to do this it would save me a ton of time.
Thank you in advance.
Copy link to clipboard
Copied
There is no way to dynamically link objects in such a manner. You could perhaps create a bunch of actions for the most common sizes and use the Transform Each feature to facilitate some of the work, though.
Mylenium
Copy link to clipboard
Copied
I think what you describe could be scripted. It may require naming the rectangles and text blocks so the script could reference them properly. It may also require running the script after adjusting the rectangle sizes which could be a simple shortcut.
Copy link to clipboard
Copied
Here is a "proof-of-concept" script that scales a named Rectangle to entered dimensions, then updates and centers the text in the rectangle.
autoSizeRect();
function autoSizeRect(){
var aDoc = app.activeDocument;
var Text1 = aDoc.textFrames['Text1'];
var Rect1 = aDoc.pathItems['Rect1'];
var rWidth;
var rHeight;
var mm = 2.835; //multiply Points to convert to mm
userPrompt();
Rect1.width = rWidth * mm;
Rect1.height = rHeight * mm;
Text1.contents = (rWidth + "mm x " + rHeight + "mm");
function userPrompt(){
myInput = prompt('Width mm, Height mm','100, 100');
splitInput = myInput.split(",");
rWidth= Number(splitInput[0]);
rHeight = Number(splitInput[1]);
}
alignToObj();
function alignToObj() {
var aDoc = app.activeDocument;
var keyCenter = getCenterPoint(Rect1);
//var curItem, curCenter;
const ALIGNMENT_PREFERENCE_VERTICAL = true;
const ALIGNMENT_PREFERENCE_HORIZONTAL = true;
Text1.left = keyCenter.h - Text1.width / 2;
Text1.top = keyCenter.v + Text1.height / 2;
}
}
And here's a simple file to test it with.
https://www.dropbox.com/s/s5jtjlme02mxkq9/test.ai?dl=0
Copy link to clipboard
Copied
Thank you! Your proof of concept works. I seem to be getting an error message about line 24 but it could be something on my end, programming isn't really my thing.
Copy link to clipboard
Copied
That's because I failed to include a critical function "getCenterPoint" (it was already loaded in AI for me). Here's an updated script:
//by Ray Craighead
//Requires AI doc with text frame named "Text1" and rectangle shape named "Rect1" .
autoSizeRect();
function autoSizeRect(){
var aDoc = app.activeDocument;
var Text1 = aDoc.textFrames['Text1'];
var Rect1 = aDoc.pathItems['Rect1'];
var rWidth;
var rHeight;
var mm = 2.835; //multiply Points to convert to mm
userPrompt();
Rect1.width = rWidth * mm;
Rect1.height = rHeight * mm;
Text1.contents = (rWidth + "mm x " + rHeight + "mm");
function userPrompt(){
myInput = prompt('Width mm, Height mm','100, 100');
splitInput = myInput.split(",");
rWidth= Number(splitInput[0]);
rHeight = Number(splitInput[1]);
}
alignToObj();
function alignToObj() {
var aDoc = app.activeDocument;
var keyCenter = getCenterPoint(Rect1);
const ALIGNMENT_PREFERENCE_VERTICAL = true;
const ALIGNMENT_PREFERENCE_HORIZONTAL = true;
Text1.left = keyCenter.h - Text1.width / 2;
Text1.top = keyCenter.v + Text1.height / 2;
}
}
function getCenterPoint(myAlignObj) {
return {
"h": myAlignObj.left + myAlignObj.width / 2,
"v": myAlignObj.top - myAlignObj.height / 2
}
}
It would take some effort to make this more generic and versital. More info on how you intend to use it would help.
Edit: The "effort" turned out to be changing one variable; "myGroup". See below.
Copy link to clipboard
Copied
Give this script a try.
Here's a short video showing it in action.
The rectangle will be resized and the text updated to reflect the new dimensions. You can have any number to such groups in the same document.
Edit: I addressed an error that occured when "Cancel" is clicked. I also changed the divider to an 'x' when entering the dimensions.
Edit: I added a simple check to make sure there is a selection. I need a better check but I'm still learning.
//by Ray Craighead
//Select a group with one text object and one rectangle object, then run the script.
autoSizeRect2();
function autoSizeRect2(){
var aDoc = app.activeDocument;
var sel = aDoc.selection;
if (sel.length != 1){
alert ('Select a group with 1 text & 1 rectangle.');
return;
}
var myGroup = aDoc.selection[0];
var Text1 = myGroup.textFrames[0];
var Rect1 = myGroup.pathItems[0];
var rWidth;
var rHeight;
var mm = 2.83464567; //multiply Points to convert to mm
userPrompt();
if (myInput == null || myInput == "") {
return;
}
Rect1.width = rWidth * mm;
Rect1.height = rHeight * mm;
Text1.contents = (rWidth + "mm x " + rHeight + "mm");
function userPrompt(){
var myW = 100;
var myH = 100;
var txt;
myInput = prompt('Width mm x Height mm' , myW + 'x' + myH);
if (myInput == null || myInput == "") {
return;
}
splitInput = myInput.split("x");
rWidth= Number(splitInput[0]);
rHeight = Number(splitInput[1]);
}
alignToObj();
function alignToObj() {
var aDoc = app.activeDocument;
var keyCenter = getCenterPoint(Rect1);
const ALIGNMENT_PREFERENCE_VERTICAL = true;
const ALIGNMENT_PREFERENCE_HORIZONTAL = true;
Text1.left = keyCenter.h - Text1.width / 2;
Text1.top = keyCenter.v + Text1.height / 2;
}
}
function getCenterPoint(myAlignObj) {
return {
"h": myAlignObj.left + myAlignObj.width / 2,
"v": myAlignObj.top - myAlignObj.height / 2
}
}
Copy link to clipboard
Copied
Bonjour à tous,
Avec un groupe (text+rect) comme la préconisé rcraighead.
Si on sélectionne le texte, le rectangle (ou autre objet) est ajusté au contenu du texte
Si on sélectionne le rectangle, le contenu du texte est mis à jour.
de elleere
if (selection.length) {
autoSizeRect3();
} else alert ("Select 1 text or 1 rectangle.\rd'un même group.");
function autoSizeRect3(){
var Rect1, Text1;
var mm = UnitValue (1,"mm").as('pt');; //multiply Points to convert to mm
var sel = selection;
if(sel[0].parent.typename == "GroupItem") {
if (sel[0].typename == "PathItem") {
Rect1 = sel[0];
Text1 = Rect1.parent.textFrames[0];
var W = (Rect1.width/mm).toFixed(2);
var H = (Rect1.height/mm).toFixed(2);
Text1.contents = (W + "x" + H + "mm");
}
else if (sel[0].typename == "TextFrame") {
Text1 = sel[0];
Rect1 = Text1.parent.pathItems[0];
var numValue = Text1.contents;
numValue = numValue.replace(/mm/,"");
try {
var numValues = numValue.split("x");
Rect1.width = numValues[0]*mm;
Rect1.height = numValues[1]*mm;
}
catch (err) {
alert ("ERREUR : " + (err.number & 0xFFFF) + ", " + err.description);
}
}
}
}
Copy link to clipboard
Copied
Elleere, I like it! And I always learn something from you! Thanks.