I want to restart Safari and reopen it with some tabs that I specify.
2 Answers
An alternative method is to use Safari's built-in "Reopen All Windows from Last Session" feature:
tell application "Safari"
quit
end tell
delay 2 -- Wait for Safari to close
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "Reopen All Windows From Last Session" of menu "History" of menu bar 1
end tell
end tell
-
This one's much smoother for just re-opening tabs/windows than the scripts I found (figures, they were old!). +1! But just quitting and re-opening the same tabs again might not be so useful...skipping the first 5 lines would make it a great way to start Safari, though. =)– LizzanNov 4, 2010 at 6:32
-
1@Lizzan well, you said you wanted to “restart safari and reopen it”, so Chealion assumed you wanted to close it and reopen it. Now if your safari is closed, then of course, the close part is unneeded. :) Nov 4, 2010 at 11:18
-
@Martín Marconcini Well, I didn't say that, the OP did, hence why I was unsure if the OP wanted to re-open the open tabs or quit and open a certain set of tabs. =)– LizzanNov 4, 2010 at 12:03
-
@Lizzan you’re right my mistake, I thought you were the OP ;) Nov 4, 2010 at 12:04
-
To save windows/tabs from a session and be able to reopen them, there's a solution here: http://hints.macworld.com/article.php?story=20030913153245341
Be sure to read the comments, apparently there have been several updates to the scripts. Edit: Just use Chealion's answer, much smoother.
If you just want to open a certain set of tabs, you can use this script:
tell application "Safari"
set urllist to {"http://google.com", "http://stackoverflow.com", "http://apple.stackexchange.com"}
repeat with i from 1 to number of items in urllist
set URL of document 1 to item i of urllist
if i is less than number of items in urllist then
my new_tab()
end if
end repeat
end tell
on 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 new_tab
Just list the addresses of the pages you want to open between the {}
on line 2. Each address within ""
and separated by ,
.