开发者

Get parameters of slices in photoshop

I have some slices in my photoshop document.

How I can get their parameters (width, height, x-offset, y-offset, name, url, 开发者_如何学JAVA...) via javascript > scripting in photoshop?


@SublimeYe, I too have the same quest, and to date, there has been no solution available. The Adobe Scripting in javascript has no way to see into the application model to gain access to slices. So, after some of my own research, the best solution I have found was presented to me by Trevor Morris (http://morris-photographics.com/photoshop/scripts/index.html). We paid Trevor some $500+ to write a script which uses a Layer Group of "Slices" and for each Layer Comp there is a corresponding Shape Layer. The javascript can see Groups, Layers, and shape layers so the script can crop and save for web nicely. The downside is that you cannot use the slicing tool, so adopting of this script has been minimal from our design team.

After still having this problem for years, I recently tried to search again found this adobe forum http://feedback.photoshop.com/photoshop_family/topics/slice_compositions_that_work_like_layer_compositions and a link to this SliceMaster tool by Enzo http://www.electriciris.com/slicemaster.html. This tool allows for multiple slices per document that are stored as slice sets in layers. My quest is now to automate Layer Comps with Layer Slice sets, which I think is very similar as yours. I have a question out to Enzo to see if he can write or plans to write this automation.

I think the only way you will get what you want to do is by using an Adobe Photoshop SDK Plugin that is written in C++ code, not a Script in javascript. I just downloaded the SDK and I'm peeking at C++ codes to see if a solution can be made.

However, I'm hoping Enzo can make the solution since he is already a Photoshop SDK developer... stay tuned!


After my research for times. I have found an easy way to get the slice info by script.

As you find out, there is no direct way to access a slice as Selection.

But when you slice an area in ps, it dispatches an event!

So, you can add an event listener to that event, an in the event handler, you can get the ActionDescriptor, and when you get the ActionDescriptor, you get everything!

If you have install the ScriptListener, you can find out the ActionDescriptor as below:

var idMk = charIDToTypeID( "Mk  " );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
    var ref1 = new ActionReference();
    var idslice = stringIDToTypeID( "slice" );
    ref1.putClass( idslice );
desc2.putReference( idnull, ref1 );
var idUsng = charIDToTypeID( "Usng" );
    var desc3 = new ActionDescriptor();
    var idType = charIDToTypeID( "Type" );
    var idsliceType = stringIDToTypeID( "sliceType" );
    var iduser = stringIDToTypeID( "user" );
    desc3.putEnumerated( idType, idsliceType, iduser );
    var idAt = charIDToTypeID( "At  " );
        var desc4 = new ActionDescriptor();
        var idTop = charIDToTypeID( "Top " );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc4.putUnitDouble( idTop, idPxl, 83.000000 );
        var idLeft = charIDToTypeID( "Left" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc4.putUnitDouble( idLeft, idPxl, 229.000000 );
        var idBtom = charIDToTypeID( "Btom" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc4.putUnitDouble( idBtom, idPxl, 144.000000 );
        var idRght = charIDToTypeID( "Rght" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc4.putUnitDouble( idRght, idPxl, 327.000000 );
    var idRctn = charIDToTypeID( "Rctn" );
    desc3.putObject( idAt, idRctn, desc4 );
var idslice = stringIDToTypeID( "slice" );
desc2.putObject( idUsng, idslice, desc3 );
executeAction( idMk, desc2, DialogModes.NO );

So, if we could get this ActionDescriptor, we could get the Rectangle infomation!

Now we start with adding an event Listener (here I use ExtendScript, also available in javascript)

private const MK_INT:int = Photoshop.app.charIDToTypeID("Mk  ");    // event name
CSXSInterface.instance.evalScript("PhotoshopRegisterEvent", MK_INT);  // add event listener
ExternalInterface.addCallback("PhotoshopCallback" + CSXSInterface.getInstance().getExtensionId(), myPhotoshopCallback);   // event handler

then we do in the event handler:

private function myPhotoshopCallback(eventID:Number, descID:Number):void {
var desc2:ActionDescriptor = new ActionDescriptor();
    desc2.fromID(descID);
    // there ! we got the ActionDescriptor!
    // now we need to filter the event
    if (eventID == MK_INT) {
         var idUsng:Number = Photoshop.app.charIDToTypeID( "Usng" );
     var classID:Number = desc2.getObjectType(idUsng);
     if (classID == Photoshop.app.stringIDToTypeID("slice")) {
    var desc3:ActionDescriptor = desc2.getObjectValue(idUsng);
    var idAt:Number = Photoshop.app.charIDToTypeID("At  ");
    var idRctn:Number = Photoshop.app.charIDToTypeID( "Rctn" );
    if (idRctn == desc3.getObjectType(idAt)) {
        var desc4:ActionDescriptor = desc3.getObjectValue(idAt);
        var idTop:Number = Photoshop.app.charIDToTypeID( "Top " );
        var idLeft:Number = Photoshop.app.charIDToTypeID( "Left" );
        var idBtom:Number = Photoshop.app.charIDToTypeID( "Btom" );
        var idRght:Number = Photoshop.app.charIDToTypeID( "Rght" );
        trace("top: " + desc4.getUnitDoubleValue(idTop));
        trace("left: " + desc4.getUnitDoubleValue(idLeft));
        trace("bottom: " + desc4.getUnitDoubleValue(idBtom));
        trace("right: " + desc4.getUnitDoubleValue(idRght));
    }

    }
    }
}

As you can see, we get the ActionDescriptor, we could get any info the that Descriptor! I tried and worked!


Use the Slice Select Tool under the Crop and Slice toolset. You can either manually select this by choosing it in your toolbar or, when hovering over a slice you've created, it will automatically appear for you.

It is also activated by holding down CMD (Ctrl in Windows) when hovering over a previously created slice.

Then double click the slice and a dialog window will open giving you all necessary info for that slice.

Happy slicing!


I have too been on this quest for years. I have finally created a solution that is near perfect. The only drawback is that it requires opening your PSD in Fireworks. Kinda lame, I know. Would be much better to just it save them from photoshop but I don't believe that it's possible. Take is code and save it as a .jsf file. Then open your PSD with slices in fireworks. Go to Commands > Run Script... and select your .jsf file. It should save all your slice info to a text file in the folder you select. Hope this helps someone.

var curDoc = fw.getDocumentDOM();
var slices = curDoc.layers[curDoc.layers.length-1].elems;
var result = "";
for (curSliceNum = slices.length-1; curSliceNum >= 0; curSliceNum--) {
        var slice = slices[curSliceNum]
        result += slice.baseName + "\r\n";
        result += "W: "+slice.width+", H: "+slice.height + "\r\n";
        result += "X: "+slice.pixelRect.left+", Y: "+slice.pixelRect.top + "\r\n";
        result += "---------------------------\r\n"; 
 }

// Saves text to the specified file
function saveText(fileURL, text){

    // Delete any existing file with the same name. If
    // you do not, and then open the file for rewriting,
    // saved text will be written over the existing file
    // which could leave remnants of the old file behind
    if (Files.deleteFileIfExisting(fileURL)){

        // Create a new file to write in. Note:
        // this is only required for Macs; Windows
        // will create a file with the call to open()
        if (Files.createFile(fileURL, ".txt", "TEXT")){

            // Open the file for writing. If successful, this
            // will return a reference to the file so that
            // text can be added to it using the write()
            // command.  
            var fileReference = Files.open(fileURL, true);
            if (fileReference){

                // Write the text to the opened file
                fileReference.write(text);

                // When finished, be sure to close the
                // file using the close() command so other
                // processes will be able to access it
                fileReference.close();

                // Returning true signals a successful save
                return true;
            }
        }
    }

    // Returning false signals a failed save
    return false;
}

var fileForSave = fw.browseForFolderURL();
saveText(fileForSave+"/coords.txt", result);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜