Skip to content

Instantly share code, notes, and snippets.

@nomagick
Created April 28, 2022 09:49
Show Gist options
  • Save nomagick/8a91ebc255bd6840b9d35d3909caf58f to your computer and use it in GitHub Desktop.
Save nomagick/8a91ebc255bd6840b9d35d3909caf58f to your computer and use it in GitHub Desktop.
Jxa sudo for electron
import { run } from '@jxa/run';
declare const Application: any;
export async function jxaRun<T>(func: Function, ...args: any[]) {
func.toString = () => {
return Function.prototype.toString.call(func).replace(/^\((.*?)\)\s*=>/g, 'function ($1)');
};
return run<T>(func as any, ...args);
}
export async function jxaSudo(command: string, prompt?: string) {
return jxaRun((command: string, prompt: string) => {
const app = Application.currentApplication();
app.includeStandardAdditions = true;
return app.doShellScript(command, {
administratorPrivileges: true,
withPrompt: prompt || '执行一次性命令'
});
}, command, prompt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment