Smart Safari Private Window/Tab Opener - Opens New Tab if Already Private

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!

Nice work! However, I'm pretty sure you can make this much simpler and skip all the AppleScript.

Here's my version that does just that...in my testing, I think it works in all your rule conditions. Note that it doesn't contain any of your error handling (as it's just a proof of concept), but it seems to work as you want it based on your rules.

The whole thing is handled by one If/Then loop that checks for the number of windows and words in the window title if there is one:

New Private Window Macro (v11.0.4)

New Private Window.kmmacros (31 KB)

Does that do what you need? If so, you could use this version after adding in your error handling, as it should be a bit quicker given it doesn't have to start an AppleScript engine.

-rob.

1 Like

Very nice, rob, thank you!

Particularly nice addition beyond the speed boost: checking the window name for , Private Browsing instead of the lazy Private (destined to fail on webpages titled "[...]private[...]". As long as I don't web search , Private Browsing (heh) I believe it'll be plenty robust enough for me.

Quick testing shows it again "works on my machine" with three simple additions: activate Safari, pause until Safari's at front, and pause until "New Private Window" is present in the menu. Perhaps I'll hit errors to handle at some point--will see!

And to make sure the new tab ends up far right, I swapped the menu selection for Command T. (I can end up with two empty tabs this way, will see if that bothers me later and practice a fix.)

Download the new native version (orig. courtesy Griffman) with the small modifications shown above:
SAP- Safari - Private WindowNATIVE (OPTION COMMA) - GriffmanMod.kmmacros (8.4 KB)

Thank you @griffman !

2 Likes

On my Mac, I have it in a group that's only active when Safari's active, but then obviously it can't be used globally, so yea, you'd need those steps.

Nice work putting it all together!

-rob.

1 Like