Skip to content

Instantly share code, notes, and snippets.

@vitorgalvao
Created February 11, 2023 19:01
Show Gist options
  • Save vitorgalvao/8d3249f4bf91ab38e47fcc3c1885fb77 to your computer and use it in GitHub Desktop.
Save vitorgalvao/8d3249f4bf91ab38e47fcc3c1885fb77 to your computer and use it in GitHub Desktop.
JavaScript for Automation (JXA) for opening a new tab in different browsers
// JavaScript for Automation (JXA) for opening a new tab in different browsers.
// Doing an "open 'https://example.com/'" and letting macOS handle it is usually preferable,
// but sometimes you want to specifically open a tab in a browser which is not the default.
// Chromium-based browsers
// Other examples include "Brave Browser", "Vivaldi"
const browser = Application("Google Chrome")
const newTab = browser.Tab({ url: "https://example.com/" })
browser.windows[0].tabs.push(newTab)
// Webkit-based browsers
// Other examples include "Safari Technology Preview"
const browser = Application("Safari")
const newTab = browser.Tab({ url: "https://example.com/" })
const frontWindow = browser.windows[0]
frontWindow.tabs.push(newTab) // Open in background
frontWindow.currentTab = newTab // Switch to tab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment