Switch Between Tabs and Spaces in BBEdit

Here’s a script I wrote to make it easy to switch between tab-based and space-based indention in BBEdit 10:

tell application "BBEdit"
    tell text window 1
        if expand tabs then
            set currentMode to "Spaces (" & tab width & ")"
        else
            set currentMode to "Tabs"
        end if
        display dialog "Current Mode: " & currentMode & ".
Enter new mode (blank for tabs):" default answer "4"
        set newTabWidth to text returned of result as number
        if newTabWidth = 0 then
            set expand tabs to false
        else
            set expand tabs to true
            set tab width to newTabWidth
        end if
    end tell
end tell

Indention control in BBEdit is painful. Whether tabs or spaces are used is in the Edit > Text Options sheet (confusingly named “Auto-expand tabs”) and the amount of spaces inserted in space-indention mode in stuck to the bottom of the Fonts panel (accessible via View > Text Display > Show Fonts).

This script makes it one action to figure out the current mode and modify it if necessary.

applescript bbedit Sep 21 2011