Skip to content

Instantly share code, notes, and snippets.

@limamedeiros
Last active August 29, 2015 14:10
Show Gist options
  • Save limamedeiros/ecdb439f79b1d19524f4 to your computer and use it in GitHub Desktop.
Save limamedeiros/ecdb439f79b1d19524f4 to your computer and use it in GitHub Desktop.
Opens Safari's current tab in a new private window and closes the tab. If a private window already exists, then opens the current tab in that window.
tell application "Safari"
activate
set theURL to the URL of the current tab of the front window
close current tab of front window
end tell
tell application "Safari"
activate
set theWindow to false
repeat with w in (every window whose visible is true)
set theMode to do JavaScript "try {localStorage.setItem('foo', 'bar');} catch(e) {modevar=4}" in current tab of w
if theMode = 4 then
tell application "Safari"
tell w
set theTab to make new tab
set the URL of theTab to theURL
set current tab to theTab
set index to 1
end tell
end tell
set theWindow to true
exit repeat
end if
end repeat
if not theWindow then
tell application "System Events"
tell process "Safari"
tell menu bar 1
click menu bar item "File"
tell menu "File"
click menu item "New Private Window"
tell application "Safari"
set URL of document 1 to theURL
set index of document 1 to 1
end tell
end tell
end tell
end tell
end tell
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment