-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-or-raise.js
More file actions
executable file
·133 lines (125 loc) · 3.36 KB
/
run-or-raise.js
File metadata and controls
executable file
·133 lines (125 loc) · 3.36 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
#!/usr/bin/env node
const { exec, spawn } = require('child_process');
const config = {
blink: [
'• Blink',
[
'/opt/google/chrome/google-chrome',
'--profile-directory=Default',
'--app-id=nbjihfimkbmgnfhjgpinlonjmecnigia'
],
'Jetbrains'
],
chrome: [
'google-chrome',
['/usr/bin/google-chrome-stable'],
"• Blink"
],
code: [
'Visual Studio Code',
['/usr/share/code/code']
],
datagrip: [
'jetbrains-datagrip',
['/home/kian/.local/share/JetBrains/Toolbox/apps/datagrip/bin/datagrip']
],
firefox: [
'org.mozilla.firefox',
['/usr/bin/firefox']
],
intellij: [
'jetbrains-idea',
['/home/kian/.local/share/JetBrains/Toolbox/apps/intellij-idea-ultimate/bin/idea']
],
lens: [
'Lens',
['/opt/Lens/lens-desktop']
],
postman: [
'Postman',
[
'/usr/bin/flatpak',
'run',
'--branch=stable',
'--arch=x86_64',
'--command=Postman',
'--file-forwarding',
'com.getpostman.Postman'
]
],
spotify: [
'Spotify',
[
'/usr/bin/flatpak',
'run',
'--branch=stable',
'--arch=x86_64',
'--command=spotify',
'--file-forwarding',
'com.spotify.Client'
]
],
terminal: [
'Konsole',
['konsole']
],
}
async function main() {
const app = process.argv[2];
const reverseOrder = process.argv[3] === '--reverse';
const [windowQuery, cmdToRun, excludeQuery] = config[app] || [null, null];
if (!windowQuery || !cmdToRun) {
console.error(`Usage: ${process.argv[0]} ${process.argv[1]} <app-name>`);
process.exit(1);
}
const windowIds = await getWindowIds(windowQuery);
const excludedWindowIds = excludeQuery ? await getWindowIds(excludeQuery) : [];
const filteredWindowIds = windowIds.filter(id => !excludedWindowIds.includes(id));
if (filteredWindowIds.length === 0) {
// No windows found with the query; start the application
spawn(cmdToRun[0], cmdToRun.slice(1), {
stdio: "inherit", // Inherit stdin, stdout, stderr
detached: true, // Detach the child process
shell: true // Use shell to handle commands properly
});
console.debug(`Spawned process`);
} else {
const windowId = reverseOrder
? getRandomWindow(filteredWindowIds, await getCurrentWindowId())
: filteredWindowIds[0];
// Activate the first window found
await execPromise(`kdotool windowactivate ${windowId}`);
console.debug(`Activated window with ID: ${windowId}`);
}
}
main()
.then(() => process.exit(0))
.catch(err => {
console.error(`Error: ${err}`);
process.exit(1);
});
function getRandomWindow(windowIds, currentWindowId) {
const filteredWindowIds = windowIds.filter(id => id !== currentWindowId);
return filteredWindowIds[(Math.floor(Math.random() * filteredWindowIds.length))];
}
async function getWindowIds(query) {
const output = await execPromise(`kdotool search '${query}'`);
return output.split('\n').filter(Boolean);
}
async function getCurrentWindowId() {
const output = await execPromise(`kdotool getactivewindow`);
return output.trim();
}
function execPromise(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(`Error executing command: ${error.message}`);
}
if (stderr) {
reject(`stderr: ${stderr}`);
}
resolve(stdout);
});
});
}