-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_layer_names.jsx
103 lines (85 loc) · 2.19 KB
/
export_layer_names.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
app.bringToFront();
var docRef = app.activeDocument;
var docName = decodeURI(app.activeDocument.name);
docName = docName.slice(0, -4); // Remove the last 4 characters (extension)
// Define pixels as unit of measurement
//var defaultRulerUnits = preferences.rulerUnits;
//preferences.rulerUnits = Units.PIXELS;
var layerNum = app.activeDocument.artLayers.length;
var layerRef = app.activeDocument.activeLayer;
var layerNames = "";
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
// Loop to iterate through all layers
function recurseLayers(currLayers) {
for ( var i = currLayers.layers.length - 1; i >= 0; i-- ) {
layerRef = currLayers.layers[i];
// test if layer is visible
if (layerRef.visible) {
var name = layerRef.name.replace(/_/g, " ");
name = name.charAt(0).toUpperCase() + name.slice(1);
layerNames += name + "\n";
}
// test if it's a layer set
if (isLayerSet(currLayers.layers[i])) {
if (currLayers.layers[i].artLayers.length > 0) {
recurseLayers(currLayers.layers[i]);
}
}
}
}
// a test for a layer set
function isLayerSet(layer) {
try {
if (layer.layers.length > 0) {
return true;
}
} catch (err) {
return false;
}
}
// Ask the user for the folder to export to
var FPath = Folder.selectDialog("Save exported layer names to");
// Detect line feed type
if ($.os.search(/windows/i) !== -1) {
fileLineFeed = "Windows";
} else {
fileLineFeed = "Macintosh";
}
// Export to txt files
function writeFiles(info, fileName) {
try {
var f = new File(FPath + "/" + docName + "_names.txt");
f.remove();
f.open("a");
f.lineFeed = fileLineFeed;
f.write(info);
f.close();
} catch (e) {}
}
// Run the functions
recurseLayers(docRef);
//preferences.rulerUnits = defaultRulerUnits;
// Set preferences back to user's defaults
writeFiles(layerNames, docName);
// Show results
if (FPath == null) {
alert("Export aborted", "Canceled");
} else {
alert(
"Exported " +
layerNum +
" layer names to " +
FPath +
"/" +
docName +
"_names.txt " +
"using " +
fileLineFeed +
" line feeds.",
"Success!"
);
}