j_d_robinson Posted July 30, 2013 Share Posted July 30, 2013 Hello! This is a pretty basic workflow that I just use for opening a new Safari window. My most used v1 extension but only just made it into a v2 workflow. http://jdr.name/projects/alfred/New Safari.alfredworkflow It's basically just an AppleScript Link to comment
auco Posted June 11, 2015 Share Posted June 11, 2015 (edited) 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 August 17, 2016 by auco Link to comment
maxyun Posted February 2, 2016 Share Posted February 2, 2016 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 comment
justcharlie Posted August 2, 2016 Share Posted August 2, 2016 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 comment
auco Posted August 3, 2016 Share Posted August 3, 2016 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 comment
auco Posted August 17, 2016 Share Posted August 17, 2016 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 comment
jaypeters Posted June 10, 2018 Share Posted June 10, 2018 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 comment
auco Posted June 11, 2018 Share Posted June 11, 2018 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 comment
jaypeters Posted June 11, 2018 Share Posted June 11, 2018 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. Link to comment
auco Posted June 11, 2018 Share Posted June 11, 2018 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: Link to comment
jaypeters Posted June 11, 2018 Share Posted June 11, 2018 Thanks! Let me play around with your suggestions to see if I can make it work. Thanks so much for your help so far, I really appreciate it. Link to comment
jaypeters Posted June 14, 2018 Share Posted June 14, 2018 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 comment
CJK Posted June 14, 2018 Share Posted June 14, 2018 (edited) 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 June 14, 2018 by CJK Formatting adjustments Link to comment
jaypeters Posted June 15, 2018 Share Posted June 15, 2018 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 comment
CJK Posted June 15, 2018 Share Posted June 15, 2018 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 deanishe 1 Link to comment
jaypeters Posted June 16, 2018 Share Posted June 16, 2018 Thanks @CJK! That works great. Link to comment
CJK Posted June 17, 2018 Share Posted June 17, 2018 (edited) 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 June 17, 2018 by CJK deanishe 1 Link to comment
jaypeters Posted June 17, 2018 Share Posted June 17, 2018 Thanks for the info and the additional script, @CJK! Link to comment
jackk Posted August 25, 2022 Share Posted August 25, 2022 (edited) I'm not sure what I'm doing wrong here, can someone look at this? Just trying to open new links in new tab instead of new window. if safari isn't open already. 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_tab with properties {URL:"http://"&q} end if else make new_tab with properties {URL:"http://www.google.com/search?q="&q} end if activate 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:q} end new_tab Edited August 25, 2022 by jackk Link to comment
vitor Posted August 25, 2022 Share Posted August 25, 2022 4 hours ago, jackk said: Just trying to open new links in new tab instead of new window. if safari isn't open already. That’s Safari’s default behaviour. Use an Open URL Action. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now