What's new
Photoshop Gurus Forum

Welcome to Photoshop Gurus forum. Register a free account today to become a member! It's completely free. Once signed in, you'll enjoy an ad-free experience and be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Reply to thread

Hi Ian, here the code is slightly changed...

[code]

function main() {


  file = File.openDialog("Please select PSD file.","PSD File:*.psd");

  if(!file.exists) return;

  file.open("r");

  file.encoding = 'BINARY';

  var PSD_data = file.read();

  file.close();

 

  var findResult;

  var indexPos =[];

  var name_arr= [];

  var ID_arr = [];


  var rex_name = new RegExp ('8BIMluni','g');


  while ((findResult = rex_name.exec(PSD_data)) != null) { // Find ALL occurencies of search string

    indexPos.push(findResult.index+(findResult[0].length));

  }


  var rex_id = new RegExp ('8BIMlyid','g');


  while ((findResult = rex_id.exec(PSD_data)) != null) { // Find ALL occurencies of search string

    ID_arr.push( readWord(PSD_data, findResult.index + 12));

  }


  function readByte(str, ofs) { // Read one byte at offset

    return str.charCodeAt(ofs);

  }

  function readInt16(str, ofs) { //read two byttes at offset

    return (readByte(str, ofs) << 8) + readByte(str, ofs+1);

  }

  function readWord(str, ofs) { //read four bytes at offset

    return (readInt16(str, ofs) << 16) + readInt16(str, ofs+2);

  }

  function readUnicodeChar(str, ofs) { //get character at offset

    return String.fromCharCode(readInt16(str,  ofs));

  }

 

  for (var i = 0; i < indexPos.length; i++) { //loop through all finds

    var ofs = indexPos[i]; //offset of find

    var name_arrLength = readWord(PSD_data, (ofs+ 4));  //Read length of string at offset

    ofs += 8; //increment offset to suit

    var str = ''; //reset string to ''

 

    for (var j = 0; j < name_arrLength; j++) { //loop through string

      str += readUnicodeChar(PSD_data, ofs); // add char

      ofs += 2; //increment two bytes

    }


    name_arr.push(str); //Layer name found and stored


  }

//reverse arrays so that ir reads Photoshop layers top down

name_arr.reverse();

ID_arr.reverse();


var PSDlayersArr = [];

var thisArr = [];

for (var i=0,len=name_arr.length;i<len;i++) {

    //remove layerset end

    if(name_arr[i].match(/<\/Layer group/)) continue;

    //If layer groups have group in thier name you could remove them here....

   // if(name_arr[i].match(/group/i)) continue;

    thisArr = [name_arr[i],ID_arr[i]];

    PSDlayersArr.push(thisArr);

}


  alert(PSDlayersArr.join("\n"));

}

 

main();

[/code]


What I normally do is look at the spec and use an hex editor to look at the file, then you can see how it is formatted.

Most of the field start with "8BIM" so it's easy to spot them. The RegExp ('8BIMxxxx','g'); will find all the positions in the file and give you a place to look at the file.

The position found by the RegExp gives the start position of the "8BIMxxxx" so you need to add 8 bytes to get to the data for that field.

If your layerset names start with Group you could remove them as above, I have amended the code so it removes the layerset ends.


Hope it's start for you.

Good luck with your project!


What is our favorite program/app? (Hint - it begins and ends with the letter P)
Back
Top