Repeat AppleScript if User Confirms a Pop-Up Dialog I built a script that selects and copies all text from the front Safari tab, closes the tab, and pastes the text into the front BBEdit document with a couple blank lines of padding.

I'll run it when I have several tabs I want to grab text from, dumping it all into one document, so I'll need a repeat option. 

I don't want to hard-code the number of repeats, because that's unpredictable (and I don't want to close Safari tabs willy-nilly). So I want the script to complete once, pop up a choice of "Repeat?" or "Done!", and then repeat, including the pop-up, until the user chooses "Done".

I don't know how to code the repeat. Here's where I'm at:

tell application "Safari" to set the clipboard to (text of current tab of front window) as string
delay 0.1

tell application "Safari"
    close current tab of front window without saving
end tell
delay 0.1

tell application "System Events"
    tell application "BBEdit" to activate
    key code 36
    key code 36

end tell

tell application "BBEdit"
    activate
    paste
end tell

tell application "System Events"
    tell application "BBEdit" to activate
    key code 36
    key code 36

end tell

set theAlertText to "Repeat?"
display alert theAlertText as critical buttons {"Yes", "No"} default button "Yes" cancel button "No"
--&gt; Result: {button returned:"Continue"}

--need repeat code here
First, you're making your life more difficult by using the clipboard and GUI scripting. BBEdit has a full scripting dictionary designed for working with text; I've modified your code to use it.

All you need is a repeat loop, and then a conditional exit repeat, as follows:

-- first find or create a text doc called 'Transcription.txt,' and select it
tell application "BBEdit"
    set target_doc_list to every document whose name is "Transcription.txt"
    if (count of target_doc_list) is 0 then
        set target_doc to make new text document with properties {name:"Transcription.txt"}
    else
        set target_doc to item 1 of target_doc_list
    end if
    select target_doc
end tell

-- repeat loop to run through Safari pages
repeat
    tell application "Safari"
        set page_title to (name of current tab of front window)
        set page_text to (text of current tab of front window)
        close current tab of front window without saving
    end tell
    tell application "BBEdit"
        tell window 1
            set end of contents of first text document to return &amp; return &amp; page_text &amp; return &amp; return
        end tell
    end tell

    tell application "Safari"
        set next_page_title to (name of current tab of front window)
    end tell

    set theAlertText to "Finished with page " &amp; page_title &amp; ". Next page is titled '" &amp; next_page_title &amp; "'. Continue?"
    display alert theAlertText as critical buttons {"Quit", "Process"} default button "Process"
    if (button returned of the result) is "Quit" then
        exit repeat
    end if
end repeat


This pulls the text of the current tab out of Safari and puts it into a variable, then adds that contents of that variable (padded with carriage returns) to the first document in the first BBEdit window. It then asks if you want to repeat with the next tab, exiting the loop if you say 'Quit'

You could modify this easily enough to skip over tabs, or to select the tabs to process in advance. Let me know if you want variations of that sort.