Script to set Safari home page?

I am looking for an Applescript that can set the home page of Safari. Does anyone know if this is possible?

I am also looking for a way to add URLs to the bookmarks - anyone know how to do this?

Cheers,
Bill

Here is a partially working script to set the home page and then open the home page. Unfortunately, I haven’t figured out how to correctly get the variable “HomePage” data into the text field in Preferences. So if you know how, I would appreciate the comments.


set HomePage to “http://www.somewhere.net

tell application “Safari”
activate
end tell

tell application “System Events”
tell application process “Safari”
tell menu bar 1
tell menu bar item “Safari”
tell menu “Safari”
tell menu item “Preferences”
tell window “General”
tell pop up button 2
tell menu item “Home Page”
–seems to work ok up to here - need text input code
tell group 1
set value of text field to HomePage

                                                                    end tell
                                                            end tell
                                                    end tell
                                            end tell
                                    end tell
                            end tell
                           
                            tell application "Safari"
                            open location "http://www.somewhere.net"
                            end tell
                           
                    end tell
            end tell
    end tell

end tell

Hi, Bill.

Here’s a script I think does what you want. I’ve used the indices of UI elements rather than their names, which should allow the script to work with anyone’s language settings. There are various ways to get Safari to go to the given home page. The most reliable in the current context seems to be to use GUI scripting to click “Home” in the “History” menu.

set HomePage to "http://www.somewhere.com"

tell application "Safari" to activate

tell application "System Events"
	tell application process "Safari"
		-- Make sure Safari's the frontmost process.
		set frontmost to true
		-- Click "Preferences." in the "Safari" menu.
		click (third menu item of menu 1 of menu bar item 2 of menu bar 1 whose title is not "")
		tell window 1 -- the Preferences window.
			-- Click the "General" button to get that panel.
			click button 1 of tool bar 1
			-- Set the value of the (only) text field to the required home page URL.
			set value of text field 1 of group 1 of group 1 to HomePage
			-- Close the Preferences window.
			click button 1
		end tell
		-- Click "Home" in the "History" menu.
		click (third menu item of menu 1 of menu bar item 6 of menu bar 1 whose title is not "")
	end tell
end tell

Thanks Nigel - works well.

Do you know how to create bookmarks in Safari using Applescript as well?

Cheers,
Bill

Hi, Bill.

You need to be more specific. Do you want to bookmark the current URL? If so, where? Do you want to create a bookmark from scratch? If so, you’ve defeated me. :wink:

This stores a bookmark for the current URL, with the current page name, in a pre-existing bookmark folder called “Added Bookmarks”:

tell application "Safari" to activate

tell application "System Events"
	tell application process "Safari"
		set frontmost to true
		-- Click "Add Bookmark." in the "Bookmarks" menu.
		click (2nd menu item of menu 1 of menu bar item 7 of menu bar 1 whose title is not "")
		-- Click "Added Bookmarks"" in the revealed pop-up menu.
		tell sheet 1 of front window
			click pop up button 1
			click menu item "Added Bookmarks" of menu 1 of pop up button 1
			-- Wait until the pop-up menu closes.
			repeat while (menu 1 of pop up button 1 exists)
			end repeat
			-- Click the "Add" button.
			click button 1
		end tell
	end tell
end tell

Hi Nigel,

Actually, I want to create 2 bookmarks in Safari from scratch. I already use my script to create 2 URLs in a folder in Applications, then create 3 alias’s for the URLs on the desktop, but was hoping to also add bookmarks for these URLs in Safari. Anyway, I will eventually figure it out.

Hey - one question about the script you sent me to create the home page. It works in 10.4.x, but not in 10.3.x. It fails on:

click button 1 of tool bar 1
– Set the value of the (only) text field to the required home page URL.
set value of text field 1 of group 1 of group 1 to HomePage

It doesn’t like the syntax for the UI elements in OS X 10.3.x. I have my UI element inspector out and am trying to resolve it, but no luck yet. I need my script to work for 10.2.3 - 10.4.x.

I do a version check in the beginning of the overall script, so I have created subroutines for 10.2, 10.3, and 10.4. I can execute different code for each version from the same script. I may have to go back to using “tell” statements for 10.3 and 10.2.

I have noticed some change in the GUI UI elements from 10.3 to 10.4. Tricky to figure out sometimes.

Bill

Oh, let’s do it the easy way:


set homePage to "http://bbs.applescript.net"
do shell script "defaults write com.apple.internetconfigpriv WWWHomePage" & space & homePage

Of course, the change won’t take effect until Safari is launched again.

Enjoy

Johnny

Hi, Bill. I’ve never used 10.3 myself, so I don’t know. Presumably, there’s some term in the script that wasn’t implemented in 10.3’s version of System Events or else the UI geography of 10.3’s Safari is different. GUI scripting wasn’t possible at all in 10.2 unless you downloaded and installed a special beta version of System Events.

I haven’t had time today to try Johnny’s suggestion in OS 10.2 or 10.4, but it might be the way to go for the home page script…

Hey - thanks to Johnnylundy and Nigel for your excellent help.

I will check the Apple website for more info on the really usefull “defaults” command.

Cheers,
Bill