Friday, November 16, 2018

#1 2018-09-24 09:20:55 am

From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 18

Toggle comments in script editor

When editing a script, I often need to disable code sections and, in the past, have done this with block comments. I found this a bit cumbersome at times and wrote a script for use in temporarily and visibly disabling sections of a script.

The code in the script's extend-selection handler was obtained from: … e-shortcut

To toggle double-dash comments--which might be more useful in general--Nigel's script in the following thread works well:


--Revised 11.01.18.

--Start run handler.
on run
   --Set variable to comment characters (the first character must be #).
   set commentCharacters to "## "
   --Extend selection to include entire line.
   set selectedCode to extendSelection()
   --Modify selected code to add or remove comment characters.
   if commentCharacters is in (selectedCode as text) then
       set modifiedCode to removeComments(commentCharacters, selectedCode)
       set modifiedCode to addComments(commentCharacters, selectedCode)
   end if
   --Replace selected code with modified code.
   tell application "Script Editor"
       tell document 1
           set contents of selection to modifiedCode
       end tell
   end tell
   --End run handler.
end run

--Extend selection handler.
on extendSelection()
       tell application "Script Editor"
           tell document 1
               set allCode to contents
               set {c1, c2} to character range of selection
               if c2 = 0 then
                   set selection to paragraph 1
               else if c1 = c2 then
                   set selection to paragraph (count (paragraphs of (text 1 thru c2 of allCode)))
                   set p1 to count (paragraphs of (text 1 thru c1 of allCode))
                   set p2 to count (paragraphs of (text 1 thru c2 of allCode))
                   if item c2 of allCode is in {return, linefeed} then set p2 to p2 - 1
                   set selection to paragraphs p1 thru p2
               end if
               set selectedCode to (paragraphs of (contents of selection))
           end tell
       end tell
   on error
       error number -128
   end try
end extendSelection

--Remove comments handler.
on removeComments(commentCharacters, selectedCode)
   set modifiedCode to selectedCode as text
   set AppleScript's text item delimiters to commentCharacters
   set modifiedCode to text items of modifiedCode
   set AppleScript's text item delimiters to ""
   set modifiedCode to modifiedCode as text
end removeComments

--Add comments handler.
on addComments(commentCharacters, selectedCode)
   set modifiedCode to {}
   repeat with i from 1 to (count selectedCode)
       set the end of modifiedCode to commentCharacters
       set the end of modifiedCode to (item i of selectedCode)
   end repeat
   set modifiedCode to modifiedCode as text
end addComments

Last edited by peavine (2018-11-01 09:55:33 am)



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)