• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help me modify the script to fit the width of the current column, thank you ~

Enthusiast ,
Mar 25, 2021 Mar 25, 2021

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.

abc-02.jpg

------------------------------

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;  
} 

 

TOPICS
Bug , Feature request , How to , Scripting , Type

Views

1.2K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 29, 2021 Mar 29, 2021

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
...

Votes

Translate

Translate
Community Expert ,
Mar 25, 2021 Mar 25, 2021

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:

 

Screen Shot 1.pngScreen Shot 2.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 27, 2021 Mar 27, 2021

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

If you want this:

 

Screen Shot 5.pngScreen Shot 7.png

 

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:

 

Screen Shot 6.png

 

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;

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 27, 2021 Mar 27, 2021

Copy link to clipboard

Copied

Or if you want to move the selected text frame to the top of the closest column:

 

Screen Shot 9.pngScreen Shot 10.png

 

//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;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 28, 2021 Mar 28, 2021

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~

eebb.jpgddee.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 28, 2021 Mar 28, 2021

Copy link to clipboard

Copied

Screen Shot 13.png

Screen Shot 14.png

 

 

 

//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;

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 29, 2021 Mar 29, 2021

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 29, 2021 Mar 29, 2021

Copy link to clipboard

Copied

Also, if the text box is not in the type page, the script is invalid

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2021 Mar 29, 2021

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;

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 10, 2021 Apr 10, 2021

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,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 21, 2021 Apr 21, 2021

Copy link to clipboard

Copied

There’s no alignment, the text frame resizes to the column. So if there’s one column.

 

Screen Shot 11.pngScreen Shot 12.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 22, 2021 Apr 22, 2021

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 04, 2021 May 04, 2021

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

before.jpgafter.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 04, 2021 May 04, 2021

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2021 May 05, 2021

Copy link to clipboard

Copied

Not seeing a problem. If you select 2 frames it will only move the first item of the selection.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 05, 2021 May 05, 2021

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)

margin.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 05, 2021 May 05, 2021

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
}

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 05, 2021 May 05, 2021

Copy link to clipboard

Copied

LATEST

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~

 

2.jpg3.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines