Scripts not working correctly on Large Scale Canvas
Copy link to clipboard
Copied
So I've attached some txt files of scripts I've written for a project I'm working on. I'm creating digital renders of walls on a 10 pixels : 1 inch ratio. The following scripts get me the dimensions in inches of a selected object and the dimensions in inches of the active artboard. It creates a line and prints text. This works great on a standard canvas. If you hit file>new and change nothing they both work as intended.
I have certain projects have up to 28 walls I need to create digital renders of and so I've made a file template that has a canvas size larger than 20,000 pixels to accomodate. On the larger canvas size, all my scripts are scaled up. My 80 pt font becomes 800 pt, my 6 pt stroke becomes 60 pt, etc.
I found another post that pointed to the large document scale factor being set to 10 instead of the regular scale factor of 1, and that can be seen through "app.activeDocument.scaleFactor". That post can be seen in more depth here.
I'm wondering if there's a way to get my script to work as intended on the large format canvas that keeps my intended scale! Any suggestions would be awesome, thanks! :))
Explore related tutorials & articles
Copy link to clipboard
Copied
Hi @Mikey_Englar1374, if I've understood your code correctly, see my attached versions of your scripts.
I only changed 3 lines. First store the scaleFactor:
var sf = doc.scaleFactor;
And then used it when calculating the dimensions. I also rejigged these lines to do the rounding in math rather than using string conversion, so better semantically:
var textWidth = (Math.round((objWidth / 10) * sf * 1000) / 1000) + '"';
var textHeight = (Math.round((objHeight / 10) * sf * 1000) / 1000) + '"';
Let me know if that works.
- Mark
Copy link to clipboard
Copied
Hey Mark @m1b! Thanks so much for looking into this! This solves one of the issues the script is having for sure! The problem your changes solve is to the printed output! For example, instead of 8.43" printing it prints the intended 84.3" so the value is correct, however I still am having an issue with the scale when it comes to generating things! I've attached screenshots to further explain!
Again, my previous code works on standard canvas size, but on the Large Scale canvas size the things that are generated are 10x to large. Hope that makes sense!
Copy link to clipboard
Copied
Hi @Mikey_Englar1374, sorry I should have thought to point out how Large Scale Documents work in the scripting context:
1. Documents have a 'scaleFactor' property which is 1 for a normal doc, and 10 for a large scale doc.
2. Nothing else! That scaleFactor property is the only difference between the two types of documents.
So from the point of view of a script, your large scale document is actually 1/10 of the size the UI says it is, and your script must take it into account. So if you want to make something 10" wide on a large scale document, you make is 1" wide. Try it!
Basically it means that our scripts must constantly factor in the scaleFactor whenever sizing things (or reporting on size, as I changed above). For example here is a function that draws a rectangle, you can see how I incorporated both scaleFactor and "unitFactor" (there are 72 points in 1 inch).
(function () {
const inch = 72;
var doc = app.activeDocument;
var myRectangle = drawRectangle(doc, [5, 10, 15, 20], inch);
})();
/**
* Draws a rectangle in the container.
* Takes Large Scale Documents into account.
* @author m1b
* @version 2024-09-26
* @param {Document} doc - an Illustrator document.
* @param {Array<Number>} rect - the rectangle dimensions [left, top, width, height].
* @param {Number} [unitFactor] - eg. 72 to convert to inches. All values will be scaled by this (default: 1).
* @param {Document|Layer|GroupItem} container - an Illustrator page item container (default: doc).
* @param {Object} [props] - properties to assign to the rectangle (default: none).
* @return {PathItem}
*/
function drawRectangle(doc, rect, unitFactor, container, properties) {
properties = properties || {};
unitFactor = unitFactor || 1;
container = container || doc;
var sf = 1 / doc.scaleFactor * unitFactor;
var rectangle = container.pathItems.rectangle(-rect[1] * sf, rect[0] * sf, rect[2] * sf, rect[3] * sf); // T,L,W,H
// defaults
rectangle.filled = true;
rectangle.stroked = false;
// apply properties
for (var key in properties)
if (properties.hasOwnProperty(key))
rectangle[key] = properties[key];
return rectangle;
};
HOWEVER, I made a bad mistake when I adjusted your scripts earlier. I forgot you wanted the print outs in inches, so we must adjust for that. Also I had an incorrect divide by 10 in there—no idea why! Could you change those calculations to these please:
var textWidth = (Math.round((objWidth / inch) * sf * 1000) / 1000) + '"';
var textHeight = (Math.round((objHeight / inch) * sf * 1000) / 1000) + '"';
and
var textWidth = (Math.round((abWidth / inch) * sf * 1000) / 1000) + '"';
var textHeight = (Math.round((abHeight / inch) * sf * 1000) / 1000) + '"';
and, at the start of your script define a variable (or constant):
const inch = 72;
And I hope everything will be back on track. Sorry I messed that up.
- Mark

