alvin alexander

AppleScript Safari URL tip: How to open multiple URLs in Safari tabs

Well, I set out to write a simple tip on how to activate a Mac application using AppleScript, and I ended up writing a program to open a list of URLs in separate tabs in Safari using AppleScript. (Yeesh, I really took a detour. Oh, well.)

Without any further ado, here's the source code for this AppleScript program:

Brief description of the code

Here's a brief description of what this AppleScript program does:

  • urlList is a simple list with the URLs I want Safari to open.
  • The repeat statement starts the loop.
  • I'm clicking File :: New Tab using the System Events because I can't figure out how to get this to work by calling Safari directly. (I'm currently using Safari 3.0.3.)
  • The keystroke command types the URL into Safari.
  • key code 36 is needed to hit the [Enter] key.

Again, I had to take a few extra steps here because I couldn't script the tabs in Safari directly. Ideally code like this would have worked inside the tell application "Safari" statement:

Hopefully that support will be available in the future, or, if I'm mistaken, please send me an email and I will correct this program.

Help keep this website running!

  • How to open a macOS Terminal in the current Finder folder
  • AppleScript tip: script a program that isn't scriptable
  • AppleScript tip: if/then syntax
  • Mac OS X Finder FAQ: How to open the Finder from a Terminal window
  • Mac AppleScript Finder: How to put the path of the current Finder window on the clipboard

books by alvin

  • ZIO ZLayer: A simple “Hello, world” example (dependency injection, services)
  • A dream vacation for the meditator in your life
  • Al's Oasis
  • Window of the Poet (painting)
  • Shinzen Young on meditating in his daily life: arising, disappearing, and The Source, and love

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

MartinLiss

Open URL in New Safari Tab

imac, Mac OS X (10.6.6), None

Posted on Mar 3, 2011 2:14 PM

Tony T1

Posted on Mar 3, 2011 2:31 PM

Also how to I format these posts so that my code looks like code,

Loading page content

Page content loaded

Mar 3, 2011 2:31 PM in response to MartinLiss

Mar 4, 2011 9:35 AM in response to MartinLiss

Mar 4, 2011 2:26 PM in response to MartinLiss

Mar 4, 2011 3:04 PM in response to Tony T1

Mar 4, 2011 4:56 PM in response to MartinLiss

MartinLiss wrote: Thanks but there's a problem in that your code always opens theURL in a new window.

Mar 4, 2011 9:46 PM in response to Tony T1

Mar 5, 2011 6:20 AM in response to MartinLiss

Mar 5, 2011 8:49 AM in response to Tony T1

Mar 5, 2011 9:22 AM in response to MartinLiss

Mar 5, 2011 9:31 AM in response to Tony T1

Mar 5, 2011 10:30 AM in response to MartinLiss

Mar 5, 2011 12:30 PM in response to Tony T1

Jul 24, 2011 1:21 PM in response to MartinLiss

Now that I've upgraded to Lion and its new version of Safari, this no longer works

set theURL to " https://www.airset.com/AirSet.jsp#gi.lkCezQTmARbJ+app.Calendar_v.month+t.today "

tell application "Safari"

tell application "System Events"

tell process "Safari"

-- System Preferences|Universal Access|'Enable access for assistive devices' must be selected for the following to work

click menu item "New Tab" of menu "File" of menu bar 1

set currentTab to current tab of window 1

set URL of document 1 to theURL

tell window 1 to set current tab to make new tab with properties { URL : theURL }

open location theURL

When Safari is already open It just gives me a new tab called 'Untitled' and the URL says 'Go to this address'. My first thought was that the Universal Access setting ahdn't been carried over and sure enough I had to set it again but it didn't help.

Jul 24, 2011 3:38 PM in response to MartinLiss

I'm embarrased. I didn't notice that the tell Application... code was commented out since it wasn't needed and I only kept it there for reference, so let me re-ask my question. Under Snow Lepeord the following gives me a new tab with the correct URL while under Lion I have the problem described above.

Jul 24, 2011 3:45 PM in response to MartinLiss

Problem solved.

tell window 1 to set current tab to make new tab --with properties {URL:theURL}

applescript

  • Getting started with applescript
  • AppleScript Browser Interactions
  • Applescript from the Terminal command line
  • Get the current URL in Safari or Google Chrome
  • Get the Title of the current page in Safari or Google Chrome
  • Use an Applescript as a shell function
  • AppleScript User Interaction
  • AppleScript Variable Types
  • Making Applescript If and Else Statements

applescript Applescript from the Terminal command line Get the current URL in Safari or Google Chrome

Fastest entity framework extensions.

From the Terminal command line

Get the current URL from Safari

Get the active URL in Google Chrome

Got any applescript Question?

pdf

  • Advertise with us
  • Cookie Policy
  • Privacy Policy

Get monthly updates about new articles, cheatsheets, and tricks.

set question to display dialog ("Find Safari tab:") default answer ""

set searchpat to text returned of question

tell application "Safari"

set winlist to every window

set winmatchlist to {}

set tabmatchlist to {}

set tabnamematchlist to {}

repeat with win in winlist

set ok to true

set tablist to every tab of win

on error errmsg

--display dialog name of win as string

set ok to false

repeat with t in tablist

if searchpat is in ( name of t as string ) then

set end of winmatchlist to win

set end of tabmatchlist to t

set end of tabnamematchlist to ( id of win as string ) & "." & ( index of t as string ) & ".   " & ( name of t as string )

--display dialog name of t as string

else if searchpat is in ( URL of t as string ) then

if ( count of tabmatchlist ) = 1 then

--display dialog "one!"

set w to item 1 of winmatchlist

set t to item 1 of tabmatchlist

set current tab of w to t

set index of w to 1

else if ( count of tabmatchlist ) = 0 then

display dialog "No matches"

set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:"

set AppleScript 's text item delimiters to "."

if whichtab is not equal to false then

set tmp to text items of ( whichtab as string )

set w to ( item 1 of tmp ) as integer

set t to ( item 2 of tmp ) as integer

set current tab of window id w to tab t of window id w

set index of window id w to 1

Using AppleScript to open a URL in Private Browsing in Safari

  • Posted 24 June 2020
  • Tagged with applescript , macos , macos:safari

I have a bunch of automations that open URLs.

If I want to open a regular window in Safari (my default browser), I have a variety of options – I can use open(1) on the command-line, or the webbrowser module in Python, or with AppleScript, or probably half a dozen other methods I haven’t thought of.

If I want to open a private browsing window, my options are more limited. The Safari AppleScript dictionary doesn’t know about private browsing, and all the other approaches I’ve seen are just passing Safari the URL to open. They can’t give it any instructions beyond “please open this URL”.

If you ask Google, there are lots of suggestions, but I struggled to find good code. Many of the results are broken, slow, or incomplete (they open the private browsing window, but not any URLs).

Rather than wade through more Google results, I came up with my own script for doing it, which opens the window and the URL:

I save this as a script in my $PATH and mark it as executable, and then I can open a new private browsing window from anywhere by running:

When you run this code, you may get an error (or a silent failure if you trigger it through a GUI rather than a terminal):

open_private_browsing:114:232: execution error: System Events got an error: osascript is not allowed assistive access. (-1719)

This is the macOS security system kicking in: it doesn’t let arbitrary scripts or applications click menu items in other applications. To allow this script to work, open the Security & Privacy preference pane. Under Accessibility , allow access to whatever application is triggering the script (in my case, iTerm 2), and it should be good.

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

How can I use AppleScript to open a new Safari window on the current desktop space?

I need to open a new Safari window in the current desktop space using AppleScript. NOT move to another space which has Safari running and then open another Safari window.

Of course, if Safari is not running then it should start a new Safari process in the current space.

  • applescript

Josiah Sprague's user avatar

You can put the following code into the apple script editor and save the file as application .

Now you can simply double click on the script and you will be able to open a new safari window inside of the current "active" space, even though you may have another safari window open in another desktop space. It will not switch.

kevin's user avatar

  • I have this script, but it opens the new window on the space where Safari is currently activated, not on my currently active desktop space. –  Josiah Sprague Mar 7, 2014 at 12:21
  • Actually, sometimes it opens two new Safari windows. One on my current desktop, and one on another desktop, which it then switches to. I can't figure out why more than one new document would be created. I am using an Alfred shortcut to call the script. –  Josiah Sprague Mar 7, 2014 at 12:26
  • Really? What version of OSX are you running? When I put Safari in my 4th desktop and I goto my 2nd Desktop with nothing opened up on it and run the above script I get a new Safari window on my 2nd Desktop. When I check back to my 4th Desktop it still has the same window I originally opened (no extra). I am running OSX 10.7.5. –  kevin Mar 7, 2014 at 18:23
  • I'm using 10.9.2. It could be a conflict with Alfred or TotalSpaces2 I suppose. I'll keep playing around with it. –  Josiah Sprague Mar 7, 2014 at 22:06
  • Hmm...it is certainly a possibly there may be 3rd party interference. If you are seeing 2 windows popup after running the script, a quick and dirty way to check if its coming from your script is add say "I am opening new window now" as the 2nd line. You'll be able to hear when your script is running and at least you tell if your script is for some reason running twice, hence the 2 windows popping up instead of one. –  kevin Mar 8, 2014 at 4:16

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged macos mac safari applescript ..

  • The Overflow Blog
  • Supporting the world’s most-used database engine through 2050
  • What language should beginning programmers choose?

Hot Network Questions

  • Do Starfleet officers retain the privileges of their rank if they time travel to other Starfleet vessels?
  • What features should a ceiling fan have so I can switch light and fan separately?
  • Where to provide a link to the repository in the distributed software?
  • Reference for a statement from Gaitsgory's thesis
  • “alignedat" will adjust the line space automatically but "array" won't in LaTeX?
  • A random urn problem - do the faster duplicating balls always dominate?
  • Does the formation of a neutron star always require a supernova explosion?
  • Quantum probabilistic method?
  • What security risks do you see with wrong OTPs appearing in application logs?
  • Was Deuteronomy 18:15, interpreted messianically in Judaism?
  • Can I increase my number of trained skills if I increase my Intelligence modifier?
  • Why does the BRK instruction set the B flag?
  • What does 'being conservative' mean in occlusion culling?
  • Laser finger guns
  • How can I make this AC current switch turn on at 20mA instead of 50mA?
  • Color for this sky
  • What is Tapscript?
  • Basis of the image
  • d orbitals PDOS analysis
  • Locally conformally flat
  • ES6 inner join equivalent
  • Does an upcast Banishment send the targets to the same place if they share a native plane?
  • Why is one process ps command showing spaces around a directory instead of slashes?
  • Are there any languages where conditions are not first-class expressions?

applescript safari open url in current tab

COMMENTS

  1. Open URL in new Safari tab with AppleScript

    tell application "Safari" try display dialog "Website URL" default answer "" buttons {"OK", "Annuler"} default button 1 set theURL to text returned of result set netProto to "https://" if theURL contains netProto then set the URL of (make new tab in window 1) to theURL else set the URL of (make new tab in window 1) to netProto & theURL end if ...

  2. How to open a specific url in the current Safari tab with AppleScript

    The current tab in Safari is current tab. Tabs are indexed according to their order in the tab bar, which means that tab 1 is not necessarily the current tab; it is the left-most tab (assuming that tabs are ordered left to right in your tab bar; I do not know, but this may vary based on localization). To open a URL in a tab in Safari, you use ...

  3. AppleScript to prompt for url input, open url in safari, then open

    You can change Open in New Tabs in click menu item "Open in New Tabs" of ¬ to the name of the target bookmark, or use the index value, e.g. 1 or whatever number it is in the list of the bookmarksFolder menu to have it click that bookmark. -

  4. AppleScript Safari URL tip: How to open multiple URLs in Safari tabs

    tell application "System Events". -- enter the url in the open window. keystroke (item 1 of urlList) key code 36. repeat with i from 2 to (numURLs) -- for each additional url, first create a tab. tell process "Safari". click menu item "New Tab" of menu "File" of menu bar 1. end tell.

  5. Applescript: Open current Safari tab in Chrome (Troubleshoot of working

    tell application "Safari". set theURL to URL of current tab of window 1. end tell. tell application "Google Chrome". if windows ≠ {} then. make new tab at the end of window 1 with properties {URL:theURL} else.

  6. AppleScript use Safari window ID to create new tabs

    The following example AppleScript code is an example of how to open a list of URLs in the same window of Safari, regardless of its window order. Notes: Do not use open location as it's a part of Standard Additions, not Safari. Use the URL property to set the URL of the document or tab. Use a list of URLs and the index of the list item (URL) in ...

  7. How do I open a generic URL from AppleScript?

    6. If myscheme is properly registered in the Launch Services Database to open a particular application, then you can use the open location command, which: "Opens a URL with the appropriate program." Use as in the following examples: set theURL to "myscheme://a/b/c". open location theURL.

  8. Open URL in New Safari Tab

    set theURL to "https://www.someURL" tell application "Safari" activate tell application "System Events" tell process "Safari" -- System Preferences|Universal Access|'Enable access for assistive devices' must be selected for the following to work. click menu item "New Tab" of menu "File" of menu bar 1 end tell end tell set currentTab to current ...

  9. Generate a List of Open Safari Tabs With AppleScript

    The syntax of these is nearly identical, only one grabs the name of the current tab and the other grabs the URL. I found the specific syntax for these commands in the Safari AppleScript dictionary. Notice that, to grab the name and URL of the current tab, we're simply using the x and y variables that increment each time the repeat block is run.

  10. applescript Tutorial => Get the current URL in Safari or Google Chrome

    From the Terminal command line. Get the current URL from Safari. osascript -e 'tell app "safari" to get the url of the current tab of window 1'. Get the active URL in Google Chrome. osascript -e 'tell app "google chrome" to get the url of the active tab of window 1'.

  11. How can I get Automator to open a URL in the current Safari tab?

    My current code involves a "Watch Me Do" block recording a ⌘ W keystroke and then opening the new URL, but that's kind of kludgy. Is there any way I can use Automator to open a URL within the current tab? AppleScript would also be acceptable. (Is this question too basic for Stack Overflow?)

  12. Open url in new tab and paste the copied text

    In my scenario, I highlight DOI address -> AppleScript will open "Sci-hub.tw" page in new tab on Safari -> paste DOI address to text box -> press Enter / Return. I found in many sites and tried to code. It here but my code just open url in new tab, not paste the highligh address. Please, help me! Thanks and best regards!

  13. Find Safari Tabs with AppleScript

    set AppleScript 's text item delimiters to "." To set this up, look in Applications, then Utilities, and run "AppleScript Editor". Cut and paste the program above into a new window there and save it as "Find Safari Tab" (or whatever name you like), and place it in your home directory, under Library/Scripts.

  14. Using AppleScript to open a URL in Private Browsing in Safari

    menu "File" of menu bar 1 of ¬. application process "Safari" end tell -- The frontmost window is the private Browsing window that just got -- opened -- change the URL to the one we want to open. tell window 1 to set properties of current tab to {URL:urlToOpen} end tell end openPrivateBrowsingWindow on run argv openPrivateBrowsingWindow(argv ...

  15. How can I use AppleScript to open a new Safari window on the current

    tell application "Safari" make new document activate end tell Now you can simply double click on the script and you will be able to open a new safari window inside of the current "active" space, even though you may have another safari window open in another desktop space. It will not switch.

  16. AppleScript : Safari : Open link in a new tab (Command+Click)

    This seems oddly specific, and there might be a better solution that's more general. - JMY1000. Jul 1, 2016 at 11:03. I do some investigation on some profile and need to open different links on different tab so I can have a overview of each data. - Kevin.

  17. AppleScript URL from open safari window

    URL is a property of a document or a property of a tab in a window, so use this: if URL of document 1 is "www.google.com" then. or this: if URL of current tab of window 1 is "www.google.com" then. Update, example of how to use the exists command: tell application "Safari". set b to exists URL of document 1 -- this put false or true into the ...

  18. Save the source of an open webpage from Safari with AppleScript

    These two entries from the AppleScript dictionary for Safari look useful: document n [see also Standard Suite] : A Safari document representing the active tab in a window. properties: source (text, r/o) : The HTML source of the web page currently loaded in the document. text (text, r/o) : The text of the web page currently loaded in the ...

  19. AppleScript: Open URL in specific tab (Google Chrome)

    I can no longer open a URL in a specific tab using AppleScript. I tried the following code without success: set URL of tab 1 to "https://" and . set myLink to "https://" tell application "Google Chrome" activate tell front window to make new tab 1 with properties {URL:myLink} end tell neither of which perform the desired action. Question