ianPark
1/3/2020 - 6:17 PM

Get Photoshop Layer Structure

tell application "Adobe Photoshop 2020"
	tell current document
		set artLayers to do javascript "artStructure = [];var doc = app.activeDocument;var art = [];var set = [];get(doc, art, set);function get (doc, art, set){for (var x = 0; x < (doc.layers.length + 1); x++) {if (x == doc.layers.length) {set.pop();} else {var lay = doc.layers[x];if (lay.typename === 'ArtLayer') {var fold = set.reverse().toString();set.reverse();if (fold == '') {artStructure.push([[lay.visible, lay.name], 'X_X', [doc.name], 'X_X'])} else {artStructure.push([[lay.visible, lay.name], 'X_X', [fold], 'X_X']);}} else {set.push([[lay.visible, lay.name], 'Y_Y']);get(lay, art, set);}}}}layerList = artStructure;"
	end tell
end tell

set artLayers_group to {}
-- SPLIT INTO: LAYER | STRUCTURE LIST
set AppleScript's text item delimiters to ",X_X,"
set artLayers_split to text items of artLayers
set AppleScript's text item delimiters to ""
-- SPLIT INTO: LAYER {VISIBLE|NAME} | STRUCTURE {{VISIBLE|NAME}|{VISIBLE|NAME}...}
repeat with x from 1 to count of artLayers_split by 2
	set split_2 to item (x + 1) of artLayers_split
	set AppleScript's text item delimiters to ",Y_Y,"
	set split_2 to text items of split_2
	set AppleScript's text item delimiters to ","
	set split_1 to text items of item x of artLayers_split
	set split_1_list to {item 1 of split_1, item 2 of split_1}
	set split_2_list to {}
	repeat with y from 1 to count of split_2
		try
			set split_y to text items of item y of split_2
			copy {item 1 of split_y, item 2 of split_y} to end of split_2_list
		end try
	end repeat
	copy {split_1_list, split_2_list} to end of artLayers_group
end repeat