Skip to main content
jdirks73
Participant
June 15, 2016
Question

PSD Script and slices

  • June 15, 2016
  • 3 replies
  • 2955 views

How do I access the PSD document slice data in photoshop script?

I would like to create a script that will export the slice coordinates, size and name to a text file.

I do not want to use the export asset feature, as I am creating a sprite sheet/atlas for a game engine that requires "power of 2", so exporting the slices using "save for web" does not work.

Also, using "Create New Layer Based Slice" does not work for me because I cannot control the slice (position, alpha bleed) that it creates from the layer.

Thanks

This topic has been closed for replies.

3 replies

Kogi18
Inspiring
December 10, 2021
JJMack
Community Expert
Community Expert
June 15, 2016

The only thing I see in Adobe Photoshop JavaScript reference for Slice is for Photoshop Preferences.  So all there seems to be in Adobe Photoshop DOM is ane interface to your Photoshop Preferences for slice.   Install Adobe  Scriptlistener Plug-in see if anything is recorded when you use Photoshop Slice tool and the slices you create.  I do not do Web stuff so all I know about the slice tool is you can slice up an images and associate a links with the slices.   I know there must be coordinates  you may me able to retrieve that information with Action Manager code.

JJMack
simonl25896876
Participant
May 22, 2017

JJMack  a écrit

Install Adobe  Scriptlistener Plug-in see if anything is recorded when you use Photoshop Slice tool and the slices you create. [...] I know there must be coordinates  you may me able to retrieve that information with Action Manager code.

Here are different output from scriptingListener :

Creating a slice :

var idMk = charIDToTypeID( "Mk  " );

    var desc111 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref28 = new ActionReference();

        var idslice = stringIDToTypeID( "slice" );

        ref28.putClass( idslice );

    desc111.putReference( idnull, ref28 );

    var idUsng = charIDToTypeID( "Usng" );

        var desc112 = new ActionDescriptor();

        var idType = charIDToTypeID( "Type" );

        var idsliceType = stringIDToTypeID( "sliceType" );

        var iduser = stringIDToTypeID( "user" );

        desc112.putEnumerated( idType, idsliceType, iduser );

        var idAt = charIDToTypeID( "At  " );

            var desc113 = new ActionDescriptor();

            var idTop = charIDToTypeID( "Top " );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc113.putUnitDouble( idTop, idPxl, 904.000000 );

            var idLeft = charIDToTypeID( "Left" );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc113.putUnitDouble( idLeft, idPxl, 487.000000 );

            var idBtom = charIDToTypeID( "Btom" );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc113.putUnitDouble( idBtom, idPxl, 972.000000 );

            var idRght = charIDToTypeID( "Rght" );

            var idPxl = charIDToTypeID( "#Pxl" );

            desc113.putUnitDouble( idRght, idPxl, 579.000000 );

        var idRctn = charIDToTypeID( "Rctn" );

        desc112.putObject( idAt, idRctn, desc113 );

    var idslice = stringIDToTypeID( "slice" );

    desc111.putObject( idUsng, idslice, desc112 );

executeAction( idMk, desc111, DialogModes.

Select a slice :

// =======================================================

var idslct = charIDToTypeID( "slct" );

    var desc119 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref33 = new ActionReference();

        var idslice = stringIDToTypeID( "slice" );

        ref33.putClass( idslice );

    desc119.putReference( idnull, ref33 );

    var idAt = charIDToTypeID( "At  " );

        var desc120 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc120.putUnitDouble( idHrzn, idPxl, 95.000000 ); //This is where I click in my psd

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc120.putUnitDouble( idVrtc, idPxl, 227.000000 ); //This is where I click in my psd

    var idPnt = charIDToTypeID( "Pnt " );

    desc119.putObject( idAt, idPnt, desc120 );

    var idaddToSelection = stringIDToTypeID( "addToSelection" );

    desc119.putBoolean( idaddToSelection, false );

executeAction( idslct, desc119, DialogModes.NO );

Moving selected slice :

// =======================================================

var idmove = charIDToTypeID( "move" );

    var desc129 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref38 = new ActionReference();

        var idslice = stringIDToTypeID( "slice" );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref38.putEnumerated( idslice, idOrdn, idTrgt );

    desc129.putReference( idnull, ref38 );

    var idT = charIDToTypeID( "T   " );

        var desc130 = new ActionDescriptor();

        var idHrzn = charIDToTypeID( "Hrzn" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc130.putUnitDouble( idHrzn, idPxl, -1.000000 );

        var idVrtc = charIDToTypeID( "Vrtc" );

        var idPxl = charIDToTypeID( "#Pxl" );

        desc130.putUnitDouble( idVrtc, idPxl, 0.000000 );

    var idOfst = charIDToTypeID( "Ofst" );

    desc129.putObject( idT, idOfst, desc130 );

executeAction( idmove, desc129, DialogModes.NO );

Delete selected slice :

// =======================================================

var idDlt = charIDToTypeID( "Dlt " );

    var desc135 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref41 = new ActionReference();

        var idslice = stringIDToTypeID( "slice" );

        var idOrdn = charIDToTypeID( "Ordn" );

        var idTrgt = charIDToTypeID( "Trgt" );

        ref41.putEnumerated( idslice, idOrdn, idTrgt );

    desc135.putReference( idnull, ref41 );

executeAction( idDlt, desc135, DialogModes.NO );

Unfortunately this is not much help when it comes to find a slice programmatically.

Akash Sharma
Legend
June 15, 2016

Moving to Photoshop Scripting