Copy link to clipboard
Copied
The script is to fit the frame to the width of the column.
Now there is a problem that it will fit the column to the width of the leftmost column.
It should fit the width of the column in the upper left corner, and the Y coordinate should not change.
Gods, who can help me revise it?
Thank you very much.
------------------------------
var curDoc = app.activeDocument;
var curSel = app.selection[0];
var gB = curSel.geometricBounds;
// Masseinheiten, Ursprung und Nullpunkt speichern und bei Bedarf verändern
hM = curDoc.viewPreferences.horizontalMeasurementUnits;
vM = curDoc.viewPreferences.verticalMeasurementUnits;
rO = curDoc.viewPreferences.rulerOrigin;
zP = curDoc.zeroPoint;
_setPref();
// Variablen initialisieren
var pWidth = curDoc.documentPreferences.pageWidth;
pWidth = Math.floor((pWidth * 1000) + 0.5) / 1000;
var pHeight = curDoc.documentPreferences.pageHeight;
pHeight = Math.floor((pHeight * 1000) + 0.5) / 1000;
var curPage = app.activeWindow.activePage;
// weitere Variablen deklarieren
var curPage;
var pM;
var curPos;
var allPos = new Array;
if(curPage.side == PageSideOptions.leftHand) {
// bei Doppelseiten bedeutet left > innen; right > außen
pM = curPage.marginPreferences.right;
}
else{
// entweder Einzelseite oder rechte Seite
pM = curPage.marginPreferences.left;
}
// die Anzahl der Spalten auf der Seite
var nC = curPage.marginPreferences.columnCount;
// die Positionen der Spaltenlinien
var colPos = curPage.marginPreferences.columnsPositions;
// Schleife durch alle Positionen, der Spaltenlinien, Werte runden
for (var k=0; k<colPos.length; k++) {
curPos = (Math.floor((colPos[k] * 1000) + 0.5) / 1000) + pM;
allPos.push(curPos);
}
gB[1] = allPos[0];
gB[3] = allPos[1];
curSel.geometricBounds = gB;
if (curSel.constructor.name == "TextFrame") fitFrame(curSel);
_reStorePref();
// --------------------- Funktionen -------------------------
function fitFrame(aFrame) {
var gB = aFrame.geometricBounds;
gB[2] = gB[2]+ 50;
aFrame.geometricBounds = gB;
var lastBaseLine = aFrame.lines.lastItem().baseline;
aFrame.geometricBounds = [ gB[0], gB[1], lastBaseLine, gB[3] ];
}
// Voreinstellungen verändern und zurücksetzen
function _setPref() {
curDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.millimeters;
curDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.millimeters;
curDoc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
curDoc.zeroPoint = [0,0];
}
function _reStorePref() {
curDoc.viewPreferences.horizontalMeasurementUnits = hM;
curDoc.viewPreferences.verticalMeasurementUnits = vM;
curDoc.viewPreferences.rulerOrigin = rO;
curDoc.zeroPoint = zP;
}
The right page problem might be the ruler origin, setting the rulers to page origin should fix that.
Also the script is checking the left edge of the selected text frame to determine which column to center the frame in. If the frame is in the pasteboard there will be an error because there is no parent page. If the left edge is in the margins you might assume it sould be moved to either the inside or outside column. So this version checks if the left edge is in the margins.
//set zero po
...
Copy link to clipboard
Copied
I’m not sure the code needs to be that complex. In this case you don’t need to save and set the ruler units because the bounds and column positions return in the document’s units. Does this work?:
//set zero point and get selection
var zp = app.activeDocument.zeroPoint;
app.activeDocument.zeroPoint = [0,0];
var curSel = app.activeDocument.selection[0];
//the selection’s page
var pp = curSel.parentPage;
//selection bounds
var sb = curSel.geometricBounds;
//the left margin, and an array of column postions from left to right
var lm = pp.marginPreferences.left
var cp = pp.marginPreferences.columnsPositions;
//left and right bounds of the outside column for a right side page
var rlb = cp[cp.length-2]+lm;
var rrb = cp[cp.length-1]+lm;
//for a leftside
var llb = cp[0]+lm;
var lrb = cp[1]+lm;
//if the selection is a text frame set its bounds to the outside column.
if (curSel.constructor.name == "TextFrame") {
if (pp.side == 1919382632) {
curSel.geometricBounds = [sb[0], rlb, sb[2], rrb]
}else {
curSel.geometricBounds = [sb[0], llb, sb[2], lrb]
}
}
app.activeDocument.zeroPoint = zp;
Before and after run:
Copy link to clipboard
Copied
Dear ~
I've tried the code you provided and it works, but it's still wrong.
What I want is to fit the bar in the upper left corner of the frame.
Copy link to clipboard
Copied
If you want this:
The code would be:
//set zero point and get selection
var zp = app.activeDocument.zeroPoint;
app.activeDocument.zeroPoint = [0,0];
var curSel = app.activeDocument.selection[0];
//the selection’s page
var pp = curSel.parentPage;
//selection bounds
var sb = curSel.geometricBounds;
//the left & top margin, and an array of column postions from left to right
var lm = pp.marginPreferences.left;
var tm = pp.marginPreferences.top
var cp = pp.marginPreferences.columnsPositions;
var lrb = cp[1]+lm;
//if the selection is a text frame
if (curSel.constructor.name == "TextFrame") {
curSel.geometricBounds = [tm, lm, sb[2], lrb]
}
app.activeDocument.zeroPoint = zp;
If you want this where the text frame fits to the top of the outside columns:
Then:
//set zero point and get selection
var zp = app.activeDocument.zeroPoint;
app.activeDocument.zeroPoint = [0,0];
var curSel = app.activeDocument.selection[0];
//the selection’s page
var pp = curSel.parentPage;
//selection bounds
var sb = curSel.geometricBounds;
//the left margin, and an array of column postions from left to right
var lm = pp.marginPreferences.left;
var tm = pp.marginPreferences.top
var cp = pp.marginPreferences.columnsPositions;
//left and right bounds of the outside column for a right side page
var rlb = cp[cp.length-2]+lm;
var rrb = cp[cp.length-1]+lm;
//for a leftside
var llb = cp[0]+lm;
var lrb = cp[1]+lm;
//if the selection is a text frame.
if (curSel.constructor.name == "TextFrame") {
if (pp.side == 1919382632) {
curSel.geometricBounds = [tm, rlb, sb[2], rrb]
}else {
curSel.geometricBounds = [tm, llb, sb[2], lrb]
}
}
app.activeDocument.zeroPoint = zp;
Copy link to clipboard
Copied
Or if you want to move the selected text frame to the top of the closest column:
//set zero point and get selection
var zp = app.activeDocument.zeroPoint;
app.activeDocument.zeroPoint = [0,0];
var curSel = app.activeDocument.selection[0];
//the selection’s page
var pp = curSel.parentPage;
//selection bounds
var sb = curSel.geometricBounds;
//the left margin, and an array of column postions from left to right
var tm = pp.marginPreferences.top
var lm = pp.marginPreferences.left
var cp = pp.marginPreferences.columnsPositions;
//the last column position
var lc = cp[cp.length-1]+lm;
var tc, lcb, rcb;
// moves the selection to the closest column
for (var i = 0; i < cp.length; i++){
if (i%2 == 0) {
tc = Math.abs((cp[i]+lm)-sb[1]);
if (tc < lc) {
lc = tc;
lcb = cp[i]+lm
rcb = cp[i+1]+lm
}
}
};
curSel.geometricBounds = [tm, lcb, sb[2], rcb]
app.activeDocument.zeroPoint = zp;
Copy link to clipboard
Copied
I hope:
Align the top left corner of the text box to the left of the column it is in, and move it horizontally
Width adapts to column width.
At the same time, the height of the text box adapts to the height of the text content.
As shown in the figure below:
Thank you~
Copy link to clipboard
Copied
//set zero point and get selection
var zp = app.activeDocument.zeroPoint;
app.activeDocument.zeroPoint = [0,0];
var curSel = app.activeDocument.selection[0];
//the selection’s page
var pp = curSel.parentPage;
//selection bounds
var sb = curSel.visibleBounds;
//the left margin, and an array of column postions from left to right
var tm = pp.marginPreferences.top
var lm = pp.marginPreferences.left
var cp = pp.marginPreferences.columnsPositions;
var lcb, rcb;
//get the new left and right bounds based on the selection’s left edge
for (var i = 0; i < cp.length; i++){
if (sb[1] >= cp[i]+lm && sb[1] <= cp[i+1]+lm) {
lcb = cp[i]+lm;
rcb = cp[(i+1)]+lm
}
};
try {
curSel.visibleBounds = [sb[0], lcb, sb[2], rcb];
curSel.fit(FitOptions.FRAME_TO_CONTENT)
}catch(e) {}
app.activeDocument.zeroPoint = zp;
Copy link to clipboard
Copied
Dear rob day
For the left page, the script is right this time.
But for the right page, the script is invalid and has no response.
Thank you very much
Copy link to clipboard
Copied
Also, if the text box is not in the type page, the script is invalid
Copy link to clipboard
Copied
The right page problem might be the ruler origin, setting the rulers to page origin should fix that.
Also the script is checking the left edge of the selected text frame to determine which column to center the frame in. If the frame is in the pasteboard there will be an error because there is no parent page. If the left edge is in the margins you might assume it sould be moved to either the inside or outside column. So this version checks if the left edge is in the margins.
//set zero point, rulers and get selection
var zp = app.activeDocument.zeroPoint;
var ro = app.activeDocument.viewPreferences.rulerOrigin;
app.activeDocument.zeroPoint = [0,0];
app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
var curSel = app.activeDocument.selection[0];
//the selection’s page
var pp = curSel.parentPage;
//selection bounds
var sb = curSel.visibleBounds;
//the left margin, and an array of column postions from left to right
var tm = pp.marginPreferences.top
var lm = pp.marginPreferences.left
var cp = pp.marginPreferences.columnsPositions;
var lcb, rcb;
//get the new left and right bounds based on the selection’s left edge
for (var i = 0; i < cp.length; i++){
if (sb[1] >= cp[i]+lm && sb[1] <= cp[i+1]+lm) {
lcb = cp[i]+lm;
rcb = cp[(i+1)]+lm
}else if (sb[1] < cp[0]+lm){
lcb = cp[0]+lm;
rcb = cp[1]+lm
}else if (sb[1] > cp[cp.length-1]){
lcb = cp[cp.length-1]+lm;
rcb = cp[cp.length-2]+lm
}
};
try {
curSel.visibleBounds = [sb[0], lcb, sb[2], rcb];
curSel.fit(FitOptions.FRAME_TO_CONTENT)
}catch(e) {}
app.activeDocument.zeroPoint = zp;
app.activeDocument.viewPreferences.rulerOrigin = ro;
Copy link to clipboard
Copied
Suddenly, if there is only one column, the script will not work.
When one column (not divided into columns), it should be aligned left and right,
Copy link to clipboard
Copied
There’s no alignment, the text frame resizes to the column. So if there’s one column.
Copy link to clipboard
Copied
Sorry, I just tried. It's OK.
I may have forgotten to update the last one a few days ago.
Copy link to clipboard
Copied
Rob day, the script you modified for me doesn't work any more. If there are two pages on the left and right with two columns, it's right to execute the script on the right page, but it's wrong on the left page with errors
Copy link to clipboard
Copied
Can you modify it based on the script I originally provided?
I think that script is a bit complicated, but it contains most possible situations
Copy link to clipboard
Copied
Not seeing a problem. If you select 2 frames it will only move the first item of the selection.
Copy link to clipboard
Copied
If the inner and outer margins are the same, your script runs correctly.
If the left and right margins are different, there is a problem.
You can try it. 32 on the inside and 21 on the outside.
The ID file is here(only 21days)
Copy link to clipboard
Copied
Looks like marginPreferences.left actually refers to the inside margin with facing pages, so replace:
var lm = pp.marginPreferences.left
with
var lm;
if (pp.side == PageSideOptions.RIGHT_HAND) {
lm = pp.marginPreferences.left
} else {
lm = pp.marginPreferences.right
}
Copy link to clipboard
Copied
It's ready to run.
Only when the upper left corner of the text box is on the two column line, or the inner edge is empty, an error occurs.
It doesn't matter. Just enough.
Thank you very much~
Copy link to clipboard
Copied
Hello, great@rob day
I finally found you.
It seems like you were the one who originally helped me modify the script.
Now it is right for text boxes, since when I don't know, it doesn't seem to work for images or regular frames.
Can you make the script differentiate between text frames, images, and regular frames?
① If it's a text frame, use the current function (auto-fit column width, or typearea).
②If it's a picture or a normal frame: zoom in proportionally to the width and fit the column width or typearea
Thank you very much.
This is the latest version. Fhe first script(Fit to ttypearea):
// the current document
//Fit to ttypearea
var curDoc = app.activeDocument;
// there must be one selection with the selection-tool and the selection must be a textframe
if ( app.selection.length != 1 || app.selection[0].constructor.name != "TextFrame" ) {
( alert ( "please select textframe" ) );
exit();
}
// the current selection
var curSel = app.selection[0];
// the page of the current selection
var curPage = curSel.parentPage;
// set the zeroPoint to 0
var zP = curDoc.zeroPoint;
if (zP != [0,0]) {
curDoc.zeroPoint = [0,0];
}
// set the ruler to page origin and store the user-pref
var rO = curDoc.viewPreferences.rulerOrigin;
if (rO != 1380143215) {
curDoc.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
}
// page width and height
var pW = curDoc.documentPreferences.pageWidth;
var pH = curDoc.documentPreferences.pageHeight;
var y1, x1, y2, x2;
// check if the page is a single, left or right page and store the margins
if ( curPage.side == PageSideOptions.leftHand ){
x2 = curPage.marginPreferences.left;
x1 = curPage.marginPreferences.right;
}
else {
x1 = curPage.marginPreferences.left;
x2 = curPage.marginPreferences.right;
}
y1 = curPage.marginPreferences.top;
x2 = pW - x2;
y2 = pH - curPage.marginPreferences.bottom;
var gB = curSel.geometricBounds;
// fit to margins
gB[0] = y1;
gB[1] = x1;
gB[2] = y2;
gB[3] = x2;
curSel.geometricBounds = gB;
// fitFrame(curSel);
// set the zeroPoint and ruler to the original state
resetAsBefore();
function fitFrame(aFrame) {
var gB = aFrame.geometricBounds;
gB[2] = gB[2]+ 50;
aFrame.geometricBounds = gB;
var lastBaseLine = aFrame.lines.lastItem().baseline;
aFrame.geometricBounds = [ gB[0], gB[1], lastBaseLine, gB[3] ];
}
function resetAsBefore() {
if ( rO != 1380143215 ) {
curDoc.viewPreferences.rulerOrigin = rO;
}
// set the zeroPoint back to the origin state
if (zP != [0,0]) {
curDoc.zeroPoint = zP;
}
}
The second script(Fit to column)
//set zero point, rulers and get selection
//Fit to column
var zp = app.activeDocument.zeroPoint;
var ro = app.activeDocument.viewPreferences.rulerOrigin;
app.activeDocument.zeroPoint = [0,0];
app.activeDocument.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
var curSel = app.activeDocument.selection[0];
//the selection’s page
var pp = curSel.parentPage;
//selection bounds
var sb = curSel.visibleBounds;
//the left margin, and an array of column postions from left to right
var tm = pp.marginPreferences.top
var lm;
if (pp.side == PageSideOptions.RIGHT_HAND) {
lm = pp.marginPreferences.left
} else {
lm = pp.marginPreferences.right
};
var cp = pp.marginPreferences.columnsPositions;
var lcb, rcb;
//get the new left and right bounds based on the selection’s left edge
for (var i = 0; i < cp.length; i++){
if (sb[1] >= cp[i]+lm && sb[1] <= cp[i+1]+lm) {
lcb = cp[i]+lm;
rcb = cp[(i+1)]+lm
}else if (sb[1] < cp[0]+lm){
lcb = cp[0]+lm;
rcb = cp[1]+lm
}else if (sb[1] > cp[cp.length-1]){
lcb = cp[cp.length-1]+lm;
rcb = cp[cp.length-2]+lm
}
};
try {
curSel.visibleBounds = [sb[0], lcb, sb[2], rcb];
curSel.fit(FitOptions.FRAME_TO_CONTENT)
}catch(e) {}
app.activeDocument.zeroPoint = zp;
app.activeDocument.viewPreferences.rulerOrigin = ro;