Jump to content

Open a new safari window


Recommended Posts

  • 1 year later...

Cheers jdr,

 

thanks for sharing this, I've used it so often!

 

(I configured my Safari to open links in new tabs, which is my preferred way of browsing the web when I'm in a window of the current context anyways. But I was always annoyed when Alfred queries send me to a different desktop space and/or maximizes a totally unrelated Safari window full of tabs).

 

As I was missing a couple of features, I took your workflow and extended the script a little bit, so it accepts a parameter which can be:

- empty (just opens a new window)

- a search string (opens google, but you can change this in the script)

- an URL (a string containing one of these substrings: http:, www., .com, .edu, .net, .org... )

 

Update Aug 2016:

- prepends "http://" if missing and some of the TLDs are detected (.com, .edu, .net, .org, .de, ... )

 

Hope someone likes it, I'm using it really often.

You might want to change the trigger key though, it's "safari"

 

http://www.audiocommander.de/.../Safari%20Window.alfredworkflow

 

 

(or just the Apple Script part)

 

on alfred_script(q)
  tell application "Safari"
    if "http:" is in q or "www." is in q or ".com" is in q or ".de" is in q or ".edu" is in q or ".net" is in q or ".org" is in q then
      if q starts with "http://" or q starts with "https://" then
        make new document with properties {URL:q}
      else
        make new document with properties {URL:"http://"&q}
      end if
    else
      make new document with properties {URL:"http://www.google.com/search?q="&q}
    end if
    activate
  end tell
end alfred_script
Edited by auco
Link to post
  • 7 months later...

here is my solution with little tweak.

after download dropbox link, import  onto Alfred workflow.

 

here is tweaks.

 

  • via Alfred
  • pop up new safari on current workspace, and make it front
  • (optional) if needed, open specific URL //currently this script valid for just 1 URL address.
  • (optional) in case of specific URL, no need of “http://”
Link to post
  • 6 months later...

 

here is my solution with little tweak.

after download dropbox link, import  onto Alfred workflow.

 

here is tweaks.

 

  • via Alfred
  • pop up new safari on current workspace, and make it front
  • (optional) if needed, open specific URL //currently this script valid for just 1 URL address.
  • (optional) in case of specific URL, no need of “http://”

 

 

This link no longer seems to work :(

Link to post

this one still works...

 

As I was missing a couple of features, I took your workflow and extended the script a little bit, so it accepts a parameter which can be:

- empty (just opens a new window)

- a search string (opens google, but you can change this in the script)

- an URL (a string containing one of these substrings: http:, www., .com, .edu, .net, .org... )

 

Hope someone likes it, I'm using it really often.

You might want to change the trigger key though, it's "safari"

Link to post
  • 2 weeks later...

Here's a small update, adding "http://" to the string, if a TLD ending is detected

 

 

(I configured my Safari to open links in new tabs, which is my preferred way of browsing the web when I'm in a window of the current context anyways. But I was always annoyed when Alfred queries send me to a different desktop space and/or maximizes a totally unrelated Safari window full of tabs).

 

As I was missing a couple of features, I took your workflow and extended the script a little bit, so it accepts a parameter which can be:

- empty (just opens a new window)

- a search string (opens google, but you can change this in the script)

- an URL (a string containing one of these substrings: http:, www., .com, .edu, .net, .org... )

 

Update Aug 2016:

- prepends "http://" if missing and some of the TLDs are detected (.com, .edu, .net, .org, .de, ... )

 

Hope someone likes it, I'm using it really often.

You might want to change the trigger key though, it's "safari"

 

http://www.audiocommander.de/.../Safari%20Window.alfredworkflow

 

 

(or just the Apple Script part)

 

on alfred_script(q)
  tell application "Safari"
    if "http:" is in q or "www." is in q or ".com" is in q or ".de" is in q or ".edu" is in q or ".net" is in q or ".org" is in q then
      if q starts with "http://" or q starts with "https://" then
        make new document with properties {URL:q}
      else
        make new document with properties {URL:"http://"&q}
      end if
    else
      make new document with properties {URL:"http://www.google.com/search?q="&q}
    end if
    activate
  end tell
end alfred_script

 

Link to post
  • 1 year later...
On 8/17/2016 at 2:13 AM, auco said:

Here's a small update, adding "http://" to the string, if a TLD ending is detected

 

 

Hi! Is it possible to use this extension to open multiple tabs in one new window? If so, what would I need to change in the AppleScript? Thanks!

 
Link to post

Yes that seems to be possible -

you have to adapt the apple script: https://stackoverflow.com/questions/11706171/how-to-open-a-new-window-and-multiple-urls-in-safari-with-apple-script

 

You can use "Script Editor.app" (to be found in /Applications/Utilities) to test the script and then just update the script in the workflow

Link to post

Thanks! I used that site to get a script working in Script Editor (first screenshot), but when I try to run it in Alfred, I get the error in the second attached screenshot. Any idea how to get it to work within Alfred?

 

Sorry they are screenshots. I'm not sure how to share code snippets on Alfred forums like you did. 

 

Screen Shot 2018-06-11 at 10.21.18 AM.png

Screen Shot 2018-06-11 at 10.20.35 AM.png

Link to post

Hey Chamend!

 

I'm not an Apple Script expert and I haven't tested your script, but I think the "end run" line looks fishy.

 

usually when you start with "on something", you'll end with "end something".
In your case you have "on alfred_script" and this is balanced incorrectly in the middle with "end run" (not sure where this is from?!) - there should be an "on run", but I don't see where that comes from, as it's neither in the original script, nor in the stackoverflow examples. Might be wrong on this one, but a quick google search revealed nothing Apple Script related with "on run" or "end run".

..."on new_tab" and "end new_tab" are balanced, as is "end alfred_script". See the indentations? They aren't required but they should reflect the on/end pairs.
 

second - it might also be that you have an invisible character somewhere that is causing this issue. Try to copy and paste the text in a plain text editor (not a rich text editor)

 

If you copy and paste the code, I might be able to test it, but I don't have the time to retype

 

Cheers!

 

1 hour ago, Chamend said:

Screen Shot 2018-06-11 at 10.21.18 AM.png

 

Link to post

I got this to work! Example code below. Swap in the URLs of your choice.

 

When this code runs, the tabs open one after the other. So, after the code runs, the active tab is the Stack Overflow one, with Google and Apple in tabs in the background. 

 

This is really handy! Thanks auco for your help.

on alfred_script(q)
	tell application "Safari"
		activate
		make new document with properties {URL:"https://www.apple.com"}
		my new_tab()
		set the URL of document 1 to "https://www.google.com"
		my new_tab()
		set the URL of document 1 to "https://stackoverflow.com"
	end tell
	end run
	on new_tab()
	tell application "Safari" to activate
	tell application "System Events"
		tell process "Safari"
			click menu item "New Tab" of menu "File" of menu bar 1
		end tell
	end tell
	end new_tab

 

Link to post

Hi @Chamend

 

This slight adjustment to the script negates the need to use System Events to issue menu clicks (which is an unreliable method):

 

on new_tab(www)

        tell application "Safari" to tell the front window to ¬

                make new tab with properties { URL:www }

end new_tab

 

Then you call the handler, specifying the URL at the same time, e.g.:

 

        my new_tab("https://stackoverflow.com")

Edited by CJK
Formatting adjustments
Link to post
16 hours ago, CJK said:

Hi @Chamend

 

This slight adjustment to the script negates the need to use System Events to issue menu clicks (which is an unreliable method):

 

on new_tab(www)

        tell application "Safari" to tell the front window to ¬

                make new tab with properties { URL:www }

end new_tab

 

Then you call the handler, specifying the URL at the same time, e.g.:

 

        my new_tab("https://stackoverflow.com")

 

Thanks @CJK! Full disclosure -- the script I shared is the first code I have ever written so I'm a complete newbie. Would you be able to give me some more detail about how to plug this adjustment into the script I wrote? 

Link to post

Basically, your script from this post reply here:

if you incorporate my suggested modifications, would now look like this:

on alfred_script(q)
	tell application "Safari"
		activate
		make new document with properties {URL:"https://www.apple.com"}
		my new_tab("https://www.google.com")
		my new_tab("https://stackoverflow.com")
	end tell
end alfred_script


on new_tab(www)
	tell application "Safari" to tell the front window to ¬
		set the current tab to make new tab ¬
			with properties {URL:www}
end new_tab

 

 

Link to post
3 hours ago, Chamend said:

Thanks @CJK! That works great.

 

You're welcome, @Chamend.  On the surface, it functions identically to yours.  But under the hood, it targets the Safari application and its window(s)/tab(s) directly, so it won't even matter whether or not Safari is in focus, or even if the window were minimised (provided it is still the front window).

 

One of the reason using System Events in that manner is (should) be a last resort (but is done all too commonly to appear to be "the right way"), is because menu items are generally referenced by name, and these can change (by language and region), so is especially less portable if you plan to share workflows with others.  Keypresses to activate a shortcut is worse still, as these are customisable by design on macOS.  Both also require  Safari to be in focus, and application focus can shift unexpectedly, which would disrupt the script.

 

Just thought it might be useful for you to know all this if you're planning on venturing more into the AppleScripting world (which I encourage!).  I actually drafted out a much more complex script to open tabs in any specific Safari window or a new window, and provide the option of deciding whether to open it in the background or bring it to the front.  It benefited from being more robust and feature-worthy, but seemed a bit overly involved in this situation.  Nevertheless, if you want to take a look at it, I've posted it here for the time-being.

Edited by CJK
Link to post

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...