Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I want to restart Safari and reopen it with some tabs that I specify.

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

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
share|improve this answer
    
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. =) –  Lizzan Nov 4 '10 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. :) –  Martín Marconcini Nov 4 '10 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. =) –  Lizzan Nov 4 '10 at 12:03
    
@Lizzan you’re right my mistake, I thought you were the OP ;) –  Martín Marconcini Nov 4 '10 at 12:04
add comment

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 ,.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.