mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-06-13 15:13:32 -07:00
36 lines
978 B
TypeScript
36 lines
978 B
TypeScript
import { execFile as nodeExecFile } from 'node:child_process';
|
|
|
|
const FOCUS_MPV_PROCESS_SCRIPT =
|
|
'tell application "System Events" to set frontmost of the first process whose name is "mpv" to true';
|
|
|
|
type ExecFileForMacOSFocus = (
|
|
command: string,
|
|
args: string[],
|
|
options: { timeout: number },
|
|
callback: (error: Error | null) => void,
|
|
) => void;
|
|
|
|
export type MacOSMpvFocusDeps = {
|
|
execFile?: ExecFileForMacOSFocus;
|
|
};
|
|
|
|
export async function focusMacOSMpvProcess(deps: MacOSMpvFocusDeps = {}): Promise<void> {
|
|
const execFile: ExecFileForMacOSFocus =
|
|
deps.execFile ??
|
|
((command, args, options, callback) => {
|
|
nodeExecFile(command, args, options, (error) => {
|
|
callback(error);
|
|
});
|
|
});
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
execFile('/usr/bin/osascript', ['-e', FOCUS_MPV_PROCESS_SCRIPT], { timeout: 2000 }, (error) => {
|
|
if (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
resolve();
|
|
});
|
|
});
|
|
}
|