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 again Paul,

Thanks so much for taking the time to look at this. As you said, this does pretty much exactly what I need for this. I have just removed the UI stuff, renamed some variables while trying to make sure I understood the logic, and put the results into array pairs for use in another script.


I needed to make a couple of amendments, mainly to get the script to skip over layer groups entirely (including the start and end of the groups) and it really highlighted my ignorance about the way in which you are reading and converting the binary data. I also found a PSD spec document (here) with the hope that I could work out how to access the relevant data to filter out specific layer types, but again my lack of knowledge on how the binary data is accessed and converted into readable strings really hindered me. It also seems that because the data is not a string as such, that my usual method of looking through the debugger to scan the data that needs parsing is not very helpful.


Can I ask your methodology when finding how to access the specific items, and if you had the time, maybe the bare-bones how the four read functions interact?


I have posted the slightly amended code below (though the functionality is essentially the same as yours).


No worries if you don't have the time to get into the details of this, I totally understand. Thanks again, you've already been a massive help in getting this working. Hopefully I can get to a point where I understand things a little deeper and don't have to bother kind people like yourself. :)


Kind regards,

- Ian



[CODE=javascript]

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) {

    return str.charCodeAt(ofs);

  }

  function readInt16(str, ofs) {

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

  }

  function readWord(str, ofs) {

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

  }

  function readUnicodeChar(str, ofs) {

    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

    }

   

    if(!str.match(/<\/Layer group/)){ // end of a layer group

    }


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


  }


name_arr.reverse();

ID_arr.reverse();


var PSDlayersArr = [];

var thisArr = [];

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

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

    PSDlayersArr.push(thisArr);

}


  alert(PSDlayersArr);

}

 

main();[/CODE]


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