-
Notifications
You must be signed in to change notification settings - Fork 8
/
test-macos-defaults.js
91 lines (84 loc) · 3.04 KB
/
test-macos-defaults.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
const {MacOSDefaults} = require('macos-defaults');
const mod = new MacOSDefaults();
const mdls = require('mdls');
/*
// Todo: Should add support of unescaped dates to `macos-defaults` (as returned by this test)
const {execSync} = require('child_process');
const PlistParser = require('macos-defaults/PlistParser');
const plist = '{' +
execSync('mdls "/Users/brett/Library/Preferences/com.apple.iWork.Numbers.plist"', {encoding: 'utf-8'}) +
'}';
console.log('plist', plist);
const parser = new PlistParser({
plist,
// allowAngledBracketStrings: true,
allowMissingSeparators: true
});
const info = parser.start();
console.log(info);
*/
/*
// These are apparently potentially available
LSHandlerRoleAll
LSHandlerRoleViewer
LSHandlerRoleEditor
LSHandlerRoleShell
Also has `LSHandlerPreferredVersions` (which may have its own `LSHandlerRoleAll` at least, e.g., set to `-`)
*/
async function getLaunchServiceHandlers ({contentTag, contentType, type = 'extension'}) {
switch (type) {
// eslint-disable-next-line default-case-last
default: {
if (type) { // Allow user to specify their own manually
break;
}
} // Fallthrough
case 'extension':
type = 'public.filename-extension';
break;
case 'mime': {
type = 'public.mime-type';
break;
}
}
const LSHandlers = await mod.read('com.apple.LaunchServices/com.apple.launchservices.secure', 'LSHandlers');
// console.log('LSHandlers', LSHandlers);
if (contentType) {
const result = LSHandlers.find(({LSHandlerContentType}) => {
return LSHandlerContentType && contentType === LSHandlerContentType;
});
// Allow `contentTag` to execute if this fails
if (result) {
return result;
}
}
if (type === 'public.mime-type') { // For MIME type, we allow `contentType` as parameter since it really is one
contentTag = contentType;
}
if (!contentTag) {
throw new Error('`getLaunchServiceHandlers()` is missing a `contentTag` or `contentType` argument');
}
return LSHandlers.find(({LSHandlerContentTag, LSHandlerContentTagClass}) => {
return LSHandlerContentTag &&
LSHandlerContentTagClass === type &&
contentTag === LSHandlerContentTag;
});
}
(async () => {
try {
const data = await mdls('/Users/brett/macOS-defaults/test/parser.js');
// Note: the prefix "kMD" is stripped
console.log('ItemContentType', data.ItemContentType);
// Todo: This may also be useful if handler not found of that type
console.log('ItemContentTypeTree', data.ItemContentTypeTree);
} catch (err) {
console.log(err);
return;
}
const handlersCSS = await getLaunchServiceHandlers({contentType: 'public.css'});
// console.log('handlers', handlers.LSHandlerPreferredVersions);
console.log('handlersCSS', handlersCSS.LSHandlerRoleAll);
const handlersByTag = await getLaunchServiceHandlers({contentTag: 'sqlite'});
console.log('handlersByTag', handlersByTag.LSHandlerRoleAll);
// const handlersCSSMimeType = await getLaunchServiceHandlers({contentTag: 'text/css', type: 'mime'}); // https://superuser.com/questions/421792/how-to-associate-mime-type-with-a-handler-in-os-x
})();