-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresizeBatchForWeb.jsx
More file actions
145 lines (121 loc) · 6.03 KB
/
resizeBatchForWeb.jsx
File metadata and controls
145 lines (121 loc) · 6.03 KB
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
// Created by Nathaniel Young (nyoungstudios)
// this script resizes all photos in the folder with the optimal settings for web use including services like Squarespace or Wix
// saves resized files in a subfolder called "Resize"
//wrapper function
function main() {
// opens folder dialog for user to select the folder to process
var inputFolder = Folder.selectDialog("Pick a source folder");
// checks if user selected no folder
if (inputFolder == null) {
return;
}
// regular expression to match file types
var fileTypes = new RegExp('.*.jpg|.*.png|.*.tif|.*.jpeg', 'i');
// gets list of files that match regular expression
var fileList = inputFolder.getFiles(fileTypes);
// parameters
var longEdgeOfDocument = 2048;
var quality = 10;
// loops over each file in the folder
for (var i = 0; i < fileList.length; i++) {
// gets file path name
var filePathName = fileList[i].toString();
// removes the file extension
var saveLocation = filePathName.substring(0, filePathName.lastIndexOf('/') + 1) + 'Resize/';
var baseDocName = filePathName.substring(filePathName.lastIndexOf('/') + 1, filePathName.lastIndexOf('.'));
// Creates Resize folder if it does not already exist
var folder = new Folder(saveLocation);
if (i == 0 && !folder.exists) {
folder.create();
}
// 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();