An AppleScript to Duplicate the Current Tab in Safari

I use AppleScript to accomplish some simple but click intensive tasks on my Mac. For instance, an aspect of my MarsEdit workflow includes a script that grabs the frontmost URL in Safari and links to that page from my selected text in MarsEdit. I’ve set the script up, however, to also close the tab that the linked URL is contained in (I’m not going to explain why here, but if you’re interested, check out 2 AppleScripts to Simplify a MarsEdit Workflow). Often, though, I want to set up a link as follows:

Download VoodooPad from Flying Meat.

The two links above are different pages from the same base URL, and in order to load them both into my browser before entering the ‘insert post links’ segment of my workflow (again, see this post for details), I’d open them both up by executing the following string of keyboard shortcuts:

  • Cmd + L (highlight location)
  • Cmd + C (copy URL)
  • Cmd + T (make new tab)
  • Cmd + V (past URL)
  • Return (go to site)
  • Intensive, to say the least. Here is an AppleScript that will duplicate the current tab in Safari (which is what the combination of shortcuts listed above does, if you’re following along):

    tell application "Safari"
    	set sameURL to URL of document 1
    end tell
    
    tell application "System Events"
    	tell application "Safari" to activate
    	tell process "Safari"
    		click menu item "New Tab" of menu "File" of menu bar 1
    	end tell
    end tell
    
    tell application "Safari"
    	set URL of document 1 to sameURL
    end tell

    I couldn’t find a script like this one online, so I hope a few of you find it useful. Download the pre-compiled script if you don’t want to mess about with Script Editor.

    3 Responses to An AppleScript to Duplicate the Current Tab in Safari

    1. Here’s a non-UIScript script :) (which I think is Leopard only):

      tell application "Webkit"
      	tell window 1
      		set current tab to (make new tab ¬
      			with properties {URL:(get URL of (current tab))})
      	end tell
      end tell

      The hardest part was that you have to specify which window to open the tab in.

      Via: http://bbs.macscripter.net/viewtopic.php?pid=100448

    2. Oh, and I forgot to mention, you could just use Cmd-L, Cmd-Enter to achieve the same effect. :)

    3. Thanks MacTipper!

      The non-GUI version is, of course, faster than mine, which in the world of AppleScript also makes it better.

      Here is your script again for Safari users:

      tell application "Safari"
      	tell window 1
      		set current tab to (make new tab ¬
      			with properties {URL:(get URL of (current tab))})
      	end tell
      end tell

      Rock on.

    Think different?