-
Notifications
You must be signed in to change notification settings - Fork 2
/
jxaSystemIO.js
481 lines (422 loc) · 13.3 KB
/
jxaSystemIO.js
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* eslint-disable max-lines-per-function */
/* eslint-disable no-undef */
/* eslint-disable strict */
/* eslint-disable no-unused-vars */
// alert :: String => String -> IO String
const alert = title =>
// Display of a given title and message.
s => {
const sa = Object.assign(
Application("System Events"), {
includeStandardAdditions: true
});
return (
sa.activate(),
sa.displayDialog(s, {
withTitle: title,
buttons: ["OK"],
defaultButton: "OK"
}),
s
);
};
// appendFile :: FilePath -> String -> IO Bool
const appendFile = fp =>
// The file at fp updated with a new string
// appended to its existing contents.
txt => {
const fpFull = filePath(fp);
return doesFileExist(fpFull)
? (() => {
const
h = $.NSFileHandle
.fileHandleForWritingAtPath(
$(fpFull)
);
return (
h.seekToEndOfFile,
h.writeData(
$(txt)
.dataUsingEncoding(
$.NSUTF8StringEncoding
)
),
h.closeFile,
true
);
})()
: doesDirectoryExist(takeDirectory(fpFull))
? (writeFile(fpFull)(txt), true)
: false;
};
// appendFileMay :: FilePath -> String -> Maybe IO FilePath
const appendFileMay = fp =>
// Just the fully-expanded file path of
// any file at found strPath, after it has been
// updated by appending the given string, or
// Nothing if no file is found at that path,
// or the file is found but can not be updated.
txt => {
const fpFull = filePath(fp);
return doesFileExist(fpFull)
? (() => {
const
h = $.NSFileHandle
.fileHandleForWritingAtPath(
$(fpFull)
);
return (
h.seekToEndOfFile,
h.writeData(
$(txt)
.dataUsingEncoding(
$.NSUTF8StringEncoding
)
),
h.closeFile,
Just(fpFull)
);
})()
: doesDirectoryExist(takeDirectory(fpFull))
? (
writeFile(fpFull)(txt),
Just(fpFull)
)
: Nothing();
};
// base64decode :: String -> String
const base64decode = s =>
// Base64 decoding of the given string.
ObjC.unwrap(
$.NSString.alloc.initWithDataEncoding(
$.NSData.alloc.initWithBase64EncodedStringOptions(
s, $.NSDataBase64DecodingIgnoreUnknownCharacters
),
$.NSUTF8StringEncoding
)
);
// base64encode :: String -> String
const base64encode = s =>
// Base64 encoding of the given string.
ObjC.unwrap(
$.NSString.stringWithString(s)
.dataUsingEncoding(
$.NSUTF8StringEncoding
)
.base64EncodedStringWithOptions(0)
);
// copyFileLR :: FilePath -> FilePath -> Either String IO ()
const copyFileLR = fpFrom =>
fpTo => {
const fpTargetFolder = takeDirectory(fpTo);
return doesFileExist(fpFrom)
? doesDirectoryExist(fpTargetFolder)
? (() => {
const
e = $(),
fpTarget = $(fpTo).stringByStandardizingPath;
return (
$.NSFileManager.defaultManager
.copyItemAtPathToPathError(
$(fpFrom).stringByStandardizingPath,
fpTarget,
e
)
? Right(ObjC.unwrap(fpTarget))
: Left(ObjC.unwrap(e.localizedDescription))
);
})()
: Left(`Target folder not found: ${fpTargetFolder}`)
: Left(`Source file not found: ${fpFrom}`);
};
// createDirectoryIfMissingLR :: Bool -> FilePath
// -> Either String FilePath
const createDirectoryIfMissingLR = blnParents =>
dirPath => {
const fp = filePath(dirPath);
return doesPathExist(fp)
? Right(fp)
: (() => {
const
e = $(),
blnOK = $.NSFileManager
.defaultManager[
"createDirectoryAtPath" + (
"WithIntermediateDirectories"
) + "AttributesError"
](fp, blnParents, void 0, e);
return blnOK
? Right(fp)
: Left(e.localizedDescription);
})();
};
// doesDirectoryExist :: FilePath -> IO Bool
const doesDirectoryExist = fp => {
const ref = Ref();
return $.NSFileManager.defaultManager
.fileExistsAtPathIsDirectory(
$(fp)
.stringByStandardizingPath, ref
) && ref[0];
};
// doesFileExist :: FilePath -> IO Bool
const doesFileExist = fp => {
const ref = Ref();
return $.NSFileManager
.defaultManager
.fileExistsAtPathIsDirectory(
$(fp).stringByStandardizingPath,
ref
) && !ref[0];
};
// doesPathExist :: FilePath -> IO Bool
const doesPathExist = fp =>
$.NSFileManager.defaultManager
.fileExistsAtPath(
$(fp).stringByStandardizingPath
);
// filePath :: String -> FilePath
const filePath = s =>
// The given file path with any tilde expanded
// to the full user directory path.
ObjC.unwrap(
$(s).stringByStandardizingPath
);
// fileSize :: FilePath -> Either String Int
const fileSize = fp =>
bindLR(fileStatus(fp))(
dct => Right(ObjC.unwrap(dct.NSFileSize))
);
// fileStatus :: FilePath -> Either String Dict
const fileStatus = fp => {
const
e = $(),
dct = $.NSFileManager.defaultManager
.attributesOfItemAtPathError(
$(fp).stringByStandardizingPath,
e
);
return dct.isNil()
? Left(ObjC.unwrap(e.localizedDescription))
: Right(ObjC.deepUnwrap(dct));
};
// fileUTI :: FilePath -> Either String String
const fileUTI = fp => {
// ObjC.import("AppKit")
const
e = $(),
uti = $.NSWorkspace.sharedWorkspace
.typeOfFileError(fp, e);
return uti.isNil()
? Left(ObjC.unwrap(e.localizedDescription))
: Right(ObjC.unwrap(uti));
};
// filesCopiedLR :: FilePath -> [FilePath] ->
// FilePath -> IO Either String [FilePath]
const filesCopiedLR = fpSourceFolder =>
// Either a message, or a list of the files
// successfully copied from the source folder
// to the target folder, or already found in place.
fileNames => fpTargetFolder => {
const
lrs = fileNames.map(k => {
const tgt = combine(fpTargetFolder)(k);
return doesFileExist(tgt)
? Right(tgt)
: copyFileLR(
combine(fpSourceFolder)(k)
)(tgt);
}),
ls = lefts(lrs);
return 0 < ls.length
? Left(ls[0])
: Right(rights(lrs));
};
// getCurrentDirectory :: IO FilePath
const getCurrentDirectory = () =>
ObjC.unwrap(
$.NSFileManager.defaultManager
.currentDirectoryPath
);
// getDirectoryContentsLR :: FilePath ->
// Either String IO [FilePath]
const getDirectoryContentsLR = fp => {
const
error = $(),
xs = $.NSFileManager.defaultManager
.contentsOfDirectoryAtPathError(
$(fp).stringByStandardizingPath,
error
);
return xs.isNil()
? Left(ObjC.unwrap(error.localizedDescription))
: Right(ObjC.deepUnwrap(xs));
};
// getHomeDirectory :: IO FilePath
const getHomeDirectory = () =>
ObjC.unwrap($.NSHomeDirectory());
// getTemporaryDirectory :: IO FilePath
const getTemporaryDirectory = () =>
ObjC.unwrap($.NSTemporaryDirectory());
// listDirectory :: FilePath -> [FilePath]
const listDirectory = fp =>
ObjC.deepUnwrap(
$.NSFileManager.defaultManager
.contentsOfDirectoryAtPathError(
$(fp).stringByStandardizingPath,
null
)
);
// modificationTime :: FilePath -> Either String Date
const modificationTime = fp =>
bindLR(fileStatus(fp))(
dct => Right(ObjC.unwrap(dct.NSFileModificationDate))
);
// newUUID :: () -> IO UUID String
const newUUID = () =>
ObjC.unwrap($.NSUUID.UUID.UUIDString);
// readFile :: FilePath -> IO String
const readFile = fp => {
// The contents of a text file at the
// given file path.
const
e = $(),
ns = $.NSString
.stringWithContentsOfFileEncodingError(
$(fp).stringByStandardizingPath,
$.NSUTF8StringEncoding,
e
);
return ObjC.unwrap(
ns.isNil()
? e.localizedDescription
: ns
);
};
// readFileLR :: FilePath -> Either String IO String
const readFileLR = fp => {
// Either a message or the contents of any
// text file at the given filepath.
const
uw = ObjC.unwrap,
e = $(),
ns = $.NSString
.stringWithContentsOfFileEncodingError(
$(fp).stringByStandardizingPath,
$.NSUTF8StringEncoding,
e
);
return ns.isNil()
? Left(uw(e.localizedDescription))
: Right(uw(ns));
};
// readPlistFileLR :: FilePath -> Either String Dict
const readPlistFileLR = fp =>
// Either a message or a dictionary of key-value
// pairs read from the given file path.
bindLR(
doesFileExist(fp)
? Right(filePath(fp))
: Left(`No file found at path:\n\t${fp}`)
)(
fpFull => {
const
e = $(),
maybeDict = $.NSDictionary
.dictionaryWithContentsOfURLError(
$.NSURL.fileURLWithPath(fpFull),
e
);
return maybeDict.isNil()
? (() => {
const
msg = ObjC.unwrap(
e.localizedDescription
);
return Left(`readPlistFileLR:\n\t${msg}`);
})()
: Right(ObjC.deepUnwrap(maybeDict));
}
);
// removeFileLR :: FilePath -> Either String String
const removeFileLR = fp => {
const error = $();
return $.NSFileManager.defaultManager
.removeItemAtPathError(fp, error)
? Right(`Removed: ${fp}`)
: Left(ObjC.unwrap(error.localizedDescription));
};
// renamedFile :: FilePath -> FilePath ->
// Either IO String IO String
const renamedFile = fp =>
// Either a message detailing a problem, or
// confirmation of a filename change in the OS.
fp1 => {
const error = $();
return $.NSFileManager.defaultManager
.moveItemAtPathToPathError(fp, fp1, error)
? Right(fp1)
: Left(
ObjC.unwrap(error.localizedDescription)
);
};
// setCurrentDirectory :: FilePath -> IO ()
const setCurrentDirectory = fp =>
$.NSFileManager.defaultManager
.changeCurrentDirectoryPath(
$(fp).stringByStandardizingPath
);
// tempFilePath :: String -> IO FilePath
const tempFilePath = template => {
// File name template to temporary path
// Random digit sequence inserted between
// template base and extension
const
fldr = ObjC.unwrap($.NSTemporaryDirectory()),
name = takeBaseName(template),
xtn = takeExtension(template),
rnd = Math.random().toString()
.substring(3);
return `${fldr}${name}${rnd}${xtn}`;
};
// unwrap :: NSObject -> a
const unwrap = ObjC.unwrap;
// wrap :: a -> NSObject
const wrap = ObjC.wrap;
// writeFile :: FilePath -> String -> IO ()
const writeFile = fp => s =>
$.NSString.alloc.initWithUTF8String(s)
.writeToFileAtomicallyEncodingError(
$(fp)
.stringByStandardizingPath, false,
$.NSUTF8StringEncoding, null
);
// writeFileLR :: FilePath ->
// String -> Either String IO FilePath
const writeFileLR = fp =>
// Either a message or the filepath
// to which the string has been written.
s => {
const
e = $(),
efp = $(fp).stringByStandardizingPath;
return $.NSString.alloc
.initWithUTF8String(s)
.writeToFileAtomicallyEncodingError(
efp, false,
$.NSUTF8StringEncoding, e
)
? Right(ObjC.unwrap(efp))
: Left(ObjC.unwrap(e.localizedDescription));
};
// writeTempFile :: String -> String -> IO FilePath
const writeTempFile = template =>
// File name template -> string data -> IO temporary path
txt => {
const
fp = ObjC.unwrap($.NSTemporaryDirectory()) +
takeBaseName(template) + Math.random()
.toString()
.substring(3) + takeExtension(template);
return (writeFile(fp)(txt), fp);
};