View difference between Paste ID: 3jZ0PjKk and 6MKzZg4r
SHOW: | | - or go back to the newest paste.
1
// script.name = scaleStrokes.jsx;
2
// script.description = scales selected objects strokes only;
3
// script.required = one or more selected objects
4
// script.parent = CarlosCanto // 5/7/12;
5
// script.elegant = false;
6
// script.updates = preview ON by default // 5/7/12
7
#target Illustrator
8
#targetengine 'main'
9
10
var unscale = 0;
11-
var idoc = activeDocument;
11+
12-
var sel = idoc.selection;
12+
var win = new Window("palette", "Scale Strokes");
13
var pnlScale = win.add("panel");
14-
if (sel.length>0) {
14+
pnlScale.orientation = "row";
15-
	var win = new Window("dialog", "Scale Strokes");
15+
var lblScale = pnlScale.add("statictext", undefined, "Scale");
16-
	var pnlScale = win.add("panel");
16+
var editScale = pnlScale.add("edittext", undefined, "100");
17-
	pnlScale.orientation = "row";
17+
editScale.characters = 5;
18-
	var lblScale = pnlScale.add("statictext",undefined,"Scale");
18+
var lblScale = pnlScale.add("statictext", undefined, "%");
19-
	var editScale = pnlScale.add("edittext",undefined,"100");
19+
20-
	editScale.characters = 5;
20+
var grpScroll = win.add("group");
21-
	var lblScale = pnlScale.add("statictext",undefined,"%");
21+
var scrlScale = grpScroll.add("scrollbar", undefined, 100, 1, 1000); // 1 min scaling to avoid division by zero
22
var btnReset = win.add("button", undefined, "Reset");
23-
	var grpScroll = win.add("group");
23+
btnReset.helpTip = "unscale or reset value to 100%"
24-
	var scrlScale = grpScroll.add("scrollbar", undefined,100, 1, 1000); // 1 min scaling to avoid division by zero
24+
win.helpTip = "\u00A9 2012 Carlos Canto";
25
26-
	var grpBottom = win.add("group");
26+
scrlScale.onChange = function() {
27-
	grpBottom.alignChildren = "top";
27+
	editScale.text = this.value; // update edit box with scroll value
28-
	var chkpreview = grpBottom.add("checkbox", undefined, "Preview");
28+
	editScale.notify("onChange") // call the onchange event handler
29-
	chkpreview.value = 1;
29+
30-
	var grpButtons = grpBottom.add("group");
30+
31-
	grpButtons.orientation = "column";
31+
editScale.onChange = function() {
32-
	var btnOk = grpButtons.add("button", undefined, "Ok");
32+
	if (this.text < 1) // if value entered by hand is less than 1, make it 1 to avoid division by zero
33-
	var btnCancel = grpButtons.add("button", undefined, "Cancel");
33+
	this.text = 1;
34-
	
34+
	scrlScale.value = this.text; // update edit box with scroll value
35-
	win.helpTip = grpBottom.helpTip = "\u00A9 2012 Carlos Canto"; 
35+
	btMsg(scaleStrokes.toString() + "\n scaleStrokes(activeDocument.selection," + editScale.text + ")");
36
}
37-
	scrlScale.onChanging = function() {
37+
38-
		editScale.text = this.value; // update edit box with scroll value
38+
// unscale (back to 100%) on reset, except no scaling has applied
39-
		scaleStrokes (sel,editScale.text); // and do the scaling as the scroll changes
39+
btnReset.onClick = function() {
40
	editScale.text = 100;
41
	editScale.notify("onChange")
42-
	editScale.onChange = function() {
42+
43-
		if (this.text<1) // if value entered by hand is less than 1, make it 1 to avoid division by zero
43+
44-
			this.text = 1;
44+
// Deal with selection changed, reset values onActivate
45-
		scrlScale.value = this.text; // update edit box with scroll value
45+
var sel, newsel;
46-
		scaleStrokes (sel,editScale.text); // and do the scaling after the edit box changes (manually entering values)
46+
47
win.onDeactivate = function() {
48
	btMsg('sel = activeDocument.selection')
49-
	chkpreview.onClick = function () {
49+
50-
		if (chkpreview.value)
50+
51-
			app.redraw();
51+
win.onActivate = function() {
52
	btMsg('newsel = activeDocument.selection')
53-
	
53+
	if (!isSelEqual(sel, newsel) && editScale.text != 100) {
54-
	// unscale (back to 100%) on cancel, except no scaling has applied
54+
		editScale.text = scrlScale.value = 100, unscale = 0;
55-
	btnCancel.onClick = function() {
55+
56-
		if (unscale != 0) {
56+
57-
			unscaleStrokes (sel, unscale);
57+
58-
		}
58+
win.center();
59-
		win.close();
59+
win.show();
60
61
function scaleStrokes(sel, scale) {
62-
	win.center();
62+
63-
	win.show();
63+
64
		unscaleStrokes(sel, unscale);
65-
else {
65+
66-
	alert("select at least one object before running");
66+
	for (i = 0; i < sel.length; i++) {
67
		var pgitem = sel[i];
68
		pgitem.resize(100, 100, undefined, undefined, undefined, undefined, scale, Transformation.CENTER);
69-
function scaleStrokes (sel,scale) {
69+
70
	unscale = 10000 / scale;
71
}
72-
		unscaleStrokes (sel, unscale);
72+
73
// unscale or bring back selection to 100%
74-
	for (i=0; i<sel.length; i++) {
74+
function unscaleStrokes(sel, unscale) {
75
	for (j = 0; j < sel.length; j++) {
76-
		pgitem.resize(100, 100, undefined, undefined, undefined, undefined, scale, Transformation.CENTER); 
76+
77
		pgitem.resize(100, 100, undefined, undefined, undefined, undefined, unscale, Transformation.CENTER);
78-
	if (chkpreview.value)
78+
79-
		app.redraw();
79+
80-
	unscale = 10000/scale;
80+
81
function BridgeTalkErrorHandler(a) {
82
	alert(a.body + "(" + a.headers["Error-Code"] + ")")
83
}
84-
function unscaleStrokes (sel,unscale) {
84+
85-
	for (j=0; j<sel.length; j++) {
85+
// use BridgeTalk to call function, this is needed because the use of "palette" panel
86
function btMsg(a) {
87-
		pgitem.resize(100, 100, undefined, undefined, undefined, undefined, unscale, Transformation.CENTER); 
87+
	var b = new BridgeTalk;
88
	b.target = "illustrator", b.body = a, b.onError = BridgeTalkErrorHandler, b.send()
89-
	if (chkpreview.value)
89+
90-
		app.redraw();
90+
91
// check whether selection changed or not
92
function isSelEqual(a, b) {
93
	if (!a || !b) return false
94
	if (a.length != b.length) return false
95
	for (var i = 0; i < a.length; i++) {
96
		if (a[i] != b[i]) return false
97
	}
98
	return true
99
}