Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

An AppleScript to launch Safari and open a given site Apps
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.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[15,523 views]  

An AppleScript to launch Safari and open a given site | 16 comments | Create New Account
Click here to return to the 'An AppleScript to launch Safari and open a given site' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to launch Safari and open a given site
Authored by: geek-e on Jul 23, '04 11:45:07AM

Couldn't you avoid the applescript all together and just create a URL file with the site you want to visit? Go to the site, click on the Favicon (to the left of the URL) and drag it to the desktop or wherever.
You could even go as far as changing the icon of hte URL file to that of Safari and put it in the Dock. Click that instead of the Safari.app icon and you'll go to your site every time.



[ Reply to This | # ]
Depends on where you want to put it. . . .
Authored by: BulbVivid on Jul 23, '04 01:41:28PM

You can save the script as an app, change the icon, and put it with the other apps in the dock. The URL clip has to go on the other side of the divider.

So, if you like to keep specific things, or nothing except minimized windows (like me) on that side of the dock, this script would be pretty handy.



[ Reply to This | # ]
An AppleScript to launch Safari and open a given site
Authored by: mm2270 on Jul 23, '04 02:50:01PM

What bug is this? Forgive me for saying so, but it sounds like maybe you're having some issue with your Mac that's not related specifically to Safari. I've ben using Safari almost exclusively since it was released by Apple, and I've never had it reset to defaults. Can anyone else verify this bug claim?
I'm not saying it's the hint author's imagination or something, but I'm very surprised to hear about this bug. I've never ever run into it.



[ Reply to This | # ]
An AppleScript to launch Safari and open a given site
Authored by: bobw on Jul 23, '04 02:59:39PM

Script doesn't work for me. Get this error;

System Events got an error: NSReceiverEvaluationScriptError: 4

and this line in the script gets highlighted;

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



[ Reply to This | # ]
An AppleScript to launch Safari and open a given site
Authored by: geek-e on Jul 23, '04 03:24:35PM

Do you have Tabbed browsing enabled in Safari?



[ Reply to This | # ]
Assistive Devices
Authored by: RickoKid on Jul 25, '04 06:44:43PM

You need to have "Enable access for assistive devices" checked in the "Universal Access" pane in System Preferences.



[ Reply to This | # ]
I've most definitely had this same problem
Authored by: Brian Purnell on Jul 23, '04 04:41:46PM

I've most definitely had this same problem, albeit intermittently. I've wondered if it had something to do with Cocktail or something else reseting things. I brought my PB to the Genius Bar and they couldn't figure it out. One trick I use when it does happen is start Safari, make the changes in the settings, quit Safari and restart the machine. It seems to hold after that. I've had it occur at least three times. It hasn't happened recently so I don't know if it was me or Safari. If you reset Safari, it will definitely happen.



[ Reply to This | # ]
settings returned to default
Authored by: fixnmacs on Jul 25, '04 05:33:04AM

Any chance you've got FileVault enabled?



[ Reply to This | # ]
An AppleScript to launch Safari and open a given site
Authored by: Saint on Jul 23, '04 04:43:02PM

do any of your other setting change with the homepage reset?
i have a problem with iPhoto here on my laptop, that has never appeared on any other machine. every time i open iphoto, my pictures refuse to be organized with the most recent at top. i opened it right now, and the option was not clicked, so i clicked it and they organized. i also switched the default rotation setting, quit and reopened. pictures were not organized properly, but button was still clicked, and rotate was still the same. iPhoto thinks it knows what is going on, but apparently does not. luckily, this is my biggest problem.
this just sounded slightly similar, no real help or anything



[ Reply to This | # ]
same problem...
Authored by: ducasi on Jul 23, '04 05:46:59PM

One of my users has the same problem, though I thought it was Internet Explorer's fault as the home page seems to revert to the one it uses as a default. (This is on 10.2.)

Lots of messing around with the preferences files and such has failed to fix it.

As for the script given in the hint, what does it do that a simple "open URL" from the command line (which can be embedded within an AppleScript) doesn't?

[ Reply to This | # ]

Non-GUI approach (shorter too)
Authored by: christianboyce on Jul 23, '04 10:14:00PM
The "click menu item" steps in your script require the user to have GUI scripting turned on (System Preferences, Universal Access, Enable access for assistive devices). That's not the way Macs are set up out of the box. Better not to rely on the user's machine being set up a certain way. Generally speaking, try to avoid GUI scripting except as a last resort.

Here is a general script that opens Safari windows and fills them in with various sites, taken from a list. I like the sites being in their own windows because I occasionally make the mistake of clicking a close button in a Safari window that has tabs, closing who knows how many tabbed sites with it.

It's nice to be able to resize the windows via JavaScript-- very fast. Of course, this depends on JavaScript being available, but this is the default for Safari.


tell application "Safari"
	--add your sites to this list, in quotes
	set thelist to {"http://www.apple.com", "http://www.christianboyce.com", "http://www.macosxhints.com"}
	--no matter how many sites you've listed, the repeat will work properly
	repeat with thesite in thelist
		set y to make new document at beginning
		set the URL of the front document to thesite
	end repeat
	--if you're in the mood for a little javascript you can resize and reposition each window to full screen in a hurry
	repeat with i from 1 to the count of documents
		do JavaScript "moveTo(0,0);self.resizeTo(screen.availWidth,screen.availHeight)" in document i
	end repeat
	--javascript courtesy of Apple's website, www.apple.com/applescript
end tell


[ Reply to This | # ]
Curious
Authored by: MJCube on Jul 23, '04 11:33:33PM

Just to chime in: In 10 years of using many different web browsers under various Systems, I have never had one forget my preferred home page, which is a local html file on my HD. That problem should be traceable.



[ Reply to This | # ]
What about...
Authored by: reg on Jul 26, '04 02:13:33AM

... the following (on the commandline):

open http://www.macosxhints.com/

Wrap this in a script, and you have a significantly shorter
version of the hint...



[ Reply to This | # ]
Curious
Authored by: jdtangney on Jul 26, '04 08:14:21PM

I saw this all the time - drove me nuts.

When I installed Panther, I did a clean install on a new disk, and since then Safari remembers its home. My Jaguar installation had been built by successive installations of upgrades, dating all the way back to the Beta of X. I was not sure if Jaguar fixed the problem or the clean install did.



[ Reply to This | # ]
A simpler version...
Authored by: reg on Jul 26, '04 02:18:41AM

you should be able to do this with the following piece of code as well:

shell "open http://www.macosxhints.com/"



[ Reply to This | # ]
An AppleScript to launch a site in Safari at a given time?
Authored by: partylikechewie on Jul 26, '04 03:00:46PM

Does anyone have a script that will open a specific site in Safari at a given time (ideally it would be able to do this if Safari is already open, or if it is not running)?

Thanks.



[ Reply to This | # ]