Can Safari automatically trigger a menu item using scripts?

I simply want to be able to click on the Safari icon and have Safari (3.1) open and automatically execute the menu item (History/Reopen all windows from last session), so I have firefox session saver functionality. I made an automator workflow that when run opens Safari and runs a “watch me” action that clicks the menu item for me. My solution is slow and clunky but works, there must be a better way.

I don’t have any scripting experience so I thought I’d come here for some help and advice from the pro’s.

Thanks,
Eric

The following is working on 10.4.11 with Safari 3.1.1.

You won’t be able to click Safari’s icon to run it, but you could save it as an application and give it a unique Dock icon instead… if that’s what you want.

Otherwise call it from the script menu, or assign it to a key combo with a third party app.

(It occurs that - being a menu item - you may be able to simply set a key combo in the Keyboard & Mouse pref pane.)

Peter B.



tell application "Safari" to activate

tell application "System Events"
	tell process "Safari"
		click menu item 10 of menu 1 of menu bar item "History" of menu bar 1
	end tell
end tell

Peter.

That did it, thanks so much. I saved it as an application and gave it a safari icon and it’s sitting in my dock.

A lot of people I know don’t use safari for just this reason, no automatic session restore. I always end the day with 20+ tabs open and need to get

right back to it in the morning, this will let me do that with safari.

Well done!

Eric

Hello

Just a detail: the posted script works only for Safari used in English.

This one:

tell application "Safari" to activate

tell application "System Events"
	tell process "Safari"
		click menu item 10 of menu 1 of menu bar item 6 of menu bar 1
	end tell
end tell

may be used worldwide :wink:

Yvan KOENIG (from FRANCE jeudi 15 mai 2008 21:42:47)

Can someone please update this script for Safari 4.0.

Thanks

Eric

I haven’t installed Safari 4, but you can probably do it yourself, Eric.

menu bar item 6 is the 6th menu (including -Menu) of Safari
menu item 10 is the 10th menu item (including separators) of the History menu

Thanks Stefan!

That was the hint I needed. While I was waiting for help I found a plugin that gives me this function and much more.
If anyone else needs to open (last session) tabs when starting Safari try Glims from Machangout.com.

Eric