I am not tested this yet but this code get 'index' property from layers. Index is this same as unique id? Layer index isn't a position on layer list? So when I move layer - index will be changed... Please correct me if I'm wrong...
Unique ID is always this same for each layer - I can change name, position on artboard, position in layer list and I still will have this same UID.
Here is how to get the layer's ID. But as Paul said the ID is only unique to the document. And the background layer always returns 0 and it's id is not unique.
function getActiveLayerId() {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "LyrI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
return executeActionGet(ref).getInteger( stringIDToTypeID( "layerID" ) );
}
Note I used both the stringID and charID for the layerID key. You can find some information about charID and stringID in the Photoshop SDK. You can find the most up to date list I know of in Xbytor's xtools.