This is my preliminary effort to smart-file unsaved scripts, so I don't have to do it manually or save to the Desktop (and file later).
When I save scripts I usually drop the full header-text into it as in the appended script, or I use an abbreviated header like so:
dNam stands for development name.
I have scripts that create these headers for me and drop them into the correct place in Script Debugger. You can work the selection in the Script Editor too:
I need to make this more intelligent still, but it's saving me time and clutter already.
-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/11/30 04:57
# dMod: 2015/12/03 05:36
# Appl: Script Debugger
# Task: Save front script in ~/Documents/Working Items (Current)/Working ⇢ AppleScripts/<script-name>
# : OR
# : Save front script in ~/Documents/Scripts (Library)/Application Scripting/<Appl-Folder>/<script-name>
# Tags: @Applescript, @Script, @Script_Debugger, @Smart, @Save
-------------------------------------------------------------------------------------------
try
set myPath to (path to me as text)
if myPath ends with "Script Debugger.app:" then error "Script is not saved!"
set workingItemsFolderHFS to ((path to documents folder as text) & "Working Items (Current):Working ⇢ AppleScripts:")
set applicationScriptingLibraryHFS to ((path to documents folder as text) & "Scripts (Library):Application Scripting:")
tell application "Script Debugger"
# Make sure NOT to use this script as its own target.
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 scriptSrc to its source text
if fndBool("(?<=^# Appl: ).+", scriptSrc, false, false) of me then
set destinationFolderPath to applicationScriptingLibraryHFS & (get matchResult of result) & ":"
if not exTant(destinationFolderPath) of me then
do shell script "mkdir -p " & (quoted form of (POSIX path of destinationFolderPath))
end if
set scriptName to fnd("(?<=^# Task: ).+", scriptSrc, false, true) of me
set scriptName to cng("[[:punct:][:blank:]]+$", "", scriptName) of me
set scriptName to cng(":", ";", scriptName) of me
set savePath to destinationFolderPath & scriptName & ".scptd"
else if fndBool("(?<=^# dNam: ).+", scriptSrc, false, false) of me then
set scriptName to matchResult of result
if scriptName = false then set scriptName to its name
set scriptName to cng(
"^(.+)\\.scptd?$",
"\\1",
scriptName)
of me set scriptName to cng("[[:punct:][:blank:]]+$", "", scriptName) of me
set scriptName to cng(":", ";", scriptName) of me
set savePath to workingItemsFolderHFS & scriptName & ".scptd"
else
error "No naming criteria found!"
end if
save it in savePath as bundled compiled script
end tell
end if
end tell
on error e number n
stdErr(e, n, true, true) of me
end try
-------------------------------------------------------------------------------------------
--» 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
-------------------------------------------------------------------------------------------
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 exTant(_path) # Takes an HFS, Posix, or ~/Posix path as input.
try
if _path is "~" or _path is "~/" then
set _path to (POSIX path of (path to home folder as text))
else if _path starts with "~/" then
set _path to (POSIX path of (path to home folder as text)) & text 3 thru -1 of _path
end if
if _path starts with "/" then
alias POSIX file _path
else
alias _path
end if
return true
on error
return false
end try
end exTant
-------------------------------------------------------------------------------------------