• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Script Editor Fails to save Description Field
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script Editor Fails to save Description Field


  • Subject: Re: Script Editor Fails to save Description Field
  • From: Laine via AppleScript-Users <email@hidden>
  • Date: Thu, 20 Apr 2023 14:54:13 -0500

> On Apr 20, 2023, at 1:48 PM, Thomas Maffucci via AppleScript-Users
> <email@hidden> wrote:
>
> Edited a Script in the Description Section then saved and Closed it. When I
> later opened the script the entire Description field was blank. Running macOS
> Monterey Version 12.6.5 Script Editor Version 2.11 (227) AppleScript 2.8

There may be more than one reason that happened. Because of that, I’d like to
suggest a workaround rather than troubleshoot. Here’s a script that will allow
a selection in Script Editor to be converted into a comment block. I’ve been
using this for so long that I don’t remember where I got it. Probably Sal, and
included with the OS’ Script Editor scripts. Anyway, it’s at MacScripter
(https://www.macscripter.net/t/where-did-comment-tags-go-script-editor-scripts/49246).
 It may not be perfect, but it should at least prevent you from losing the most
contents of the description by converting them to a block comment in the
script’s body until you are confident that you are able to prevent the
description from being lost. I like it better because you can place relevant
information where it does the most good. I usually apply it using Keyboard
Maestro, but you could use Daniel Jalkut's FastScripts (Red Sweater Software
<http://www.red-sweater.com/>) or create a service using Automator, although
Automator would probably introduce more lag.

If the author of the script sees this, apologies, and please correct my
shortcoming by telling me you are the author. Thanks.

#########
set CR to ASCII character 13
set NL to ASCII character 10

tell application "Script Editor"
        tell front document
                set the target_string to "--XXXX"

                set the selected_text to contents of selection

                if the selected_text is not "" then
                        if the selected_text contains "(*" and the
selected_text contains "*)" then
                                --Could be a remove or another add
                                set BlockStart to the selected_text starts with
"(*" & CR or the selected_text starts with "(*" & NL
                                set BlockEnd to the selected_text ends with
"*)" & CR or the selected_text ends with "*)" & NL
                                if BlockStart and BlockEnd then
                                        --Case as normally created by this
script
                                        set contents of selection to characters
4 through -4 of selected_text as text
                                else
                                        set BlockStart to the selected_text
starts with "(*"
                                        set BlockEnd to the selected_text ends
with "*)"
                                        if BlockStart and BlockEnd then
                                                --Block starts with "(*"
exactly and ends with "*)" exactly
                                                set contents of selection to
characters 3 through -3 of selected_text as text
                                        else
                                                --find first occurrence of "(*"
                                                set BlockStartOffset to offset
of "(*" in selected_text
                                                --find last occurrence of "*)"
                                                set SelectionLength to length
of selected_text
                                                set BlockEndOffset to
SelectionLength - ¬
                                                        (offset of ")*" in
(reverse of (characters of selected_text) as text))
                                                log {BlockStartOffset,
BlockEndOffset, SelectionLength}

                                                if BlockStartOffset is less
than BlockEndOffset then
                                                        if BlockStart then
                                                                --Block starts
with "(*", but "*)" is somewhere before end of selection
                                                                set Newtext to
(characters 3 through (BlockEndOffset - 1) of selected_text as text) & ¬

characters (BlockEndOffset + 2) through -1 of selected_text as text
                                                                set contents of
selection to Newtext
                                                        else
                                                                if BlockEnd then
                                                                        --Block
ends with "*)", but "(*" is somewhere past beginning of selection
                                                                        set
Newtext to (characters 1 through (BlockStartOffset - 1) of selected_text as
text) & ¬

characters (BlockStartOffset + 2) through -3 of selected_text as text
                                                                        set
contents of selection to Newtext
                                                                else
                                                                        --Block
start and end are not at the selection end points (extract three blocks of text)
                                                                        set
Newtext to ((characters 1 through (BlockStartOffset - 1) of selected_text as
text) & ¬

characters (BlockStartOffset + 2) through (BlockEndOffset - 1) of selected_text
as text) & ¬

characters (BlockEndOffset + 2) through -1 of selected_text as text
                                                                        set
contents of selection to Newtext
                                                                end if
                                                        end if
                                                else
                                                        if true then
                                                                --Must be a
request to add comment block
                                                                if the last
character of selected_text is in {CR, NL} then
                                                                        set
contents of selection to "(*" & return & ¬

selected_text & ¬

"*)" & return
                                                                else
                                                                        set
contents of selection to return & ¬

"(*" & return & ¬

selected_text & return & ¬

"*)" & return
                                                                end if
                                                        else
                                                                --display
dialog "Invalid block comment range selected." with icon stop buttons "OK"
default button 1
                                                        end if
                                                end if
                                        end if
                                end if
                        else
                                --Must be a request to add comment block
                                if the last character of selected_text is in
{CR, NL} then
                                        set contents of selection to "(*" &
return & ¬
                                                selected_text & ¬
                                                "*)" & return
                                else
                                        set contents of selection to return & ¬
                                                "(*" & return & ¬
                                                selected_text & return & ¬
                                                "*)" & return
                                end if
                        end if
                else
                        display dialog "Select some text before invoking this
script command." with icon stop buttons "OK" default button 1
                end if
        end tell
end tell
#########
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >Script Editor Fails to save Description Field (From: Thomas Maffucci via AppleScript-Users <email@hidden>)

  • Prev by Date: Re: Script Editor Fails to save Description Field
  • Next by Date: Re: Script Editor Fails to save Description Field
  • Previous by thread: Re: Script Editor Fails to save Description Field
  • Next by thread: Script Editor Fails to save Description Field
  • Index(es):
    • Date
    • Thread