-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAi Sessions.jsx
321 lines (269 loc) · 8.66 KB
/
Ai Sessions.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* @author Scott Lewis <scott@iconify.it>
* @copyright 2017 Scott Lewis
* @version 1.0.0
* @url http://github.com/iconifyit
*
* ABOUT:
*
* This script demonstrates how to implement a progress bar in your script.
*
* USAGE:
*
* 1. Place this script in Applications > Adobe Illustrator > Presets > en_US > Scripts
* 2. Restart Adobe Illustrator to activate the script
* 3. The script will be available under menu File > Scripts > Ai Sessions
*
* NO WARRANTIES:
*
* You are free to use, modify, and distribute this script as you see fit.
* No credit is required but would be greatly appreciated.
*
* THIS SCRIPT IS OFFERED AS-IS WITHOUT ANY WARRANTY OR GUARANTEES OF ANY KIND.
* YOU USE THIS SCRIPT COMPLETELY AT YOUR OWN RISK AND UNDER NO CIRCUMSTANCES WILL
* THE DEVELOPER AND/OR DISTRIBUTOR OF THIS SCRIPT BE HELD LIABLE FOR DAMAGES OF
* ANY KIND INCLUDING LOSS OF DATA OR DAMAGE TO HARDWARE OR SOFTWARE. IF YOU DO
* NOT AGREE TO THESE TERMS, DO NOT USE THIS SCRIPT.
*/
/**
* Declare the target app.
*/
#target illustrator
/**
* Include the libraries we need.
*/
#includepath "/Users/scott/github/iconify/jsx-common/";
#include "JSON.jsxinc";
#include "Utils.jsxinc";
#include "Logger.jsxinc";
/**
* Name that script.
*/
#script "Ai Sessions";
/**
* Disable Illustrator's alerts.
*/
Utils.displayAlertsOff();
/**
* Set some global variables.
*/
var DATE_STRING = Utils.dateFormat(new Date().getTime());
var SESSION_FILENAME = "ai-" + DATE_STRING + "-r1.json";
/**
* @type {{
* SRCFOLDER: string,
* LOGFOLDER: string,
* LOGFILE: string,
* NO_OPEN_DOCS: *,
* NO_DOC_SELECTED: *,
* SESSION_SAVED: *,
* ENTER_FILENAME: *,
* JSON_EXT: string,
* TEXT_EXT: string
* }}
*/
var CONFIG = {
APP_NAME : "ai-sessions",
SRCFOLDER : "/Users/scott/Dropbox/Dropbox (Personal)/ai-sessions",
LOGFOLDER : "/Users/scott/Dropbox/Dropbox (Personal)/ai-sessions/logs",
LOGFILE : "/Users/scott/Dropbox/Dropbox (Personal)/ai-sessions/logs/ai-log-" + DATE_STRING + "-r1.log",
NO_OPEN_DOCS : localize({en_US: "There are no open docs to save for this session"}),
NO_DOC_SELECTED : localize({en_US: "You have not selected a session to open"}),
SESSION_SAVED : localize({en_US: "Your Session Was Saved!"}),
ENTER_FILENAME : localize({en_US: "Enter a session file name or click enter to use the default name"}),
JSON_EXT : ".json",
TEXT_EXT : ".txt"
};
/**
* Run the script using the Module patter.
*/
var AiSessions = (function(CONFIG) {
/**
* The module dialog.
* @type {Window}
*/
var dialog = null;
/**
* The local scope logger object.
* @type {Logger}
*/
var logger = new Logger(CONFIG.APP_NAME, CONFIG.LOGFOLDER);
/**
* The Dialog for this module.
* @returns {*}
* @constructor
*/
var Dialog = function() {
// Show dialog in center of screen
dialog = Utils.window(
"dialog",
Utils.i18n("Ai Sessions"),
350, 350
);
// Message area
dialog.msgBox = dialog.add("statictext", [30,30,300,60], "");
// Cancel button
dialog.closeBtn = dialog.add("button", [30,275,120,315], "Close", {name:"close"});
dialog.closeBtn.onClick = function() { dialog.close(); };
dialog.openBtn = dialog.add("button", [130,275,220,315], "Open", {name:"open"});
dialog.openBtn.enabled = false;
dialog.openBtn.onClick = function() {
doOpenCallback(dialog.sessions.selection.text);
};
dialog.saveBtn = dialog.add("button", [230,275,320,315], "Save", {name:"save"});
dialog.saveBtn.onClick = doSaveCallback;
initSessionsList();
return dialog;
};
/**
* Populates the sessions select list.
*/
var initSessionsList = function() {
var sessions = new Folder(CONFIG.SRCFOLDER).getFiles("*.json");
if (! sessions.length) {
dialog.msgBox.text = Utils.i18n("You have no saved sessions");
}
else {
if (dialog.sessions) {
dialog.sessions.removeAll();
}
/**
* Let's show the newest sessions at the top.
*/
sessions.sort(comparator);
sessions.reverse();
dialog.sessions = dialog.add("listbox", [30, 70, 320, 230]);
for (i=0; i < sessions.length; i++) {
item = dialog.sessions.add("item", (new File(sessions[i])).name);
}
dialog.sessions.onChange = function() {
dialog.openBtn.enabled = true;
}
dialog.sessions.onDoubleClick = function() {
dialog.openBtn.enabled = true;
doOpenCallback(dialog.sessions.selection.text);
}
}
};
/**
* Callback to open the selected session.
* @param filepath
*/
var doOpenCallback = function(filepath) {
filepath = CONFIG.SRCFOLDER + "/" + filepath;
var theFile = new File(decodeURI(filepath));
if (theFile.exists) {
dialog.close();
try {
if (theFile.alias) {
while (theFile.alias) {
theFile = theFile.resolve().openDlg(
CONFIG.CHOOSE_FILE,
txt_filter,
false
);
}
}
}
catch(ex) {
dialog.msgBox.text = ex.message;
}
try {
var session = Utils.read_json_file(theFile);
if (typeof(session) == 'object') {
if (session.files) {
for(i=0; i<session.files.length; i++) {
var ai_file_path = decodeURIComponent(session.files[i]);
var thisFile = new File(ai_file_path);
if (thisFile.exists) {
doc = app.open(thisFile);
app.executeMenuCommand('fitall');
}
}
}
}
}
catch(ex) {
dialog.msgBox.text = ex.message;
logger.error(ex.message);
}
}
else {
logger.error(
localize({en_US: "%1 - %2 - File `%3` does not exist."}, $.line, $.fileName, filepath)
);
}
Utils.displayAlertsOn();
};
/**
* Saves the current session.
*/
var doSaveCallback = function() {
if (app.documents.length == 0) {
alert(CONFIG.NO_OPEN_DOCS);
}
else {
try {
var openDocs = [];
for (x=0; x<app.documents.length; x++) {
openDocs.push(
'"' + app.documents[x].path + "/" + app.documents[x].name + '"'
);
}
var testFile = new File(CONFIG.SRCFOLDER + "/" + SESSION_FILENAME);
var n = 1;
var max = 100;
while (testFile.exists && n < max) {
SESSION_FILENAME = "ai-" + DATE_STRING + "-r" + n + CONFIG.JSON_EXT;
testFile = new File(CONFIG.SRCFOLDER + "/" + SESSION_FILENAME);
n++;
}
Utils.write_file(
CONFIG.SRCFOLDER + "/" + SESSION_FILENAME,
'{"files":[\r' + ' ' + openDocs.join(',\r ') + '\r]}',
true
);
initSessionsList(dialog);
dialog.msgBox.text = CONFIG.SESSION_SAVED;
dialog.saveBtn.enabled = false;
}
catch(ex) {
logger.error(ex.message);
}
}
Utils.displayAlertsOn();
};
/**
* Callback for sorting the file list.
* @param {File} a
* @param {File} b
* @returns {number}
*/
var comparator = function(a, b) {
var nameA = Utils.slugger(a.name.toUpperCase());
var nameB = Utils.slugger(b.name.toUpperCase());
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
}
/**
* Returns the public module object.
*/
return {
/**
* Runs the module code.
*/
run: function() {
new Dialog().show();
}
}
})(CONFIG);
/**
* Run the module.
*/
AiSessions.run();