-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresizeFileForWeb.jsx
139 lines (116 loc) · 5.54 KB
/
resizeFileForWeb.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
// Created by Nathaniel Young (nyoungstudios)
// this script resizes a photo with the optimal settings for web use including services like Squarespace or Wix
// saves resized files in folder that the user selects
//wrapper function
function main() {
// opens file dialog for user to select the file to process
var filePathName = File.openDialog("Pick a file");
// checks if the user does not select a file
if (filePathName == null) {
return;
} else {
filePathName = filePathName.toString();
}
// opens folder dialog for user to select the folder to process
var saveLocation = Folder.selectDialog("Pick a destination folder");
// checks if the user does not select a folder
if (saveLocation == null) {
return;
} else {
saveLocation = saveLocation.toString() + '/';
}
// parameters
var longEdgeOfDocument = 2048;
var quality = 10;
// removes the file extension
var baseDocName = filePathName.substring(filePathName.lastIndexOf('/') + 1, filePathName.lastIndexOf('.'));
// appends underscore (_) if the original document name ends in a number
var lastChar = baseDocName[baseDocName.length - 1];
if (lastChar >= '0' && lastChar <= '9') {
baseDocName += '_';
}
// =======================================================
// opens photo
var idOpn = charIDToTypeID( "Opn " );
var desc01 = new ActionDescriptor();
var iddontRecord = stringIDToTypeID( "dontRecord" );
desc01.putBoolean( iddontRecord, false );
var idforceNotify = stringIDToTypeID( "forceNotify" );
desc01.putBoolean( idforceNotify, true );
var idnull = charIDToTypeID( "null" );
desc01.putPath( idnull, new File( filePathName ) );
var idDocI = charIDToTypeID( "DocI" );
desc01.putInteger( idDocI, 286 );
executeAction( idOpn, desc01, DialogModes.NO );
// =======================================================
// gets the current active document open
currentDocument = app.activeDocument;
var originalWidthOfDocument = parseInt(currentDocument.width.toString().replace(' px', ''));
var originalHeightOfDocument = parseInt(currentDocument.height.toString().replace(' px', ''));
if (originalWidthOfDocument >= originalHeightOfDocument) {
// =======================================================
// resizes photo if it is in landsacpe orientation
var idImgS = charIDToTypeID( "ImgS" );
var desc02 = new ActionDescriptor();
var idWdth = charIDToTypeID( "Wdth" );
var idPxl = charIDToTypeID( "#Pxl" );
desc02.putUnitDouble( idWdth, idPxl, longEdgeOfDocument );
var idscaleStyles = stringIDToTypeID( "scaleStyles" );
desc02.putBoolean( idscaleStyles, true );
var idCnsP = charIDToTypeID( "CnsP" );
desc02.putBoolean( idCnsP, true );
var idIntr = charIDToTypeID( "Intr" );
var idIntp = charIDToTypeID( "Intp" );
var idautomaticInterpolation = stringIDToTypeID( "automaticInterpolation" );
desc02.putEnumerated( idIntr, idIntp, idautomaticInterpolation );
executeAction( idImgS, desc02, DialogModes.NO );
} else {
// =======================================================
// resizes photo if it is in portrait orientation
var idImgS = charIDToTypeID( "ImgS" );
var desc05 = new ActionDescriptor();
var idHght = charIDToTypeID( "Hght" );
var idPxl = charIDToTypeID( "#Pxl" );
desc05.putUnitDouble( idHght, idPxl, longEdgeOfDocument );
var idscaleStyles = stringIDToTypeID( "scaleStyles" );
desc05.putBoolean( idscaleStyles, true );
var idCnsP = charIDToTypeID( "CnsP" );
desc05.putBoolean( idCnsP, true );
var idIntr = charIDToTypeID( "Intr" );
var idIntp = charIDToTypeID( "Intp" );
var idautomaticInterpolation = stringIDToTypeID( "automaticInterpolation" );
desc05.putEnumerated( idIntr, idIntp, idautomaticInterpolation );
executeAction( idImgS, desc05, DialogModes.NO );
}
// appends the width of the document
baseDocName += currentDocument.width.toString().replace(' px', '');
// =======================================================
// saves jpg photo
var idsave = charIDToTypeID( "save" );
var desc03 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc04 = new ActionDescriptor();
var idEQlt = charIDToTypeID( "EQlt" );
desc04.putInteger( idEQlt, quality );
var idMttC = charIDToTypeID( "MttC" );
var idMttC = charIDToTypeID( "MttC" );
var idNone = charIDToTypeID( "None" );
desc04.putEnumerated( idMttC, idMttC, idNone );
var idJPEG = charIDToTypeID( "JPEG" );
desc03.putObject( idAs, idJPEG, desc04 );
var idIn = charIDToTypeID( "In " );
desc03.putPath( idIn, new File( saveLocation + baseDocName + ".jpg" ) );
var idDocI = charIDToTypeID( "DocI" );
desc03.putInteger( idDocI, 1363 );
var idCpy = charIDToTypeID( "Cpy " );
desc03.putBoolean( idCpy, true );
var idsaveStage = stringIDToTypeID( "saveStage" );
var idsaveStageType = stringIDToTypeID( "saveStageType" );
var idsaveBegin = stringIDToTypeID( "saveBegin" );
desc03.putEnumerated( idsaveStage, idsaveStageType, idsaveBegin );
executeAction( idsave, desc03, DialogModes.NO );
// =======================================================
// closes photoshop file
currentDocument.close(SaveOptions.DONOTSAVECHANGES);
};
main();