Skip to content

Instantly share code, notes, and snippets.

@boraseoksoon
Created May 9, 2024 00:53
Show Gist options
  • Save boraseoksoon/64897b17735552195e72992f97d647ad to your computer and use it in GitHub Desktop.
Save boraseoksoon/64897b17735552195e72992f97d647ad to your computer and use it in GitHub Desktop.
open safari
// openSafari.js;
/**
* Function to open Safari browser on macOS using AppleScript
*/
async function openSafari() {
// Create the AppleScript command to activate Safari
const command = `osascript -e 'tell application "Safari" to activate'`;
// Run the command via bash in a subprocess
const process = Deno.run({
cmd: ["bash", "-c", command],
stdout: "piped",
stderr: "piped",
});
// Await the subprocess result
const { code } = await process.status();
// If the subprocess succeeded
if (code === 0) {
const output = new TextDecoder().decode(await process.output());
console.log("Safari opened successfully:", output);
} else {
const error = new TextDecoder().decode(await process.stderrOutput());
console.error("Error opening Safari:", error);
}
// Close the process
process.close();
}
// Export the openSafari function as the default export
export default openSafari;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment