Friday, October 22, 2021

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

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

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 and visibly disabling sections of a script.

Applescript:

-- Revised 2020.10.03

main()

on main()
   set commentCharacters to "## "
   
   set AppleScript's text item delimiters to {""}
   
   set selectionList to extendSelection()
   set selectionText to selectionList as text
   
   if selectionText contains commentCharacters then
       set modifiedCode to removeComment(commentCharacters, selectionText)
   else
       set modifiedCode to addComment(commentCharacters, selectionList)
   end if
   
   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 insertion point c1
   end tell
end main

on extendSelection() -- credit to Lri on stackoverflow for basic code
   tell application "Script Editor" to tell document 1
       try
           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 p to count (paragraphs of (text 1 thru c2 of allCode))
               if contents of selection is in {return, linefeed} then set p to p - 1
               set selection to paragraph p
           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
           return paragraphs of (contents of selection)
       on error
           error number -128
       end try
   end tell
end extendSelection

on removeComment(commentCharacters, selectionText)
   set AppleScript's text item delimiters to commentCharacters
   set selectionText to text items of selectionText
   set AppleScript's text item delimiters to {""}
   return (selectionText as text)
end removeComment

on addComment(commentCharacters, selectionList)
   ignoring white space
       repeat with aParagraph in selectionList
           if contents of aParagraph > "" then
               set contents of aParagraph to (commentCharacters & aParagraph)
           end if
       end repeat
   end ignoring
   return (selectionList as text)
end addComment

Last edited by peavine (2020-10-03 06:42:59 pm)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

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

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

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 Catalina - Script Debugger 8

Offline

 

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

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

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: 1089

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 Catalina - Script Debugger 8

Offline

 

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

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

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: 1089

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 Catalina - Script Debugger 8

Offline

 

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

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

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: 1089

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 Catalina - Script Debugger 8

Offline

 

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

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

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: 1089

Re: Toggle comments in script editor

Thanks Shane. I am totally embarrassed smile


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

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

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

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 Catalina - Script Debugger 8

Offline

 

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

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

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: 1089

Re: Toggle comments in script editor

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


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

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

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

Re: Toggle comments in script editor

The following version of my script works with Script Debugger. As noted above, this script is not intended for actual use, because the built-in functionality of Script Debugger does the job extremely well.

Applescript:

-- Revised 2021.07.27

on main()
   set commentCharacters to "## "
   set characterRange to getCharacterRange()
   
   tell application "Script Debugger" to tell document 1
       set selection to characterRange
       set selectedCode to selection
   end tell
   
   if selectedCode contains commentCharacters then
       set modifiedCode to removeComment(commentCharacters, selectedCode)
   else
       set modifiedCode to addComment(commentCharacters, selectedCode)
   end if
   
   tell application "Script Debugger" to tell document 1
       set contents of selection to modifiedCode
       set selection to {item 1 of characterRange, 0}
   end tell
end main

on removeComment(commentCharacters, selectedCode)
   set AppleScript's text item delimiters to {commentCharacters}
   set selectedCode to text items of selectedCode
   set AppleScript's text item delimiters to {""}
   return (selectedCode) as text
end removeComment

on addComment(commentCharacters, selectedCode)
   set selectedCode to paragraphs of selectedCode
   repeat with aParagraph in selectedCode
       set contents of aParagraph to (commentCharacters & aParagraph & linefeed)
   end repeat
   try
       return text 1 thru -2 of (selectedCode as text)
   on error
       return commentCharacters
   end try
end addComment

on getCharacterRange()
   tell application "Script Debugger" to tell document 1
       set allCode to source text
       set {c1, c2} to character range of selection
   end tell
   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 (2021-07-27 06:57:11 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#15 2021-07-24 02:50:20 pm

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

Re: Toggle comments in script editor

The following script is similar to that above except that it makes use of ASObjC. I ran timing tests and found this script to be slower but only by a few milliseconds.

Applescript:

-- Revised 2021.07.25

use framework "Foundation"
use scripting additions

on main()
   set commentCharacters to "## "
   
   tell application "Script Debugger" to tell document 1
       set allCode to source text
       set theCharacterRange to my getCharacterRange(allCode)
       set selection to theCharacterRange
       set selectedCode to selection
   end tell
   
   if selectedCode contains commentCharacters then
       set modifiedCode to removeComment(commentCharacters, selectedCode)
   else
       set modifiedCode to addComment(commentCharacters, selectedCode)
   end if
   
   tell application "Script Debugger" to tell document 1
       set contents of selection to modifiedCode
       set selection to {item 1 of theCharacterRange, 0}
   end tell
end main

on removeComment(commentCharacters, selectedCode)
   set selectedCode to current application's NSString's stringWithString:selectedCode
   set selectedCode to selectedCode's stringByReplacingOccurrencesOfString:commentCharacters withString:""
   return selectedCode as text
end removeComment

on addComment(commentCharacters, selectedCode)
   set selectedCode to current application's NSString's stringWithString:selectedCode
   set theDelimiter to current application's NSCharacterSet's characterSetWithCharactersInString:linefeed
   set selectedCode to (selectedCode's componentsSeparatedByCharactersInSet:theDelimiter)'s mutableCopy()
   
   repeat with aParagraph in selectedCode
       set contents of aParagraph to current application's NSString's stringWithFormat_("%@%@%@", commentCharacters, aParagraph, linefeed)
   end repeat
   
   try
       return text 1 thru -2 of ((selectedCode as list) as text)
   on error
       return commentCharacters
   end try
end addComment

on getCharacterRange(allCode)
   set allCode to current application's NSString's stringWithString:allCode
   set theDelimiter to current application's NSCharacterSet's characterSetWithCharactersInString:linefeed
   set allCode to allCode's componentsSeparatedByCharactersInSet:theDelimiter
   set allCodeParagraphCounts to (allCode's valueForKey:"length") as list
   
   tell application "Script Debugger" to set {c1, c2} to character range of selection of document 1
   set {c1, c2} to {c1, c1 + c2 - 1}
   
   set c1Set to false
   set runningTotal to 0
   repeat with aCount in allCodeParagraphCounts
       set runningTotal to runningTotal + aCount + 1
       if c1Set = false and runningTotal ≥ c1 then
           set c1 to runningTotal - aCount
           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 (2021-07-31 07:14:08 am)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)