When I first start scripting an application the first thing I do is create a Temp_Script for that app and a script to open the Temp_Script in Script Debugger.
I've done this by hand for years, but I had to do it about six times in a row recently – got fed up – and wrote a script to do it for me.
A template script is required (I'm using a script bundle).
The scripts will be named using the name of the front Finder window, and they will be duplicated to that directory.
It should be easy enough to adapt to the Script Editor app.
(I use the shell in the Open_Temp_Script script, because Script Debugger doesn't always behave properly when scripted directly if the script is already open. I had a reason for not using the Finder to do it too, but I've forgotten what it was.)
-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/10/20 03:00
# dMod: 2015/10/20 04:11
# Appl: Finder & Script Debugger
# Task: Make Temp_Scripts for the given App's open script folder (frontmost in Finder).
# Libs: None
# Osax: None
# Tags: @Applescript, @Make, @Temp, @Script
# Note: Tested only on OSX 10.11.
-------------------------------------------------------------------------------------------
--» PROPERTIES
-------------------------------------------------------------------------------------------
property expandedPathTo : {}
property pathToSpec : {application support, applications folder, desktop, desktop pictures folder, documents folder, downloads folder, favorites folder, Folder Action scripts, fonts, help, home folder, internet plugins, keychain folder, library folder, modem scripts, movies folder, music folder, pictures folder, preferences, printer descriptions, public folder, scripting additions folder, scripts folder, shared documents, shared libraries, sites folder, startup disk, startup items, system folder, system preferences, temporary items, trash, users folder, utilities folder, workflows folder}
-------------------------------------------------------------------------------------------
try
set scriptBundleWithLibs to alias ((path to application support from user domain as text) & "Script_Support:New_File_Here!:Script-Bundle-with-Libraries.scptd:")
tell application "Finder"
set insertionLocation to insertion location
set folderName to name of insertionLocation
set newTempScript to duplicate scriptBundleWithLibs to insertionLocation
set newTempScript to newTempScript as alias
set name of newTempScript to "Temp_Script ⇢ " & folderName & ".scptd"
set newOpenTempScript to duplicate scriptBundleWithLibs to insertionLocation
set newOpenTempScript to newOpenTempScript as alias
set name of newOpenTempScript to "Open ⇢ Script ⇢ 'Temp_Script ⇢ " & folderName & ".scptd'.scptd"
end tell
if expandedPathTo = {} then expandedPathToPaths()
set scriptPath to makeRelativeAlias(newTempScript)
set openScriptText to "
-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# Appl: Script Debugger & Shell
# dCre: " & (strftime (get current date) into "%Y/%m/%d %H:%M") & "
# dMod: " & (strftime (get current date) into "%Y/%m/%d %H:%M") & "
# Task: Open the " & folderName & " temp script in Script Debugger.
# Libs: ELb, FLb, GLb, NLb, NLbD
# Osax: None
# Tags: @Applescript, @Script, @Open, @Temp, @Script, @" & folderName & "
-------------------------------------------------------------------------------------------
try
set scriptFile to " & scriptPath & "
do shell script \"open -a 'Script Debugger' \" & quoted form of scriptFile
on error e number n
stdErr(e, n, true, true) of me
end try
-------------------------------------------------------------------------------------------
"
tell application "Script Debugger"
set newTempScript to open newTempScript
set bounds of front window to {606, 23, 1920, 1196}
tell newTempScript to close with saving
set newOpenTempScript to open newOpenTempScript
set bounds of front window to {606, 23, 1920, 1196}
tell newOpenTempScript
set its source text to text 2 thru -1 of openScriptText
close it with saving
end tell
end tell
on error e number n
stdErr(e, n, true, true) of me
end try
------------------------------------------------------------------------------------------------
--» HANLDERS
-------------------------------------------------------------------------------------------
on expandedPathToPaths()
repeat with i in pathToSpec
set end of expandedPathTo to (path to (get contents of i) from user domain) as text
end repeat
end expandedPathToPaths
-------------------------------------------------------------------------------------------
on makeRelativeAlias(_item)
set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ":"}
set _item to _item as text
set _len to (length of (text items of _item))
repeat with i from _len to 1 by -1
set _path to ((text items 1 thru i of _item) as text) & ":"
if _path is in expandedPathTo then
repeat with idx from 1 to (length of expandedPathTo)
if (item idx of expandedPathTo) = _path then
set refFldr to "path to " & (item idx of pathToSpec) as text
set diskLocalRef to ("posix path of " & ("(" & "(" & refFldr & " as text) & ") & "\"" & (text items (i + 1) thru -1 of _item) as text) & "\")"
try
run script diskLocalRef
on error
set refFldr to refFldr & " from user domain"
set diskLocalRef to ("posix path of " & ("(" & "(" & refFldr & " as text) & ") & "\"" & (text items (i + 1) thru -1 of _item) as text) & "\")"
try
run script diskLocalRef
on error e
error e
end try
end try
set AppleScript's text item delimiters to oldTIDS
return diskLocalRef
end if
end repeat
end if
end repeat
end makeRelativeAlias
-------------------------------------------------------------------------------------------
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
-------------------------------------------------------------------------------------------