Hello
You may use good old "load script" command or new "use" statement introduced in AppleScript 2.3 under 10.9.
Script library using "load script" command is something like this. Here it assumes lib.scptd is saved in ~/Library/Scripts.
-- caller script
set lib to load script POSIX file ((path to scripts folder from user domain)'s POSIX path & "lib.scptd") -- ~/Library/Scripts/lib.scptd
display dialog "Good bye"
lib's close_self({saving:yes})
-- lib.scptd
on close_self(opt)
(*
record opt : {saving: savo}
savo : (constant) yes | no | ask (default = ask)
* close window of this script in AppleScript editor
*)
set {saving:savo} to opt & {saving:ask}
set p2m to (path to me)'s POSIX path -- [1]
do shell script "/usr/bin/osascript <<'EOF' - " & p2m's quoted form & " " & savo & " &>/dev/null &
on run argv
set {p, savo} to argv
if savo = \"yes\" then
set savo to yes
else if savo = \"no\" then
set savo to no
else
set savo to ask
end if
delay 0.5
tell application id \"com.apple.ScriptEditor2\"
tell document 1 whose path = p
if exists then close saving savo
end tell
end tell
end run
EOF"
(*
[1] (path to me) for never-saved script is path to script editor.
Because of this, this handler won't close window of never-saved script.
*)
end close_self
If I'm not mistaken, script library using "use" statment would be something like this. (Not tested, for I'm using 10.6.8)
-- caller script
(*
OSX 10.9 or later (AppleScript 2.3 or later)
*)
use script "MyLib" -- [1]
use scripting additions -- [2]
(*
[1] MyLib.scpt or MyLib.scptd is searched in the following locations in this order and the first matching script is used:
Contents/Resources/Script Libraries/
~/Library/Script Libraries/
/Library/Script Libraries/
/Network/Library/Script Libraries/
/System/Library/Script Libraries/
[2] If you use any "use" statement, you need to specify "use script additions" explicitly in order to use scripting additions.
cf.
https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/
AppleScript Language Guide
> Script Objects
> Script Libraries
> Control Statements Reference
> use Statements
*)
display dialog "Good bye"
close_self({saving:yes})
-- MyLib.scptd
property name : "MyLib"
property id : "org.someone.mylib"
property version : "0.1.1"
on close_self(opt)
(*
record opt : {saving: savo}
savo : (constant) yes | no | ask (default = ask)
* close window of this script in AppleScript editor
*)
set {saving:savo} to opt & {saving:ask}
set p2m to (path to me)'s POSIX path -- [1]
do shell script "/usr/bin/osascript <<'EOF' - " & p2m's quoted form & " " & savo & " &>/dev/null &
on run argv
set {p, savo} to argv
if savo = \"yes\" then
set savo to yes
else if savo = \"no\" then
set savo to no
else
set savo to ask
end if
delay 0.5
tell application id \"com.apple.ScriptEditor2\"
tell document 1 whose path = p
if exists then close saving savo
end tell
end tell
end run
EOF"
(*
[1] (path to me) for never-saved script is path to script editor.
Because of this, this handler won't close window of never-saved script.
*)
end close_self
Good luck,
H