An AppleScript to launch Safari and open a given site

Jul 23, '04 11:11:00AM

Contributed by: Anonymous

I have been plagued with a bug, for some time and for many verisons of Safari, that for one reason or another insists on resetting my Home Page to the "factory default settings" every so often. Just often enough to be really annoying and have me using Camino most of the time. That said, I really like Safari and this little bug has always bothered me, so I finally decided to do something about it: enter Safari Launcher. A simple AppleScript (my first ever) designed to (1) launch Safari and immediately go to a particular page (defined within the script), and (2) in the event Safari is already open when the script is executed, bring it (Safari) to the fore front and open a new browser tab.

Read the rest of the hint for the script...

Safari Launcher v0.05

The purpose of this code is to launch the Safari web browser, while overriding the defined Home Page. This is particularly useful due to an ongoing bug within Safari that causes it to periodically reset the user defined settings to the factory defaults. This behavior adversely effects the Home Page setting and is very irritating, hence the need to code this script. The code and logic within this sciprt has been provided "AS IS" and the responsibility for its operation is entirely your own.

tell application "Safari"
  if not (exists (document 1)) then
    --
    -- Safari is *Not* Opened, Call Open New Window Function, Go To Preferred Page
    -- and Activate Safari to Enable Scripting
    --
    tell application "Safari" to activate
    my open_new_window()
    --
    -- Set Safari Location to Preferred Page
    --
    set the URL of document 1 to "http://localhost/"
  else
    --
    -- Safari is Opened, Bring it To Front and Open New Tab w/Preferred Page
    --
    if (exists (URL of document 1)) then
      my open_new_tab()
    end if
    --
    -- and Activate Safari to Enable Scripting
    --
    tell application "Safari" to activate
    --
    -- Set Safari Location to Preferred Page
    --
    set the URL of document 1 to "http://localhost/"
  end if
end tell

--
-- Open New Window Function
--
on open_new_window()
  tell application "Safari" to activate
  tell application "System Events"
    tell process "Safari"
      click menu item "New Window" of menu "File" of menu bar 1
    end tell
  end tell
end open_new_window

--
-- Open New Tab Function
--
on open_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 open_new_tab
I am sure many of you can find ways to do the same thing better (as mentioned, this is my first script), but this does do the trick. As you can see from the code above, simply alter the lines with "http://localhost/" to suit your own needs, save your changes and you're set. This can be run as a script or saved/run as an application (my own preference.) For easy access, I've added mine to the Dock.

[robg adds: I've never had an issue with Safari changing my homepage, so I suspect something else is at work here. Nonetheless, perhaps this script will be useful to someone.]

Comments (16)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040720011307587