Skip to main content
tokuredit
Inspiring
December 1, 2021
Answered

Check the document for a margin or border

  • December 1, 2021
  • 5 replies
  • 674 views

Good afternoon everyone!
Is there any way for a script to check if there is a 5mm white margin or border in the document?


If it's possible to share some script, I'll be very grateful.

This topic has been closed for replies.
Correct answer Shulipa Bernad

Based on the @Chuck Uebele  tip, this script checks whether or not the document has borders of any size.

 

main();
function main(){
    var startRulerUnits = app.preferences.rulerUnits;   
    app.preferences.rulerUnits = Units.PIXELS; 
    var doc = activeDocument; 
    doc.selection.selectAll();
    notBorder();
    var x1 = Number(doc.width); 
    var x2=  Number(doc.selection.bounds[2]);
    doc.selection.deselect();
    if(x1 == x2){alert ("It has no border")} 
    else{alert ("Has border")}
    app.preferences.rulerUnits =  startRulerUnits;
}

function notBorder(){
    var d1 = new ActionDescriptor();
    var r = new ActionReference();
    r.putProperty(app.charIDToTypeID("Chnl"), app.stringIDToTypeID("selection"));
    d1.putReference(app.charIDToTypeID("null"), r);
    var d2 = new ActionDescriptor();
    d2.putUnitDouble(app.charIDToTypeID("Hrzn"), app.charIDToTypeID("#Pxl"), 0);
    d2.putUnitDouble(app.charIDToTypeID("Vrtc"), app.charIDToTypeID("#Pxl"), 0);
    d1.putObject(app.charIDToTypeID("T   "), app.charIDToTypeID("Pnt "), d2);
    executeAction(app.stringIDToTypeID("subtractFrom"), d1, DialogModes.NO);
}

 

5 replies

Kukurykus
Legend
December 2, 2021

Where is 'Layout'?

tokuredit
tokureditAuthor
Inspiring
December 1, 2021

Perfect! All three solutions work perfectly!
@Chuck Uebele, @jazz-y, @Shulipa Bernad , Thanks for sharing.

Shulipa Bernad
Shulipa BernadCorrect answer
Inspiring
December 1, 2021

Based on the @Chuck Uebele  tip, this script checks whether or not the document has borders of any size.

 

main();
function main(){
    var startRulerUnits = app.preferences.rulerUnits;   
    app.preferences.rulerUnits = Units.PIXELS; 
    var doc = activeDocument; 
    doc.selection.selectAll();
    notBorder();
    var x1 = Number(doc.width); 
    var x2=  Number(doc.selection.bounds[2]);
    doc.selection.deselect();
    if(x1 == x2){alert ("It has no border")} 
    else{alert ("Has border")}
    app.preferences.rulerUnits =  startRulerUnits;
}

function notBorder(){
    var d1 = new ActionDescriptor();
    var r = new ActionReference();
    r.putProperty(app.charIDToTypeID("Chnl"), app.stringIDToTypeID("selection"));
    d1.putReference(app.charIDToTypeID("null"), r);
    var d2 = new ActionDescriptor();
    d2.putUnitDouble(app.charIDToTypeID("Hrzn"), app.charIDToTypeID("#Pxl"), 0);
    d2.putUnitDouble(app.charIDToTypeID("Vrtc"), app.charIDToTypeID("#Pxl"), 0);
    d1.putObject(app.charIDToTypeID("T   "), app.charIDToTypeID("Pnt "), d2);
    executeAction(app.stringIDToTypeID("subtractFrom"), d1, DialogModes.NO);
}

 

Legend
December 1, 2021

Script checks the size of the white border on all sides of the document. If on at least one side it exceeds the allowable threshold  from the specified size, then the script reports an error.

 

 

#target photoshop

var border = 5, //mm
    threshold = 0.1, //mm, allowable deviation from the size of the border 
    doc = new AM('document'),
    res = doc.getProperty('resolution'),
    dW = doc.getProperty('width') * res / 72,
    dH = doc.getProperty('height') * res / 72;

doc.makeSelectionFromChannel('RGB')
doc.inverseSelection()
var lr = doc.descToObject(doc.getProperty('selection').value),
    margins = {
        left: lr.left / res * 25.4,
        top: lr.top / res * 25.4,
        right: (dW - lr.right) / res * 25.4,
        bottom: (dH - lr.bottom) / res * 25.4
    },
    err = (function (o, size) {
        for (var a in o) { if (Math.abs(size - o[a]) > threshold) return true };
        return false;
    })(margins, border);

if (!err) alert('Ok!') else alert('Not ok!\n' + margins.toSource())
doc.deselect()

function AM(target) {
    var s2t = stringIDToTypeID,
        t2s = typeIDToStringID;

    target = target ? s2t(target) : null;

    this.getProperty = function (property, id, idxMode) {
        property = s2t(property);
        (r = new ActionReference()).putProperty(s2t('property'), property);
        id != undefined ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id)) :
            r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
        return getDescValue(executeActionGet(r), property)
    }

    this.hasProperty = function (property, id, idxMode) {
        property = s2t(property);
        (r = new ActionReference()).putProperty(s2t('property'), property);
        id ? (idxMode ? r.putIndex(target, id) : r.putIdentifier(target, id))
            : r.putEnumerated(target, s2t('ordinal'), s2t('targetEnum'));
        try { return executeActionGet(r).hasKey(property) } catch (e) { return false }
    }

    this.descToObject = function (d) {
        var o = {}
        for (var i = 0; i < d.count; i++) {
            var k = d.getKey(i)
            o[t2s(k)] = getDescValue(d, k)
        }
        return o
    }

    this.makeSelectionFromChannel = function (channel) {
        (r = new ActionReference()).putProperty(s2t('channel'), s2t('selection'));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        (r1 = new ActionReference()).putEnumerated(s2t('channel'), s2t('channel'), s2t(channel));
        d.putReference(s2t('to'), r1)
        executeAction(s2t('set'), d, DialogModes.NO)
    }

    this.deselect = function () {
        (r = new ActionReference()).putProperty(s2t('channel'), s2t('selection'));
        (d = new ActionDescriptor()).putReference(s2t('null'), r);
        d.putEnumerated(s2t('to'), s2t('ordinal'), s2t('none'));
        executeAction(s2t('set'), d, DialogModes.NO);
    }

    this.inverseSelection = function () {
        executeAction(s2t('inverse'), undefined, DialogModes.NO);
    }

    function getDescValue(d, k) {
        switch (d.getType(k)) {
            case DescValueType.OBJECTTYPE: return { type: t2s(d.getObjectType(k)), value: d.getObjectValue(k) };
            case DescValueType.LISTTYPE: return d.getList(k);
            case DescValueType.REFERENCETYPE: return d.getReference(k);
            case DescValueType.BOOLEANTYPE: return d.getBoolean(k);
            case DescValueType.STRINGTYPE: return d.getString(k);
            case DescValueType.INTEGERTYPE: return d.getInteger(k);
            case DescValueType.LARGEINTEGERTYPE: return d.getLargeInteger(k);
            case DescValueType.DOUBLETYPE: return d.getDouble(k);
            case DescValueType.ALIASTYPE: return d.getPath(k);
            case DescValueType.CLASSTYPE: return d.getClass(k);
            case DescValueType.UNITDOUBLE: return (d.getUnitDoubleValue(k));
            case DescValueType.ENUMERATEDTYPE: return { type: t2s(d.getEnumerationType(k)), value: t2s(d.getEnumerationValue(k)) };
            default: break;
        };
    }
}

 

 

Kukurykus
Legend
December 2, 2021

Can you upload examplary image, since each I try your script on gives 'no ok' result. Okay I did it.

Chuck Uebele
Community Expert
Community Expert
December 1, 2021

I would use scriptListener to:

Select all

Then use magic wand in subtract mode to select a point on the margin. 

Get the size of the full doc

Get the size of the selection

Subtract the two and divide by 2

That should give you the size

 

tokuredit
tokureditAuthor
Inspiring
December 1, 2021

Thanks @Chuck Uebele , I get it! I'll try to follow your tip when I get home, then come back to report the result.