-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReplaceWithSymbol_v2.jsx
147 lines (110 loc) · 4.63 KB
/
ReplaceWithSymbol_v2.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/******************************
* @title ReplaceWithSymbol
* @author Jonatan Hilden
* @info http://koponen-hilden.fi
* @version 2.0.0
DESCRIPTION
Script for replacing graphic objects with symbols. Select a number of objects and a symbol instance before running the script. Gives a choice of using the size of the symbol (default) or scaling the symbols to match the size of replaced objects.
Groups are treated as single objects. Potentially slow for many objects.
*******************************/
var scriptID = "Replace with symbol ";
var numSelectedItems = 0;
var docRef = app.activeDocument;
var selectedSymbol = null;
// find a symbol in the selection (first symbol found is used)
selectedSymbol = getSymbolInSelection(docRef, selectedSymbol);
//$.write(selectedSymbol);
// script will not work unless selection includes a symbol
var symbolName = 'NONE SELECTED – script will fail!';
if (selectedSymbol){
symbolName = selectedSymbol.name;
}
// Make a simple interface box
var box = new Window('dialog', scriptID);
box.alignChildren='left';
// headline
box.add('statictext', undefined, "Replace selected objects with symbol instance");
// stats panel: display selected object count and symbol
box.statsPanel = box.add('panel', undefined, "Info");
box.statsPanel.alignChildren='left';
box.statsPanel.group0 = box.statsPanel.add('group', undefined );
box.statsPanel.add('statictext', undefined, "Symbol name: " + symbolName);
box.statsPanel.add('statictext', undefined, "Selected object count: " + numSelectedItems);
// options panel
box.optionsPanel = box.add('panel', undefined, "Options");
box.optionsPanel.alignChildren='left';
// scale checkbox, default: false
var scaleCheck = box.optionsPanel.add('checkbox', undefined, "Scale symbols to match original objects?");
scaleCheck.value=false;
// clear originals checkbox; default true
var clearCheck = box.optionsPanel.add('checkbox', undefined, "Clear original objects?");
clearCheck.value=true;
// ok and cancel buttons group
box.buttons = box.add('group', undefined );
box.buttons.orientation='row';
box.buttons.add('statictext', undefined, "Run script");
var okButton = box.buttons.add('button',undefined, "OK", {name:'ok'});
var closeButton = box.buttons.add('button',undefined, "Cancel", {name:'close'});
// Ok button function
okButton.onClick = function(){
var scaleSymbol = scaleCheck.value;
var clearOriginals = clearCheck.value;
if (selectedSymbol == null)
{alert(scriptID + "\n" + "please select a symbol", scriptID, true);
box.close();
} else {
replaceWithSymbol(docRef, selectedSymbol, clearOriginals, scaleSymbol);
box.close();
}
// box.ok();
}
closeButton.onClick = function(){
box.close();
}
box.show()
function replaceWithSymbol(docRef, selectedSymbol, clearOriginals, scaleSymbol){
// loop through remaining selection and replace them with symbols on active layer
// clear originals if desired
for(i=0;i<docRef.selection.length;i++){
var currObj=docRef.selection[i];
// $.write(currObj.layer);
var currLeft=currObj.left;
var currTop=currObj.top;
var currWidth=currObj.width;
var currHeight=currObj.height;
var currInstance=docRef.activeLayer.symbolItems.add(selectedSymbol);
if (scaleSymbol){
currInstance.width*=currHeight/currInstance.height;
currInstance.height=currHeight;
currInstance.left=currLeft;
currInstance.top=currTop;
} else {
currInstance.position = Array( currObj.position[0]+currWidth/2 - currInstance.width/2,
currObj.position[1]-currHeight/2 + currInstance.height/2 );
}
currObj.selected = false;
currInstance.selected = true;
if (clearOriginals) {currObj.remove();}
}
redraw();
if (numSelectedItems > 0) {
// alert(scriptID + "\n" + numSelectedItems + " " + ((numSelectedItems == 1)?"object":"objects") + " in total", scriptID, true);
} else {
alert(scriptID + "\n" + "please select objects", scriptID, true);
return;
}
}
function getSymbolInSelection(docRef, selectedSymbol) {
// loop through selected items to get the desired symbol and deselect this item
var symbolLayer = null;
for(i=0;i<docRef.selection.length;i++){
if(docRef.selection[i].symbol) {
symbolLayer = docRef.selection[i].layer;
selectedSymbol = docRef.selection[i].symbol;
docRef.selection[i].selected = false;
numSelectedItems -= 1;
}
numSelectedItems += 1;
}
return selectedSymbol;
}