forked from Paul-Riggott/PS-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuideSets.jsx
164 lines (164 loc) · 5.84 KB
/
GuideSets.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#target photoshop
app.bringToFront();
///////////////////////////////////
// Photoshop CS5 or better
///////////////////////////////////
function main(){
if(Number(app.version.match(/^\d+/)) <12){
alert("Sorry but you need to have Photoshop CS5 or better");
return;
}
if(!documents.length) return;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var psNamespace = "http://ns.multiguidesets/1.0/";
var psPrefix = "mgsets:";
XMPMeta.registerNamespace(psNamespace, psPrefix);
existingGuidePresets = [];
existingGuidePresets = getGuideArray();
var guides = app.activeDocument.guides;
var gH = '';
var gV = '';
for( var g = 0; g < guides.length; g++ ){
if(guides[g].direction.toString() == 'Direction.HORIZONTAL'){
gH+=(parseInt(guides[g].coordinate.value));
gH+=',';
}else{
gV+=(parseInt(guides[g].coordinate.value));
gV+=','
}
}
gH=gH.replace(/,$/,'');
gV=gV.replace(/,$/,'');
app.preferences.rulerUnits = startRulerUnits;
var win = new Window( 'dialog', 'Custom Guides' );
g = win.graphics;
var myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.99, 0.99, 0.99, 1]);
g.backgroundColor = myBrush;
win.p1= win.add("panel", undefined, undefined, {borderStyle:"black"});
win.g1 = win.p1.add('group');
win.g1.orientation = "row";
win.title = win.g1.add('statictext',undefined,'Custom Guides');
win.title.alignment="fill";
var g = win.title.graphics;
g.font = ScriptUI.newFont("Georgia","BOLDITALIC",22);
win.g5 =win.p1.add('group');
win.g5.orientation = "row";
win.g5.alignment='fill';
win.g5.spacing=10;
win.g5.st1 = win.g5.add('statictext',undefined,'Existing Presets');
win.g5.dd1 = win.g5.add('dropdownlist');
win.g5.dd1.preferredSize=[300,20];
for(var w in existingGuidePresets){
win.g5.dd1.add('item',existingGuidePresets[w].toString().match(/[^¬]*/));
}
win.g5.dd1.selection=0;
win.g10 =win.p1.add('group');
win.g10.orientation = "row";
win.g10.alignment='fill';
win.g10.spacing=10;
win.g10.bu1 = win.g10.add('button',undefined,'Remove Preset');
win.g10.bu2 = win.g10.add('button',undefined,'Use and Append Guides');
win.g10.bu3 = win.g10.add('button',undefined,'Use and Remove Guides');
win.g15 =win.p1.add('group');
win.g15.orientation = "row";
win.g15.alignment='fill';
win.g15.st1 = win.g15.add('statictext',undefined,'New Preset Name');
win.g20 =win.p1.add('group');
win.g20.orientation = "row";
win.g20.alignment='fill';
win.g20.spacing=10;
win.g20.et1 = win.g20.add('edittext');
win.g20.et1.preferredSize=[300,20];
win.g20.bu1 = win.g20.add('button',undefined,'Add New Preset');
win.g200 =win.p1.add('group');
win.g200.orientation = "row";
win.g200.bu1 = win.g200.add('button',undefined,'Cancel');
win.g200.bu1.preferredSize=[350,30];
win.g10.bu1.onClick=function(){
if(existingGuidePresets.length == 0) return;
existingGuidePresets.splice(win.g5.dd1.selection.index,1);
win.g5.dd1.removeAll();
for(var w in existingGuidePresets){
win.g5.dd1.add('item',existingGuidePresets[w].toString().match(/[^¬]*/));
}
win.g5.dd1.selection=0;
putGuideArray(existingGuidePresets);
}
win.g10.bu2.onClick=function(){
if(existingGuidePresets.length == 0) return;
win.close(0);
var ar1 = existingGuidePresets[win.g5.dd1.selection.index].toString().split('¬');
var Hor = ar1[1].toString().split(',');
var Ver = ar1[2].toString().split(',');
for(var H in Hor){
activeDocument.guides.add(Direction.HORIZONTAL,new UnitValue(Number(Hor[H]),'px'));
}
for(var V in Ver){
activeDocument.guides.add(Direction.VERTICAL,new UnitValue(Number(Ver[V]),'px'));
}
}
win.g10.bu3.onClick=function(){
if(existingGuidePresets.length == 0) return;
win.close(0);
clearGuides();
var ar1 = existingGuidePresets[win.g5.dd1.selection.index].toString().split('¬');
var Hor = ar1[1].toString().split(',');
var Ver = ar1[2].toString().split(',');
for(var H in Hor){
activeDocument.guides.add(Direction.HORIZONTAL,new UnitValue(Number(Hor[H]),'px'));
}
for(var V in Ver){
activeDocument.guides.add(Direction.VERTICAL,new UnitValue(Number(Ver[V]),'px'));
}
}
win.g20.bu1.onClick=function(){
if(app.activeDocument.guides.length == 0){
alert("No guides exist");
return;
}
if(win.g20.et1.text == ''){
alert("You need to enter a name for the new preset");
return;
}
win.close(0);
currentGuides = win.g20.et1.text + "¬" + gH + "¬" + gV;
existingGuidePresets.push(currentGuides);
putGuideArray(existingGuidePresets);
}
win.center();
win.show();
function getGuideArray(){
var xmp;
var newGuideArray=[];
xmp = new XMPMeta( app.activeDocument.xmpMetadata.rawData );
var Count = xmp.countArrayItems(psNamespace, "GuideArray");
for(var i = 1;i <= Count;i++){
newGuideArray.push(xmp.getArrayItem(psNamespace, "GuideArray", i).toString());
}
return newGuideArray;
}
function putGuideArray(gArray){
var xmp;
xmp = new XMPMeta( app.activeDocument.xmpMetadata.rawData );
xmp.deleteProperty(psNamespace, "GuideArray");
for(var g in gArray){
xmp.appendArrayItem(psNamespace, "GuideArray", gArray[g].toString(), 0, XMPConst.ARRAY_IS_UNORDERED);
}
app.activeDocument.xmpMetadata.rawData = xmp.serialize();
}
function clearGuides() {
var id556 = charIDToTypeID( "Dlt " );
var desc102 = new ActionDescriptor();
var id557 = charIDToTypeID( "null" );
var ref70 = new ActionReference();
var id558 = charIDToTypeID( "Gd " );
var id559 = charIDToTypeID( "Ordn" );
var id560 = charIDToTypeID( "Al " );
ref70.putEnumerated( id558, id559, id560 );
desc102.putReference( id557, ref70 );
executeAction( id556, desc102, DialogModes.NO );
};
}
main();