Tuesday, September 27, 2022

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

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

Toggle comments in script editor

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.

Applescript:

-- 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()

Last edited by peavine (2022-02-23 07:15:59 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#2 2020-04-19 12:29:41 pm

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

Re: Toggle comments in script editor

I've been using the above script without issue until a recent update of Catalina. Now, when run by way of FastScripts and on a somewhat intermittent basis, a colon is added to the beginning or end of the comment characters. For example:

## set testOne to "aa"
:## set testTwo to "bb"
:## set testThree to 1
:## set testFour to 2

I changed the comment characters to "--" but the issue persisted. I do not have this issue when the script is 1) run from within Script Editor, 2) run as an Automator service, 3) run as an app on the desktop. I emailed Daniel of FastScripts but he was unable to reproduce the issue.

Anyone have any idea why those colons are being inserted? Thanks.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#3 2020-04-19 06:27:52 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6793

Re: Toggle comments in script editor

FastScripts uses the same component instance of AppleScript for all scripts it runs, so things like text item delimiters are common to all scripts. Your code contains:

Applescript:

set selectedText to selectedList as text

That's coercing a list to text, which means it will use whatever your TIDs are at that time.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#4 2020-04-19 07:45:33 pm

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

Re: Toggle comments in script editor

Thanks Shane. Upon further investigation, I found a script, which I run by way of FastScripts, in which I had set the text item delimiter to a colon but had not reset the text item delimiter at the end of the script. As you surmise, that was the root cause of this issue. I fixed that and all is working well.

I greatly appreciate your help

Last edited by peavine (2020-04-22 12:18:31 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#5 2020-04-19 11:01:56 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6793

Re: Toggle comments in script editor

FWIW, there are Comment and Uncomment commands built in to Script Debugger, with toolbar buttons.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#6 2021-07-10 07:37:01 pm

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

Re: Toggle comments in script editor

I've been attempting to get this script to work with Script Debugger but without success. It appears that 'content' has to be changed to 'source text' and that 'character range' and 'selection' are supported but apparently 'paragraph' is not. Anyways, I thought someone who is familiar with scripting Script Debugger might be able to suggest a fix or perhaps advise that my script won't work with Script Debugger.

Thanks for the help.

Last edited by peavine (2021-07-10 07:50:12 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#7 2021-07-10 08:38:46 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6793

Re: Toggle comments in script editor

Your script would need to be modified to work with Script Debugger. But I'm not sure why you'd bother, given it has toolbar buttons and menu commands (which you can assign keyboard shortcuts to) that already do the job faster (and in some cases more cleanly).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#8 2021-07-14 10:31:31 am

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

Re: Toggle comments in script editor

Shane. I was aware there are toolbar buttons to add and remove comment characters but I didn't know there are menu items for this. I tested these and they work great.

I do like my script's ability to set the format of the comment characters, which I find to be a small convenience when editing a script. However, as things go, this is a minor matter and not worth the time it would appear to take to modify my script. I thought it might be an easy fix.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#9 2021-07-14 05:34:23 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6793

Re: Toggle comments in script editor

peavine wrote:

I do like my script's ability to set the format of the comment characters



You can change that in Script Debugger by going to Preferences -> Editing -> Comment Inserts:.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#10 2021-07-15 06:40:20 am

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

Re: Toggle comments in script editor

Thanks Shane. I am totally embarrassed smile


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#11 2021-07-22 10:22:48 am

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

Re: Toggle comments in script editor

The native ability of Script Debugger to add and remove comments is great and that's what I use. However, I wanted to learn a little about scripting Script Debugger and decided to rewrite the above script for use with Script Debugger just for learning purposes.

I've gotten everything fixed but cannot overcome one issue. I need to have the script select a particular paragraph (or range of paragraphs) of the script but can't seem to manage that. The equivalent code in Script Editor would be:

Applescript:

tell application "Script Editor" to tell document 1
   set selection to paragraph 2 -- reposition the selection
end tell

The above doesn't work in Script Debugger and so I looked at the Script Debugger dictionary, which contains:

selection (selection-object, point, or text) : The current selection within the script’s source text. If you get selection, you are handed the selected text. If you ask for selection as point or character range of selection, you get a list of two integers, the character offset after the insertion point or the start of the selection (the first character offset is 1) and the count of characters in the selection. Similarly, you can set selection to alter the selected text, but if what you set the selection to is a list of two integers, or if you set character range of selection to such a list, you reposition the selection.



So, there does not appear to be a Script-Debugger equivalent to the Script Editor code and I thought perhaps I could get the character range of the numbered paragraph and then use that with the selection commmand but that didn't work either.

Anyone have an ideas how to get the following to work:

Applescript:

tell application "Script Debugger" to tell document 1
set selection to paragraph 2 -- reposition the selection
end tell

Thanks for the help.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#12 2021-07-22 08:50:16 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6793

Re: Toggle comments in script editor

You need to get the source text as a string, and do your selection calculations based on that. It's a bit tedious (but relatively fast because there are minimal Apple events).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#13 2021-07-23 06:48:04 am

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

Re: Toggle comments in script editor

Thanks Shane. That will be a fun project for me to work on.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#14 2021-07-23 07:49:24 pm

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

Re: Toggle comments in script editor

The following version of my script works with Script Debugger.

Applescript:

-- revised 2022.07.26

on main()
   set commentCharacters to "# " -- must begin with "--" or "#"
   
   tell application "Script Debugger" to tell document 1
       set allCode to source text
       set characterRange to character range of selection
       set characterRange to getCharacterRange(allCode, characterRange) of me
       set selection to characterRange
       set selectedCode to 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 ""
       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 Debugger" to tell document 1
       set contents of selection to selectedCode
       set selection to {item 1 of characterRange, 0} -- remove selection
       -- set selection to {item 1 of characterRange, ((count selectedCode) + 1)} -- keep selection
   end tell
end main

on addComment(commentCharacters, selectedCode)
   if selectedCode = {} then return {commentCharacters}
   repeat with aParagraph in selectedCode
       set contents of aParagraph to (commentCharacters & aParagraph)
   end repeat
   return selectedCode
end addComment

on removeComment(commentCharacters, selectedCode)
   set commentCharactersCount to ((count commentCharacters) + 1)
   repeat with aParagraph in selectedCode
       if aParagraph begins with commentCharacters then
           try
               set contents of aParagraph to text commentCharactersCount thru -1 of aParagraph
           on error
               set contents of aParagraph to ""
           end try
       end if
   end repeat
   return selectedCode
end removeComment

on getCharacterRange(allCode, characterRange)
   set {c1, c2} to characterRange
   set {c1, c2} to {c1, c1 + c2 - 1}
   
   set c1Set to false
   set runningTotal to 0
   set allCode to paragraphs of allCode
   repeat with aParagraph in allCode
       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, (c2 - c1)}
end getCharacterRange

main()

Last edited by peavine (2022-07-24 06:31:23 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#15 2022-09-15 02:37:42 pm

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

Re: Toggle comments in script editor

The script contained below works with Script Debugger and is faster than the earlier version. The script does not add comment characters to lines that are blank or contain whitespace only, although this behavior is changed by editing the line that contains the following comment:

-- change "+" to "*" to comment blank lines



Applescript:

-- revised 2022.09.23

use framework "Foundation"
use scripting additions

on main()
   set commentCharacters to "# " -- must begin with "--" or "#"
   
   tell application id "com.latenightsw.ScriptDebugger8" to tell document 1
       set allCode to source text
       set {cr1, cr2} to character range of selection
       set {pr1, pr2} to getParagraphRange(allCode, cr1, cr2) of me
       set selection to {pr1, pr2}
       set selectedCode to selection
   end tell
   
   set selectedCode to current application's NSString's stringWithString:selectedCode
   set trimmedCode to getTrimmedCode(selectedCode)
   set editedCode to getEditedCode(trimmedCode, commentCharacters)
   
   tell application id "com.latenightsw.ScriptDebugger8" to tell document 1
       set contents of selection to editedCode
       set selection to {pr1, 0}
   end tell
end main

on getParagraphRange(theString, cr1, cr2)
   set theString to current application's NSString's stringWithString:theString
   set paragraphRange to theString's paragraphRangeForRange:{(cr1 - 1), cr2}
   set theParagraphs to {((paragraphRange's location) + 1), paragraphRange's |length|}
   return theParagraphs
end getParagraphRange

on getTrimmedCode(theString)
   set thePattern to "(?m)^\\h+"
   set theString to (theString's stringByReplacingOccurrencesOfString:thePattern withString:"" options:1024 range:{0, theString's |length|()})
   return theString
end getTrimmedCode

on getEditedCode(theString, theCharacters)
   set thePattern to "(?m)^" & theCharacters
   set theRange to theString's rangeOfString:thePattern options:1024
   if theRange's |length|() > 0 then -- comment characters exist`
       set theString to (theString's stringByReplacingOccurrencesOfString:thePattern withString:"" options:1024 range:{0, theString's |length|()})
   else
       set thePattern to "(?m)^(.+)$" -- change "+" to "*" to comment blank lines
       set theString to (theString's stringByReplacingOccurrencesOfString:thePattern withString:(theCharacters & "$1") options:1024 range:{0, theString's |length|()})
   end if
   return theString as text
end getEditedCode

main()

Last edited by peavine (2022-09-23 03:12:12 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#16 2022-09-18 02:36:40 pm

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

Re: Toggle comments in script editor

The script contained below works with Script Editor and is faster than the earlier version. The script does not add comment characters to lines that are blank or contain whitespace only, although this behavior is changed by editing the line that contains the following comment:

-- change "+" to "*" to comment blank lines



This script contains a bug which occurs when a blank line is selected with the line ending but without any preceding or subsequent text. The impact of this bug is relatively small.

Applescript:

-- revised 2022.09.23

use framework "Foundation"
use scripting additions

on main()
   set commentCharacters to "# " -- must begin with "--" or "#"
   
   tell application "Script Editor" to tell document 1
       set allCode to contents
       set {cr1, cr2} to character range of selection
       set {pr1, pr2} to getParagraphRange(allCode, cr1, cr2) of me
       set selection to {pr1, pr2}
       set selectedCode to contents of selection
   end tell
   
   set selectedCode to current application's NSString's stringWithString:selectedCode
   set trimmedCode to getTrimmedCode(selectedCode)
   set editedCode to getEditedCode(trimmedCode, commentCharacters)
   
   tell application "Script Editor" to tell document 1
       set contents of selection to editedCode
       set selection to insertion point (pr1 + 1)
   end tell
end main

on getParagraphRange(theString, cr1, cr2)
   set cr2 to cr2 - cr1
   set theString to current application's NSString's stringWithString:theString
   set paragraphRange to theString's paragraphRangeForRange:{cr1, cr2}
   set {pr1, pr2} to {(paragraphRange's location), paragraphRange's |length|}
   return {pr1, pr2 + pr1}
end getParagraphRange

on getTrimmedCode(theString)
   set thePattern to "(?m)^\\h+"
   set theString to (theString's stringByReplacingOccurrencesOfString:thePattern withString:"" options:1024 range:{0, theString's |length|()})
   return theString
end getTrimmedCode

on getEditedCode(theString, theCharacters)
   set thePattern to "(?m)^" & theCharacters
   set theRange to theString's rangeOfString:thePattern options:1024
   if theRange's |length|() > 0 then -- comment characters exist
       set theString to (theString's stringByReplacingOccurrencesOfString:thePattern withString:"" options:1024 range:{0, theString's |length|()})
   else
       set thePattern to "(?m)^(.+)$" -- change "+" to "*" to comment blank lines
       set theString to (theString's stringByReplacingOccurrencesOfString:thePattern withString:(theCharacters & "$1") options:1024 range:{0, theString's |length|()})
   end if
   return theString as text
end getEditedCode

main()

Last edited by peavine (Yesterday 08:20:25 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)