Search code examples
applescriptbbedit

Get number of tabstops or white space and concatenate in Applescript (smart newline script to insert bullet)


My end objective is to create an applescript that intelligently inserts a bullet point for me automatically when i hit Alt + Enter. I'm trying to do this in BBEdit and here's the Apple script i snagged from the BBEdit forums:

tell application "BBEdit"
    try
        tell text of front text window
            set lineOfInsertionPoint to line (startLine of selection)
            set findReco to find "^\\s*\\d+\\." searching in lineOfInsertionPoint options {search mode:grep}
            if found of findReco = true then
                set leadingNumber to text 1 thru -2 of (found text of findReco)
                set text of selection to return & (leadingNumber + 1) & ". "
                select insertion point after selection
            else if found of findReco = false then
                set findReco to find "^\\s*\\* " searching in lineOfInsertionPoint options {search mode:grep}
                if found of findReco = true then
                    set text of selection to return & "* "
                    select insertion point after selection
                else
                    set findReco to find "^\\s*\\+" searching in lineOfInsertionPoint options {search mode:grep}
                    if found of findReco = true then

                        set text of selection to return & tab & "+ "
                        select insertion point after selection
                    end if
                end if
            end if
        end tell
    on error errMsg number errNum
        set sep to "=============================="
        set e to sep & return & "Error: " & errMsg & return & sep & return ¬
            & "Error Number: " & errNum & return & sep
        beep
        display dialog e
    end try
end tell

The script works well, but the issue is that when you already have a certain number of tab stops or white space at the beginning, the applescript inserts the next bullet right at the start of the line ignoring the whitespaces/tab stop.

So my actual question is quite simply "How does one get hold of the number of leading tab stops or whitespaces through Applescript" and concatenate it here?

Cheers.


Solution

  • Kendall Conrad has updated a similar and more functional script at http://www.angelwatt.com/words/2011/04/11/bbedit-smart-newline-open-line/ .