Monday, March 25, 2019

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

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 55

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:

https://apple.stackexchange.com/questio … e-shortcut

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

https://macscripter.net/viewtopic.php?id=35772

Applescript:


--Reviewed 02.03.19.

--Start run handler.
on run
   
   --Set variable to comment characters (the first character must be #).
   set commentCharacters to "## "
   
   --Extend selected code 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)
   else
       set modifiedCode to addComments(commentCharacters, selectedCode)
   end if
   
   --Replace selected code with modified code.
   tell application "Script Editor" to tell document 1
       set contents of selection to modifiedCode
       set {c1, c2} to character range of selection
       set selection to {c1 - 1, c1 - 1}
   end tell
   
   --End run handler.
end run

--Extend selection.
on extendSelection()
   try
       tell application "Script Editor" to 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)))
           else
               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
   on error
       error number -128
   end try
end extendSelection

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

--Add comment characters.
on addComments(commentCharacters, selectedCode)
   set modifiedCode to {}
   repeat with aLine in selectedCode
       set the end of modifiedCode to commentCharacters
       set the end of modifiedCode to contents of aLine
   end repeat
   set modifiedCode to modifiedCode as text
end addComments

Last edited by peavine (2019-02-03 03:18:32 pm)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)