0

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"
--> Result: {button returned:"Continue"}

--need repeat code here
0

2 Answers 2

1

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 & return & page_text & return & 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 " & page_title & ". Next page is titled '" & next_page_title & "'. 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.

3
  • Thanks!! Couple things: 1. If I choose to end the process, Script Editor reports "error "User canceled." number -128". And also...... 2. is there a way to add a step checking whether the top BBEdit window is titled "Transcriptions.txt"? If so, it should proceed, and if not, it should create a new untitled doc to paste into. (this will rescue me if I haven't set up BBEdit properly for this script, i.e. refrain from dumping tons of text into a doc I'm working on for something else!). – Jimmbo
    – Jimmbo
    Jan 25, 2020 at 16:44
  • @Jimmbo — You're getting a 'user cancelled' error because that's what you told AppleScript to do, when you specified cancel button "Quit". Delete that phrase and Display Dialog will return "Quit" rather than throw an error. I'll edit revisions into my answer to address both these issues. Jan 25, 2020 at 18:48
  • Thanks! I didn't want to create a doc titled "Transcriptions" if it didn't exist (i.e. I wanted it to dump into a new untitled doc), but I fixed that by removing " with properties {name:"Transcription.txt"" BTW, I find your coding especially clear, which is great, I'm learning!
    – Jimmbo
    Jan 25, 2020 at 21:15
0

You can just put everything into a repeat statement, which will continually loop unless you specify otherwise - quitting the dialog will cancel it and the script. Note that BBEdit has decent scripting support, so the System Events stuff is not needed:

repeat -- forever, or at least until quit

    tell application "Safari"
        activate -- show what we're working with
        display alert "Continue?" message "Continue processing and close the current Safari tab?" as critical buttons {"Stop", "Continue"} default button "Continue" cancel button "Stop"
        set the clipboard to (text of current tab of front window) as text
        close current tab of front window
    end tell

    tell application "BBEdit"
        try -- guard
            get front document
        on error
            make new document
        end try
        tell front document
            set theText to (the clipboard)
            if theText is not "" then set after (get last line) to return & return & theText
        end tell
    end tell
end repeat
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .