-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminalIsFrontWindow.js
More file actions
executable file
·106 lines (87 loc) · 2.25 KB
/
terminalIsFrontWindow.js
File metadata and controls
executable file
·106 lines (87 loc) · 2.25 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
#!/usr/bin/osascript -l JavaScript
const terminal = Application("Terminal");
const skipKeys = {
content: true,
history: true,
closeable: true,
zoomed: true,
pcls: true,
name: true,
miniaturizable: true,
miniaturized: true,
resizable: true,
zoomable: true,
contents: true,
history: true
};
function toObj(path, objIn) {
const objInType = typeof objIn;
if('function' == objInType) {
const value = objIn();
const valueType = typeof value;
// console.log('objInType:', path, objInType, valueType);
return value;
}
if('object' == objInType) {
if(Array.isArray(objIn)) {
// console.log('objInType:', path, objInType, 'Array');
return objIn.map((e, i) => toObj(path.concat(i), e));
}
// console.log('objInType:', path, objInType);
const objOut = {};
for(let key in objIn) {
if(skipKeys[key]) {
continue;
}
objOut[key] = toObj(path.concat(key), objIn[key]);
}
return objOut;
}
// console.log('objInType:', path, objInType);
return objIn;
}
function windowInfo() {
const windows = terminal.windows();
let frontWindowGroupId = null;
const winGroups = {};
for(let winId in windows) {
const win = windows[winId];
const winPath = ['win', winId];
const winProps = toObj(winPath, win.properties());
// console.log(winPath, JSON.stringify(winProps, null, 2));
const winOrigin = winProps.origin;
const key = [winOrigin.x,winOrigin.y].join(":");
if(winProps.frontmost) {
frontWindowGroupId = key;
}
let winGroup = winGroups[key];
if(!winGroup) {
winGroup = [];
winGroups[key] = winGroup;
}
winGroup.push({ win, winPath, winProps, winOrigin });
// console.log(winPath, key, JSON.stringify(winOrigin, null, 2));
/*
for(let tabId in win.tabs) {
const tab = win.tabs[tabId];
const tabPath = winPath.concat(['tabs', tabId]);
const tabProps = toObj(tabPath, tab.properties());
console.log(tabPath, JSON.stringify(tabProps, null, 2));
}
*/
}
return {
frontWindowGroupId,
groups: winGroups
};
}
function isFrontWindow() {
const winInfo = windowInfo();
const frontWindowGroup = winInfo.groups[winInfo.frontWindowGroupId];
// console.log(JSON.stringify(frontWindowGroup, null, 2));
if(frontWindowGroup.length == 1) {
return 'true';
}
return 'false';
}
isFrontWindow();