Jump to content

workflow to take current URL and open it in a new private safari browser window


Recommended Posts

I am trying to create a workflow to open URL from website that nags to login in when all I want is to read article which opens perfectly if in a private Safari window.

 

The three scripts below all work individually, but I have not been able to make them work together. 

I cobbled this together by searching for AppleScript to accomplish the task.

I am unable to get script 1 to pass its information to the next. Script 2 opens a private window, but if I add script 3 to it it doesn't execute the open URL portion.

I entered a hard coded set of TextURL in script 3 as proof of concept.

 

Thank you for helping. 

 

----------------------------------------------------------------

-- Script 1 Get the current URL

----------------------------------------------------------------

 

tell application "Safari" to return URL of front document

set textURL to text returned of the result

return textURL

 

 

----------------------------------------------------------------

-- Script 2 This opens a Private mode Safari Window

----------------------------------------------------------------

 

 

on is_running(appName)

tell application "System Events" to (name of processes) contains appName

end is_running

 

set isRunning to is_running("Safari")

 

if isRunning then

tell application "Safari" to activate

delay 0.5

tell application "System Events" to keystroke "n" using {shift down, command down}

else

tell application "Safari" to activate

delay 1

tell application "System Events"

keystroke "w" using command down

keystroke "n" using {shift down, command down}

end tell

end if

 

 

----------------------------------------------------------------

-- Script 3. This will cause Safari to open the textURL

----------------------------------------------------------------

 

-- I would like this next statement to use the variable from script 1 above,

-- but this lets me test the script

 

set textURL to "https://www.apple.com"

 

-- end of test variable setting

 

tell application "Safari"

tell window 1

tell current tab

set URL to textURL

end tell

end tell

end tell

 

 

 

 

Link to comment

Hi @ell1134, welcome to the forum.

 

1 hour ago, ell1134 said:

I am unable to get script 1 to pass its information to the next

 

If you put the script in a Run Script box, Alfred will automatically pass its output to the next script.

 

Doesn't matter, though. It's better to write the whole thing as one script. Try this:

 

-- Maximum time (in seconds) to wait for the new window to appear.
property _timeout : 2

tell application "Safari"
	activate
	set _url to URL of front document
	set _count to (count of windows)
	
	-- Open a new private window
	tell application "System Events" to keystroke "n" using {shift down, command down}
	
	-- Wait up to _timeout seconds for the new window to appear
	repeat (_timeout * 10) times
		delay 0.1
		if (count of windows) > _count then
			-- New window will be the front window (i.e. window 1)
			tell window 1 to set URL of current tab to _url
			return
		end if
	end repeat
	
	-- If we reach here, the script timed out waiting for the new window to appear
	display alert ¬
		¬
			"An error has occured" message ¬
		"Timed out waiting for new private window" as critical ¬
		buttons {"OK"}
end tell

 

Link to comment
  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...