My goal:
Use a quick hotkey (option comma) so I can begin typing in the address bar of Safari, knowing it's a new private tab, while reducing likelihood of unnecessary clutter of new private windows if the last window I was using was already private. (More reliable at ensuring I'll end up in a private tab than "Search the Web" action, which will open search results in a non-private window if that's what I was using last.)
Disclaimer:
"Works on my machine" - an LLM-generated AppleScript inside a Keyboard Maestro macro; use at your own risk.
What this macro should do:
- If Safari has no windows open → Opens a new Private window
- If the front window is already Private → Opens a new tab in that window
- If the front window is not Private → Opens a new Private window
How it works:
The script checks if the front Safari window's title contains "Private" to determine if it's a Private window. It then either opens a new tab (using the File menu, which respects your Start Page settings) or a new Private window as appropriate. - LMK if you prefer a wholly blank private tab instead of the Start Page tab.
Setup:
Either create a new macro with an Execute AppleScript action and paste this code, or grab the macro from below.
AppleScript:
Code
-- Guaranteed Private Tab (optimized)
-- - Launches Safari if needed
-- - If front window is Private -> opens a new tab (via menu/UI, Start Page honored)
-- - If not -> opens a new Private Window, with a short wait to ensure it exists
property privLabel : "Private" -- change if your UI is not English
on run
try
my ensureSafariRunningAndActivated()
-- If there are no windows, open a Private Window and finish
tell application "Safari" to set wCount to (count of windows)
if wCount = 0 then
my openNewPrivateWindow()
return "Opened new Private Window (no windows)."
end if
-- If the front window is Private -> open a tab; else open a Private Window
if my isPrivateByTitle(privLabel) then
my openNewTabInFrontWindow()
return "Opened a new tab in an existing Private window."
else
my openNewPrivateWindow()
return "Opened a new Private Window."
end if
on error errMsg number errNum
if errNum is not 0 then set errMsg to errMsg & " (" & errNum & ")"
return "Failed: " & errMsg
end try
end run
on ensureSafariRunningAndActivated()
if not (application "Safari" is running) then tell application "Safari" to launch
tell application "Safari" to activate
my waitForProcess("Safari", 5.0)
end ensureSafariRunningAndActivated
on isPrivateByTitle(pLabel)
-- Method 2: window title contains "Private"
tell application "System Events"
if not (exists application process "Safari") then return false
tell application process "Safari"
if not (exists window 1) then return false
try
set wTitle to (title of window 1) as text
return wTitle contains pLabel
on error
return false
end try
end tell
end tell
end isPrivateByTitle
-- New Tab via menu (honors Start Page), with Command-T fallback
on openNewTabInFrontWindow()
tell application "System Events"
if not (exists application process "Safari") then return
tell application process "Safari"
try
click menu item "New Tab" of menu "File" of menu bar 1
on error
keystroke "t" using {command down}
end try
end tell
end tell
end openNewTabInFrontWindow
on openNewPrivateWindow()
tell application "System Events"
if not (exists application process "Safari") then return
tell application process "Safari"
try
click menu item "New Private Window" of menu "File" of menu bar 1
on error
keystroke "N" using {shift down, command down}
end try
end tell
end tell
-- Give Safari a moment to instantiate the new window
my waitForFrontWindow(3.0)
end openNewPrivateWindow
on waitForProcess(pName, timeoutSeconds)
tell application "System Events"
set t0 to (current date)
repeat
if exists application process pName then exit repeat
if ((current date) - t0) > timeoutSeconds then exit repeat
delay 0.1
end repeat
end tell
end waitForProcess
on waitForFrontWindow(timeoutSeconds)
set t0 to (current date)
repeat
tell application "Safari" to set wCount to (count of windows)
if wCount > 0 then exit repeat
if ((current date) - t0) > timeoutSeconds then exit repeat
delay 0.1
end repeat
end waitForFrontWindow
Macro Image:
Macro Download:
SAP- Safari - Private Window-Tab.kmmacros (6.6 KB)
Tested on: macOS Sequoia 15.6 with Safari 18.6 (set to "Safari opens with:" "A new private window" in Safari settings), US English
The script includes an attempt at error handling and fallback keyboard shortcuts if the menu items are inaccessible. It also tries to ensure Safari is running and activated before attempting any actions. Note much if not essentially all of this macro could be replicated via native actions. Also, your tab might not open at the end of the tab bar but next to an active tab, which could probably be worked around by adding a Command>9 prior to opening the new tab (or perhaps via AppleScript), or making Command>T the primary New Tab method instead of fallback, if desired.
Thank you, friendly macro people!