When editing a script with Script Editor, I often need to disable code sections and, in the past, I have used block comments for this purpose. I found this a bit cumbersome at times and wrote the following script for use in temporarily disabling sections of a script.
This script works as a toggle. If any line in the selection begins with the specified comment characters, then the comment characters are removed from the beginning of all lines in the selection. Otherwise, comment characters are added to the beginning of every line in the selection.
-- Revised 2022.02.23
on main()
set commentCharacters to "# " -- must begin with "--" or "#"
tell application "Script Editor" to tell document 1
set allCode to contents
set characterRange to character range of selection
set characterRange to getCharacterRange(allCode, characterRange) of me
set selection to characterRange
set selectedCode to contents of selection
end tell
set commentCharactersExist to false
set selectedCode to paragraphs of selectedCode
repeat with aParagraph in selectedCode
set theParagraph to contents of aParagraph
try
repeat while text 1 of theParagraph is in {tab, space}
set theParagraph to text 2 thru -1 of theParagraph
end repeat
on error
set theParagraph to tab
end try
set contents of aParagraph to theParagraph
if theParagraph begins with commentCharacters then set commentCharactersExist to true
end repeat
if commentCharactersExist then
set selectedCode to removeComment(commentCharacters, selectedCode)
else
set selectedCode to addComment(commentCharacters, selectedCode)
end if
set text item delimiters to {linefeed}
set selectedCode to selectedCode as text
set text item delimiters to {""}
tell application "Script Editor" to tell document 1
set contents of selection to selectedCode
set {c1, c2} to character range of selection
set selection to insertion point c1
end tell
end main
on addComment(commentCharacters, selectedCode)
if selectedCode = {} then set selectedCode to {tab}
repeat with aParagraph in selectedCode
set contents of aParagraph to (commentCharacters & aParagraph)
end repeat
return selectedCode
end addComment
on removeComment(commentCharacters, selectedCode)
if selectedCode = {} then set selectedCode to {tab}
repeat with aParagraph in selectedCode
if aParagraph begins with commentCharacters then
try
set contents of aParagraph to (text ((count commentCharacters) + 1) thru -1 of aParagraph)
on error
set contents of aParagraph to tab
end try
end if
end repeat
return selectedCode
end removeComment
on getCharacterRange(allCode, characterRange)
set {c1, c2} to characterRange
if c2 = 0 then set c2 to 1
if item c2 of allCode is in {return, linefeed} then set c2 to (c2 - 1) -- remove linefeed at end of selection
set c1Set to false
set runningTotal to 0
set allCode to paragraphs of allCode
repeat with aParagraph in allCode
set aParagraph to contents of aParagraph
set runningTotal to runningTotal + (count aParagraph) + 1
if c1Set = false and runningTotal > c1 then
set c1 to runningTotal - (count aParagraph)
set c1Set to true
end if
if c1Set = true and runningTotal > c2 then
set c2 to runningTotal
exit repeat
end if
end repeat
return {c1 - 1, c2 - 1}
end getCharacterRange
main()