Skip to main content
Known Participant
November 18, 2022
Answered

Script for divide split slices into arrays of certain length?

  • November 18, 2022
  • 2 replies
  • 1466 views

Hello everyone, I'm looking for i need a script that can split an image into equal horizontal slices, with each slice length greater than 1000px and less than 2000px. By the way, I could not use "pixels per slice" at divide slice dialog because I can't control the last slice of the image. I usually look at the length of the image, then divide by 2000 and get the nearest higher value.

For example, I have a 600x8000px file. 8000 divide by 2000, the result is 4, and I will divide the file into 5 slices. There are many files with different sizes, I can't split each of the multiple files. So if there is any possible solution, please point me out. Thank you very much!

This topic has been closed for replies.
Correct answer Chuck Uebele

Try this.

#target photoshop 

var doc = activeDocument;
var oldPrefs = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.PIXELS;
var dH = doc.height.value;
var splits = parseInt( dH/2000 + 1);

var sliceAmt = dH/splits

for (var i=0;i<splits;i++){
    if(i + 1 == splits){
        sliceImg (i*sliceAmt, 0, doc.height.value, doc.width.value)
        }
    sliceImg (i*sliceAmt, 0, (i+1) * sliceAmt, doc.width.value)
    }

app.preferences.rulerUnits = oldPrefs;

function sliceImg(topS, leftS, bottomS, rightS){
    var idMk = charIDToTypeID( "Mk  " );
        var desc4 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idslice = stringIDToTypeID( "slice" );
            ref1.putClass( idslice );
        desc4.putReference( idnull, ref1 );
        var idUsng = charIDToTypeID( "Usng" );
            var desc5 = new ActionDescriptor();
            var idType = charIDToTypeID( "Type" );
            var idsliceType = stringIDToTypeID( "sliceType" );
            var iduser = stringIDToTypeID( "user" );
            desc5.putEnumerated( idType, idsliceType, iduser );
            var idAt = charIDToTypeID( "At  " );
                var desc6 = new ActionDescriptor();
                var idTop = charIDToTypeID( "Top " );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idTop, idPxl, topS );
                var idLeft = charIDToTypeID( "Left" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idLeft, idPxl, leftS );
                var idBtom = charIDToTypeID( "Btom" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idBtom, idPxl, bottomS );
                var idRght = charIDToTypeID( "Rght" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idRght, idPxl, rightS );
            var idRctn = charIDToTypeID( "Rctn" );
            desc5.putObject( idAt, idRctn, desc6 );
        var idslice = stringIDToTypeID( "slice" );
        desc4.putObject( idUsng, idslice, desc5 );
    executeAction( idMk, desc4, DialogModes.NO );    
    }

2 replies

Chuck Uebele
Community Expert
Chuck UebeleCommunity ExpertCorrect answer
Community Expert
November 19, 2022

Try this.

#target photoshop 

var doc = activeDocument;
var oldPrefs = app.preferences.rulerUnits
app.preferences.rulerUnits = Units.PIXELS;
var dH = doc.height.value;
var splits = parseInt( dH/2000 + 1);

var sliceAmt = dH/splits

for (var i=0;i<splits;i++){
    if(i + 1 == splits){
        sliceImg (i*sliceAmt, 0, doc.height.value, doc.width.value)
        }
    sliceImg (i*sliceAmt, 0, (i+1) * sliceAmt, doc.width.value)
    }

app.preferences.rulerUnits = oldPrefs;

function sliceImg(topS, leftS, bottomS, rightS){
    var idMk = charIDToTypeID( "Mk  " );
        var desc4 = new ActionDescriptor();
        var idnull = charIDToTypeID( "null" );
            var ref1 = new ActionReference();
            var idslice = stringIDToTypeID( "slice" );
            ref1.putClass( idslice );
        desc4.putReference( idnull, ref1 );
        var idUsng = charIDToTypeID( "Usng" );
            var desc5 = new ActionDescriptor();
            var idType = charIDToTypeID( "Type" );
            var idsliceType = stringIDToTypeID( "sliceType" );
            var iduser = stringIDToTypeID( "user" );
            desc5.putEnumerated( idType, idsliceType, iduser );
            var idAt = charIDToTypeID( "At  " );
                var desc6 = new ActionDescriptor();
                var idTop = charIDToTypeID( "Top " );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idTop, idPxl, topS );
                var idLeft = charIDToTypeID( "Left" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idLeft, idPxl, leftS );
                var idBtom = charIDToTypeID( "Btom" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idBtom, idPxl, bottomS );
                var idRght = charIDToTypeID( "Rght" );
                var idPxl = charIDToTypeID( "#Pxl" );
                desc6.putUnitDouble( idRght, idPxl, rightS );
            var idRctn = charIDToTypeID( "Rctn" );
            desc5.putObject( idAt, idRctn, desc6 );
        var idslice = stringIDToTypeID( "slice" );
        desc4.putObject( idUsng, idslice, desc5 );
    executeAction( idMk, desc4, DialogModes.NO );    
    }
Known Participant
November 19, 2022

Many thanks! This script works for me, really thank you!

Chuck Uebele
Community Expert
Community Expert
November 19, 2022

Glad it works for you!

Known Participant
November 19, 2022

Thank you for the answer. I just looked through the links, they don't seem to suit my intentions well, sorry about my poor wording (but they are very useful anyway, thanks a lot)

Do you know any solution to create slices (not export them, just divide into slice) via canvas size?