Script to Insert a Custom Header into Script Debugger Scripts

Hey Folks,

Mark has a nice template system for Script Debugger that allows newly created scripts to insert various information, but for decades now I’ve rolled my own.

Appended is my most basic development header script and is used for when I’m working on something new and don’t want much cruft in the way. I find that if I don’t give my working scripts at least some basic ID and date information they all too easily get lost in the shuffle.

The working header looks like this:

--------------------------------------------------------
# dNam: | «-- CURSOR
# dMod: 2021/09/06 10:46 
--------------------------------------------------------

Once I give the script a dev name I can hit a hotkey and save it instantly (without a dialog) to my working items folder.

Or – if I give the name a prefix like /KMF/Script-Name it will be saved to:

~/Working Items/Keyboard Maestro Forum/Script-Name.scpt

I have a much more sophisticated header-creation-script that mines the working script for data to insert into the header, but it presently depends upon the Satimage.osax.

I’m working to move SIO functions to ASObjC and will eventually post it.

The appended script serves as an example of dynamically creating a script header, and working with the selection in Script Debugger.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2010/10/11 21:32
# dMod: 2021/09/06 22:44
# Appl: Script Debugger
# Task: Insert a terse dev header into the front script document.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script_Debugger, @Script, @Header
# Note: Operates on Document 2 if the script itself is Document 1.
# Test: macOS 10.12.6 ⇢ 10.14.6
# Vers: 2.00
--------------------------------------------------------
use AppleScript version "2.5" -- El Capitan or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------
property LF : linefeed
--------------------------------------------------------

set dateTime to its makeFormattedDateString:(current date) usingFormat:"Y/MM/dd hh:mm"

set headerTextTemplate to text 2 thru -1 of "
--------------------------------------------------------
# dNam: 
# dMod: " & dateTime & " 
--------------------------------------------------------
"

# Example Find/Replace for changing the header string on demand.
# set headerTextTemplate to its cngStr:"(?m)^(# dMod:.+)" intoString:("# dCre: " & LF & "$1") inString:headerTextTemplate

set myPath to (path to me as text)

tell application "Script Debugger"
   
   --» If THIS script document is FRONTMOST operate on Document 2.
   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
         --» Insert Header Text:
         set selection to {1, 0}
         set selection to headerTextTemplate
         set selection to {101, 0}
      end tell
   end if
   
end tell

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on cngStr:findString intoString:replaceString inString:dataString
   set anNSString to current application's NSString's stringWithString:dataString
   set dataString to (anNSString's ¬
      stringByReplacingOccurrencesOfString:findString withString:replaceString ¬
         options:(current application's NSRegularExpressionSearch) range:{0, length of dataString}) as text
end cngStr:intoString:inString:
--------------------------------------------------------
on makeFormattedDateString:theDate usingFormat:formatString
   set theFormatter to current application's NSDateFormatter's new()
   theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
   theFormatter's setDateFormat:formatString
   set theString to theFormatter's stringFromDate:theDate
   return theString as text
end makeFormattedDateString:usingFormat:
--------------------------------------------------------