Previous  |  Main  |  Next
AppleScript Logo

Prefix / Suffix Lines with Grep

Script for: BBEdit
Copyright: Matthias Steffens, use at your own risk!
Description: This script adds a specified prefix or suffix to all lines. The pre-/ suffix will be
remembered. If something is selected, only the selection will be processed,
otherwise all lines from the cursor position downwards are processed.

Differences to the normal "Prefix/Suffix Lines..." plug-in:
  • only lines with content (i.e. more than a return) will be treated
  • the grep command & as well as \t, \r or \xnn might be used
  • only "insert pre-/suffix" provided, not "delete pre-/suffix"
Necessary: None
Download: PreSuffixLines_v1.2.hqx (for BBEdit 6.x)
PreSuffixLines_v1.1.hqx (for BBEdit 5.x)
History: v1.2  -  works now with BBEdit 6.x (but not with BBEdit 5.x anymore -- use v1.1 together with BBEdit 5.x)
v1.1  -  code was re-written completely :) and the formerly chosen button will now be the default button upon next run
v1.0  -  original script posted at this site
The Script:
property thePreSuffix : "--\\t"
property SelectedButton : "Prefix"

tell application "BBEdit 6.0"
	activate
	if not (exists text window 1) then -- BBEdit 5.x: "if not (exists window 1) then"
		display dialog "No open document!" with icon caution buttons "Oops" default button 1
	else
		set DialogResult to display dialog ¬
			"Enter the prefix or suffix string:" & return & "(grep command & and \\t or \\r " & ¬
			"allowed)" with icon note default answer thePreSuffix buttons {"Cancel", "Suffix", "Prefix"} default button SelectedButton
		
		if button returned of DialogResult is not "Cancel" then
			set SelectedButton to button returned of DialogResult -- save the chosen button as default for the next time
			set thePreSuffix to text returned of DialogResult
			
			if SelectedButton is "Prefix" then
				set ReplaceString to (thePreSuffix & "&")
			else if SelectedButton is "Suffix" then
				set ReplaceString to ("&" & thePreSuffix)
			end if
			
			try
				tell text window 1 -- BBEdit 5.x: "tell window 1"
					if (length of selection) is 0 then -- nothing selected -> process all lines from the cursor position downwards:
						replace ("^[^" & return & "].*$") using ReplaceString options {search mode:grep} -- BBEdit 6.x
						-- replace Every Occurrence searching for "^[^" & return & "].*$" using ReplaceString with grep -- BBEdit 5.x
					else -- some text is selected -> process only (fully selected) lines within the selection:
						replace ("^[^" & return & "].*$") using ReplaceString options {search mode:grep} searching in selection -- BBEdit 6.x
						-- replace Every Occurrence searching for "^[^" & return & "].*$" using ReplaceString with grep and selection only -- BBEdit 5.x
					end if
				end tell
			on error ErrMsg number errNo
				display dialog "The following error has occurred:" & return & return & ¬
					ErrMsg & return & "(Error No: " & errNo & ")" with icon caution buttons "OK" default button 1
			end try
		end if
	end if
end tell

 


Contact: Matthias Steffens  |  Previous  |  Main  |  Next  |  Last Updated: 24-Sep-00