I wonder if anyone else has such a template they could share.
# Script Debugger Template Version 2015-12-11
(*
Description of the code goes here.
To Do:
-- Item 1
*)
-- Properties to Make Script Relocatable. ** NB: For this to work correctly in SD, script must be saved first. Doesn't work in AppleScript Editor
property pMyHostName : host name of (system info) -- Host on which the script was compiled
property pMyScriptName : missing value
property pMyFolder : POSIX file (POSIX path of ((path to me as text) & "::")) as text
property pMyHomeFolder : path to home folder as text
property pConfigurationChange : true -- Flag a configuration change
-- Optional Log File
property pLogFile : missing value
property pLogFileName : missing value
property pLogFileFolder : "Library:Logs:"
property pLogDiagnosticInfo : true
-- Debug Control
property pDebugCaseTestLog : false -- Test writing to log even if app doesn't use log
-- Behavior Control
property pDialogTitle : missing value -- Run time. Holder for Display Dialog titles. Default pScriptName
property pDialogTimeOut : 5 -- Optional time out for dialogs
property pFirstRun : true
-- Error Numbers
property pUserCanceled : -128 -- Display Dialog cancel button
property pFileAlreadyExists : -48 -- Finder error message
-- Miscellaneous Dictionary
property pReturn2 : return & return
property pContentsApplet : ":Contents:MacOS:applet" -- Specify the applet in the application bundle
-- Your Properties Go Here
on run
-------------------------------------------------------------------
-- Code to Make Script Relocatable
-------------------------------------------------------------------
-- First save the previous location values
set pConfigurationChange to false
if pMyScriptName is missing value then set pMyScriptName to getMyName() -- Must be the first run
set previousMyScriptName to pMyScriptName
set previousHostName to pMyHostName
set previousMyFolder to pMyFolder
set previousHomeFolder to pMyHomeFolder
-- Get the current location values
set pMyHostName to host name of (system info)
set pMyScriptName to getMyName()
set pMyFolder to POSIX file (POSIX path of ((path to me as text) & "::")) as text
set pMyHomeFolder to path to home folder as text
-- Flag if the configuration has changed
if previousMyScriptName ≠ pMyScriptName then set pConfigurationChange to true
if previousHostName ≠ pMyHostName then set pConfigurationChange to true
if previousMyFolder ≠ pMyFolder then set pConfigurationChange to true
if previousHomeFolder ≠ pMyHomeFolder then pConfigurationChange
-------------------------------------------------------------------
-- First Run Code
-------------------------------------------------------------------
if pFirstRun or pConfigurationChange then
set pLogFileName to pMyScriptName & ".log"
set pLogFile to POSIX path of (pMyHomeFolder & pLogFileFolder & pLogFileName)
set pDialogTitle to pMyScriptName
-- Your first run code goes here
set pFirstRun to false
set pConfigurationChange to false
else
-- Your code for every run but first run goes here
end if
-------------------------------------------------------------------
-- Every Run Code
-------------------------------------------------------------------
set handlerPrefix to camelCase(pMyScriptName)
if pDebugCaseTestLog then logEvent(handlerPrefix, "Log Test", pLogFile)
-- Your code for every run goes here
end run
-------------------------------------------------------------------
-- Your Script Handlers
-------------------------------------------------------------------
-------------------------------------------------------------------
-- Template Handlers
-------------------------------------------------------------------
on logEvent(theSender, theMessage, theLogFile)
-- theLogFile must be a POSIX path
if class of theLogFile is not text or the first character of theLogFile is not "/" then
display dialog "Error: theLogFile must be a POSIX path" with title "logEvent: " & theSender
return
end if
log (theMessage)
set theSender to quoted form of theSender
set theMessage to quoted form of theMessage
set theLogFile to quoted form of theLogFile
set theLine to (do shell script ¬
"date +'%Y-%m-%d %H:%M:%S'" as string) & " " & theSender & " " & theMessage
log "theLine: " & theLine
try
do shell script "echo " & theLine & ¬
" >> " & theLogFile
on error errMsg number errNum
display dialog errMsg with title "logEvent: " & theSender
end try
end logEvent
on getMyName()
set x to path to me
tell application "Finder"
set y to name of file x as text
set z to "." & name extension of file x as text
end tell
set zz to offset of z in y
if zz = 0 then
set myName to y
else
set myName to (characters 1 thru (zz - 1)) of y as text
end if
return myName
end getMyName
on camelCase(aPhrase)
set camelCasePhrase to ""
repeat with nn from 1 to count of words of aPhrase
set theWord to word nn of aPhrase
set leadCharacter to first character of theWord
if nn = 1 then
if (ASCII number of leadCharacter) ≤ (ASCII number of "Z") then
set leadCharacter to ASCII character of ((ASCII number of leadCharacter) + 32)
end if
else
if (ASCII number of leadCharacter) > (ASCII number of "Z") then
set leadCharacter to ASCII character of ((ASCII number of leadCharacter) - 32)
end if
end if
set theWord to leadCharacter & (characters 2 through (count of theWord)) of theWord as text
set camelCasePhrase to camelCasePhrase & theWord as string
end repeat
return camelCasePhrase
end camelCase
#=====
on getTextItems(theText, replacementDelimiterList)
-- Uses Text Item Delimiters to extract text items. Saves and restores existing delimiters
-- From Yvan Koenig
local existingDelimiterList, theTextItems
set {existingDelimiterList, AppleScript's text item delimiters} to {AppleScript's text item delimiters, replacementDelimiterList}
set l to text items of theText
set AppleScript's text item delimiters to existingDelimiterList
return theTextItems
end getTextItems
#=====
on makeTextFromItems(listOfItems, replacementDelimiterList)
-- Make a list of text items into text
-- From Yvan Koenig
local existingDelimiterList, theText
set {existingDelimiterList, AppleScript's text item delimiters} to {AppleScript's text item delimiters, replacementDelimiterList}
set theText to listOfItems as text
set AppleScript's text item delimiters to existingDelimiterList
return theText
end makeTextFromItems
#=====
on replaceText(theText, targetText, replacementText)
-- Replaces every occurence of targetText by replacementText in theText
-- From Yvan Koenig
local existingDelimiterList, intermediateText
set {existingDelimiterList, AppleScript's text item delimiters} to {AppleScript's text item delimiters, targetText}
set l to text items of theText
set AppleScript's text item delimiters to replacementText
set theText to intermediateText as text
set AppleScript's text item delimiters to existingDelimiterList
return theText
end replaceText
#=====