This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
us-export-to-ios.jsx
150 lines (123 loc) · 4.17 KB
/
us-export-to-ios.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
/*!
* iOS Assets for Photoshop
* =============================
*
* Version: 1.0.0
* Author: Gaston Figueroa (Uncorked Studios)
* Site: uncorkedstudios.com
* Licensed under the MIT license
*/
// Photoshop variables
var docRef = app.activeDocument,
activeLayer = docRef.activeLayer,
activeLayer2,
newWidth,
newHeight,
docName = docRef.name;
var scaleFactors = {
'@3x': 1,
'@2x': 1.5,
'@1x': 3,
};
// Run main function
init();
// The other functions
function init() {
if(!isDocumentNew()) {
for(var dpi in scaleFactors) {
saveFunc(dpi);
}
} else {
alert("Please save your document before running this script.");
}
}
// Test if the document is new (unsaved)
// http://2.adobe-photoshop-scripting.overzone.net/determine-if-file-has-never-been-saved-in-javascript-t264.html
function isDocumentNew(doc){
// assumes doc is the activeDocument
cTID = function(s) { return app.charIDToTypeID(s); }
var ref = new ActionReference();
ref.putEnumerated( cTID("Dcmn"),
cTID("Ordn"),
cTID("Trgt") ); //activeDoc
var desc = executeActionGet(ref);
var rc = true;
if (desc.hasKey(cTID("FilR"))) { //FileReference
var path = desc.getPath(cTID("FilR"));
if (path) {
rc = (path.absoluteURI.length == 0);
}
}
return rc;
};
function resizeDoc(document, scale) {
var calcWidth = activeLayer.bounds[2] - activeLayer.bounds[0], // Get layer's width
calcHeight = activeLayer.bounds[3] - activeLayer.bounds[1]; // Get layer's height
// Resize assets
newHeight = Math.floor(calcHeight / scaleFactors[scale]);
newWidth = Math.floor(calcWidth / scaleFactors[scale]);
// Resize temp document using Bicubic interpolation
resizeLayer(newWidth);
// Merge all layers inside the temp document
activeLayer2.merge();
}
// document.resizeImage doesn't seem to support scalestyles so we're using this workaround from http://ps-scripts.com/bb/viewtopic.php?p=14359
function resizeLayer(newWidth) {
var idImgS = charIDToTypeID( "ImgS" );
var desc2 = new ActionDescriptor();
var idWdth = charIDToTypeID( "Wdth" );
var idPxl = charIDToTypeID( "#Pxl" );
desc2.putUnitDouble( idWdth, idPxl, newWidth);
var idscaleStyles = stringIDToTypeID( "scaleStyles" );
desc2.putBoolean( idscaleStyles, true );
var idCnsP = charIDToTypeID( "CnsP" );
desc2.putBoolean( idCnsP, true );
var idIntr = charIDToTypeID( "Intr" );
var idIntp = charIDToTypeID( "Intp" );
var idBcbc = charIDToTypeID( "Bcbc" );
desc2.putEnumerated( idIntr, idIntp, idBcbc );
executeAction( idImgS, desc2, DialogModes.NO );
}
function dupToNewFile() {
// TO DO: Target all files ending with '#'
var fileName = activeLayer.name.replace(/\.[^\.]+$/, ''),
calcWidth = Math.ceil(activeLayer.bounds[2] - activeLayer.bounds[0]),
calcHeight = Math.ceil(activeLayer.bounds[3] - activeLayer.bounds[1]),
docResolution = docRef.resolution,
document = app.documents.add(calcWidth, calcHeight, docResolution, fileName, NewDocumentMode.RGB,
DocumentFill.TRANSPARENT);
app.activeDocument = docRef;
// Duplicated selection to a temp document
activeLayer.duplicate(document, ElementPlacement.INSIDE);
// Set focus on temp document
app.activeDocument = document;
// Assign a variable to the layer we pasted inside the temp document
activeLayer2 = document.activeLayer;
// Center the layer
activeLayer2.translate(-activeLayer2.bounds[0],-activeLayer2.bounds[1]);
}
function saveFunc(dpi) {
dupToNewFile();
var docRef2 = app.activeDocument;
resizeDoc(docRef2, dpi);
var Name = docRef2.name.replace(/\.[^\.]+$/, ''),
Ext = decodeURI(docRef2.name).replace(/^.*\./,''),
Path = docRef.path,
folder = Folder(Path + '/' + docName + '-assets/');
if(!folder.exists) {
folder.create();
}
// Name the new asset
var saveFile = File(folder + "/" + Name + (dpi === '@1x' ? '' : dpi) + ".png");
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.PNG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = 100;
sfwOptions.PNG8 = false;
// Export the layer as a PNG
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
// Close the document without saving
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}