forked from graysitory/Novelty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Text_2_Layer.jsx
executable file
·120 lines (100 loc) · 2.99 KB
/
Text_2_Layer.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Text_2_Layer.jsx
Version 2.0
Indesign CS5 javascript
Bruno Herfst 2011
Move all text to new layer (except for locked layers)
Finds stories and moves all it’s textframes to a new layer.
This makes sure all text will be done even TextPaths.
12.12.13: Added layer support
*/
#target "InDesign"
var myDoc, selectedLayer = undefined;
function go() {
//Make certain that user interaction (display of dialogs, etc.) is turned on.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
if (app.documents.length != 0) {
//get ref to doc
myDoc = app.activeDocument;
myDisplayDialog();
} else {
alert("Please open a document and try again.");
}
}
//start dialog
function myDisplayDialog() {
// Create lists
var list_of_layers = myDoc.layers.everyItem().name;
list_of_layers.push("[New Layer]");
var myDialog = app.dialogs.add({
name: "Move all text to layer"
});
with(myDialog.dialogColumns.add()) {
var mySelectedLayer = dropdowns.add({
stringList: list_of_layers
});
mySelectedLayer.selectedIndex = 0;
}
var myResult = myDialog.show();
if (myResult == true) {
if (mySelectedLayer.selectedIndex == list_of_layers.length - 1) {
selectedLayer = createLayer();
} else {
selectedLayer = myDoc.layers[mySelectedLayer.selectedIndex];
}
myDialog.destroy();
//do story
if (selectedLayer != undefined) {
doAllStories();
}
} else {
myDialog.destroy();
exit();
}
}
//end dialog
function doAllStories() {
//send every story to a new layer
for (var i = 0; i < myDoc.stories.length; i++) {
myStory = myDoc.stories.item(i);
moveStoryToLayer(myStory, selectedLayer);
}
alert("All found text to layer " + selectedLayer.name);
}
function moveStoryToLayer(myStory, selectedLayer) {
var myTextFrame;
for (var myCounter = myStory.textContainers.length - 1; myCounter >= 0; myCounter--) {
myTextFrame = myStory.textContainers[myCounter];
switch (myTextFrame.constructor.name) {
case "TextFrame":
//I use a try statement here so it will continue moving text
//even if it finds text on a locked layer.
try {
myTextFrame.move(selectedLayer);
} catch (myError) {}
break;
default:
try {
myTextFrame.parent.move(selectedLayer);
} catch (myError) {}
}
}
}
function createLayer() {
var layerName = prompt("New layer name:");
if (layerName != null) {
var layer = myDoc.layers.item(layerName);
if (layer.isValid) {
alert("Layer already exist!");
createLayer();
} else {
layer = myDoc.layers.add({
name: layerName
});
return layer;
}
} else {
return undefined;
}
}
go();