• 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
Automating Script Headers
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Automating Script Headers


  • Subject: Automating Script Headers
  • From: Christopher Stone <email@hidden>
  • Date: Wed, 22 Feb 2017 20:08:35 -0600

On Feb 22, 2017, at 18:10, Jean-Christophe Helary <email@hidden> wrote:
I like the way you set headers for your scripts. Which tool do you use ?


Hey Jean-Christophe,

Thanks.

It's completely custom.

Requires Script Debugger and the Satimage.osax.

It could be adapted for another editor of course.

The script is more complex than it has to be, because I'm inserting text in a way that doesn't disturb the existing formatting in the script window.  I do this, because I want to be able to tell at a glance what text was newly inserted – and the color differences give me exactly that ability.

I've bound the script to a hotkey in Script Debugger's script menu so as to instantly have a header at the press of (Ctrl-h).

The script tries to automatically add app-names and certain tags.

I have a similar but much simpler script to insert a temporary-development-header:

-------------------------------------------------------------------------------------------
# dNam:>|< cursor here
# dMod: 2017/02/22 19:57 
-------------------------------------------------------------------------------------------

When I'm developing a script I don't want a bunch of clutter, but I often want a dev-name for quick identification and reference – and a mod-date, so I know when I was last working on the script.

I have another script that updates the modification date in the header with a keystroke (Ctrl-Opt-Cmd-M).

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2010/10/11 21:32
# dMod: 2017/02/22 19:42
# Appl: Script Debugger
# Task: Insert info header into the front script document.
# Libs: ELb, FLb, GLb
# Osax: Satimage.osax { http://tinyurl.com/dc3soh }
# Tags: @Applescript, @Script_Debugger, @Script, @Insert, @Attribution, @Info, @Header,
-------------------------------------------------------------------------------------------

try

   

   set dateTime to strftime (current date) into "%Y/%m/%d %R" -- Satimage Osax
   set _sep to "

-------------------------------------------------------------------------------------------
"
   set headerTextTemplate to text 2 thru -1 of "
-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: " & dateTime & "
# dMod: " & dateTime & " 
# Appl: <apps>
# Task: 
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, <tags>
-------------------------------------------------------------------------------------------

"
   set myPath to (path to me as text)
   if myPath ends with "Script Debugger.app:" then error "Script is NOT saved!"

   

   tell application "Script Debugger"

      

      -------------------------------------------------------------------------------------
      # Make sure this script doesn't act on itself (must be saved).
      -------------------------------------------------------------------------------------
      if myPath = (get file spec of front document as text) then
         set scriptDoc to a reference to document 2
      else
         set scriptDoc to a reference to document 1
      end if
      -------------------------------------------------------------------------------------

      

      if scriptDoc exists then
         tell scriptDoc
            set scriptSource to its source text

            

            if (fndBool("^# Auth.+", scriptSource, false, false) of me) = false then

               

               --» Compile Tags for found Applications from script content:
               set appList to fndUsing("^[[:blank:]]*tell application \"(.+?)\"", "\\1", scriptSource, true, true) of me
               if class of appList is text then set appList to {appList}
               set appList to sortlist appList with remove duplicates
               set headerTextTemplate to cng("<apps>", (join appList using ", "), headerTextTemplate) of me

               

               copy appList to tagList

               

               if fndBool("use framework", scriptSource, false, false) of me then
                  set end of tagList to "ASObjC"
                  set tagList to sortlist tagList with remove duplicates
               end if

               

               set tagList to cng("^", "@", tagList) of me
               set tagList to cng(" ", "_", tagList) of me
               set tagList to join tagList using ", "
               set headerTextTemplate to cng("<tags>", tagList, headerTextTemplate) of me

               

               --» Remove trailing whitespace and separater line if it exists:
               set textSelector to fnd("^\\s*----------*\\s*\\Z", scriptSource, 0, 0) of me

               

               if (textSelector ≠ false) then
                  set {_matchPos, _matchLen} to {matchPos, matchLen} of textSelector
                  set _matchPos to _matchPos + 1 # Translate SIO to SD coordinates
                  set selection to {_matchPos, _matchLen}
                  set selection to _sep
               else
                  --» Select insertion point at end of text.
                  set _len to length of scriptSource
                  set selection to {_len + 1, 0}
                  set selection to _sep
               end if

               

               --» Allow for leading whitespace at top of script:
               set textSelector to fnd("\\A\\s*----------*\\s*", scriptSource, 0, 0) of me

               

               --» Insert Header Text:
               if textSelector ≠ false then

                  

                  set {_matchPos, _matchLen} to {matchPos, matchLen} of textSelector
                  set _matchPos to _matchPos + 1 # Translate SIO to SD coordinates
                  set selection to {_matchPos, _matchLen}
                  set selection to headerTextTemplate

                  

               else

                  

                  set selection to {1, 0}
                  set selection to headerTextTemplate
               end if

               

            end if

            

         end tell
      end if
   end tell

   

on error e number n
   stdErr(e, n, true, true) of me
end try

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on cng(_find, _replace, _data)
   change _find into _replace in _data with regexp without case sensitive
end cng
-------------------------------------------------------------------------------------------
on fnd(_find, _data, _all, strRslt)
   try
      find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
   on error
      return false
   end try
end fnd
-------------------------------------------------------------------------------------------
on fndBool(_find, _data, _all, strRslt)
   try
      find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
      return true
   on error
      return false
   end try
end fndBool
-------------------------------------------------------------------------------------------
on fndUsing(_find, _capture, _data, _all, strRslt)
   try
      set findResult to find text _find in _data using _capture all occurrences _all ¬
         string result strRslt with regexp without case sensitive
   on error
      false
   end try
end fndUsing
-------------------------------------------------------------------------------------------
on removeDupesFromListOfStr(theList)
   set tempList to {}
   set listLength to length of theList
   repeat with itemNum from 1 to listLength
      if itemNum ≠ listLength and (items (itemNum + 1) thru -1 of theList) contains (item itemNum of theList) then
         set (item itemNum of theList) to null
      end if
   end repeat

   

   return text of theList

   

end removeDupesFromListOfStr
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
   set e to e & return & return & "Num: " & n
   if beepFlag = true then
      beep
   end if
   if ddFlag = true then
      tell me
         set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
      end tell
      if button returned of dDlg = "Copy" then set the clipboard to e
   else
      return e
   end if
end stdErr
-------------------------------------------------------------------------------------------



 _______________________________________________
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

  • Prev by Date: Re: Finder window views ?
  • Next by Date: tools !!!!
  • Previous by thread: Re: [ANN] Table dialogs
  • Next by thread: tools !!!!
  • Index(es):
    • Date
    • Thread