Sometimes I want to dump my Script Debugger session, so I can quit with safety and reboot or whatever.
This script saves aliases of all previously saved open script documents to a new dated folder, and it saves the source of unsaved script documents into individual .applescript files in the same folder. It then opens the dump-folder in the Finder for convenience.
The script is dependent upon the Satimage.osax. At the moment I don’t want to take the time to make a vanilla variant, but it quite obviously wouldn’t be difficult.
-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2013-05-20 : 16:36
# dMod: 2013-05-20 : 17:08
# Appl: Script Debugger 5
# Task: Save Script Aliases & Unsaved Script Source to Saved-Session-Scripts folder.
# Libs: err.lib, find.lib, gen.lib (Irrelevant in posted script.)
# Osax: Satimage.osax
# Tags: @Applescript, @Script_Debugger, @Backup, @Dump, @Session
-------------------------------------------------------------------------------------------
try
set unsavedFlag to false
tell application "Script Debugger"
set sdAppSuptFldr to (join (items 1 thru -3 of (splittext (templates folder as text) using ":")) using ":") & ":"
set dumpFldr to sdAppSuptFldr & "Saved-Session-Scripts:"
set scriptPathList to file spec of documents whose path contains "users/"
set unsavedDocSrcL to source text of every document whose name starts with "Untitled"
if unsavedDocSrcL ≠{} then
repeat with i in unsavedDocSrcL
# Strip whitespace off top and bottom of src:
set contents of i to cng("\\A\\s+|\\s+\\Z", "", contents of i) of me
end repeat
set unsavedFlag to true
end if
end tell
set _date to strftime (get current date) into "%Y.%m.%d.%H%M%S"
set newFldrPathH to (dumpFldr & "Session_Dump_" & _date) & ":"
do shell script "mkdir -p " & (quoted form of (POSIX path of newFldrPathH))
set writeToAlias to alias newFldrPathH
tell application "Finder"
repeat with i in scriptPathList
make new alias file at writeToAlias to (i)
end repeat
open alias dumpFldr
end tell
if unsavedFlag then
repeat with i from 1 to length of unsavedDocSrcL
writetext (item i of unsavedDocSrcL) to file ((newFldrPathH as text) & "Unsaved_Script_Src_" & i & ".applescript")
end repeat
end if
on error e number n
stdErr(e, n, true, true) of me
end try
-------------------------------------------------------------------------------------------
on cng(findText, changeText, srcData)
change findText into changeText in srcData with regexp without case sensitive
end cng
-------------------------------------------------------------------------------------------
on stdErr(e, n, beepFlg, ddFlg)
set e to e & return & return & "Num: " & n
if beepFlg = true then
beep
end if
if ddFlg = 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
-------------------------------------------------------------------------------------------
I am on Script Debugger 4.5.7, I wonder where I find the splittext handler, as it won’t compile without it.
Edit
I have Satimage.osax installed, it is funny that it doesn’t get the call.
Ok it turned out to be something else.
First of all, I had to change the name to Script Debugger 4.5.
Secondly Script Debugger 4.5 doesn’t have a templates folder, so I made one by installing a property:
property templates_folder : (path to desktop as text) & "Templates"
And referencing that property in the offending line instead.
set sdAppSuptFldr to (join (items 1 thru -3 of (splittext (templates_folder) using ":")) using ":") & ":"
Third change was that I had to change the line that grabs the source of the scripts, and change that into:
set unsavedDocSrcL to (get script source of every document whose name starts with "Untitled")
And now it works great!
Thank you very much Chris, I can’t even describe that god-awful feeling when it crashes, and the Untitled one’s doesn’t get back up again when I restart it! -It may be an end to that now, if I only remember to run this script!
Okay. Would you test these for me and see if they play nice with SD4.5?
set smFl to scripts menu folder
set sdApp to (path to frontmost application as text)
tell application sdApp
set _version to version as text
if _version starts with "5." then
set unsavedDocSrcL to source text of every document whose name starts with "Untitled"
else if _version starts with "4.5" then
set unsavedDocSrcL to script source of every document whose name starts with "Untitled"
end if
end tell
Okay. I’m not surprised that it wouldn’t compile on SD4.5. It does in SD5, so I wanted to be sure.
This should work:
set sdApp to (path to frontmost application as text)
set _sd5 to "source text of every document whose name starts with \"Untitled\""
set _sd4 to "script source of every document whose name starts with \"Untitled\""
tell application sdApp
set _version to version as text
if _version starts with "5." then
set unsavedDocSrcL to run script _sd5
else if _version starts with "4.5" then
set unsavedDocSrcL to run script _sd4
end if
end tell
If it doesn’t I’ll dig out a copy of SD4.5.x and do some testing locally.
What I didn’t manage to figure out, was how to get the values in unsavedDocSrcL out again.
Edit
It should be possible to make it work over several scripts, since it is possible to share a global between them.
Something I think could work, is for instance to choose a version of a script based on a bundle, and then load the
correct script file, and run it. It is an awkward workaround, but it should work. Maybe you know of something better.
I think, it would really suffice, to just have the alternative line commented out: if you are running 4.5 and tries to compile it.it will hang above or below a comment about what to do, and that is it.
Heck, people who runs Script Debugger is supposed to be able to do such things.
At least that is more than good enough in my book!
By the way, unsavedDocSrcL doesn’t get the value back in your last version either, not on my machine at least.
Edit
If your goal is to distribute it as a script, then I guess it needs to be recompiled for 4.5 anyway, and if not, and you want the process to go flawlessly, then I suggest programmatically editing and compiling the script on a users machine, from an installer script, (or something). Not convenient if it was to be shipped as a run-only script though, but it think that approach to be more practically feasible, then having a bundle with certain scripts being called all the time.
It may be an easier work around than all of this out there anyway.
A scheme is to split your script into chunks, so the differing parts is secluded, then to make version 1 you could