Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript: How to self-close AppleScript Editor window after execution of its own script?

How do I make AppleScript Editor window self-close (not quit AppleScript Editor) after executing its own script?


Version1 (won't work):

beep

tell application "AppleScript Editor"

close window 1

end tell


Version2 (won't work):

beep

tell me to close window 1


I get this error message:

The document can't be closed while the script is still running.


Thanks!

Posted on Apr 10, 2014 4:37 AM

Reply
11 replies

Apr 10, 2014 6:05 AM in response to red_menace

However, the following works.

beep

tell application "AppleScript Editor"

quit

end tell


The reason I want to close the active window instead of quit AppleScript Editor is that I have a multiple AppleScript Editor windows open. Quit the app will close all windows.


Is there a way to terminate the script process so that the window can self-close? It seems doable because the execution always happens prior to window close.


Thanks.

Apr 10, 2014 6:14 AM in response to Mr. Latte

It seems doable because the execution always happens prior to window close.



The script doesn't end until the last command has received a reply from the Apple Event Manager. That's why you can't do it.


However, if you have other windows open, you can close the window by sending the close command from another window.


i.e, from window 1,


tell my window 2 to close


will work

Apr 10, 2014 8:02 AM in response to Mr. Latte

Hello


You may try something like the script below.


Note that

a) you must call close_self() handler at the very end of your script, otherwise it won't work,

b) window of never-saved script won't close,

c) you may specify the close option – saving yes|no|ask – as parameter


Hope this may help,

H


display dialog "Good bye"
close_self({saving:yes})

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]
    tell application id "com.apple.ScriptEditor2"
        set wid to -1
        repeat with i in (get windows's id)
            set i to i's contents
            try
                if window id i's document's path = p2m then
                    set wid to i
                    exit repeat
                end if
            end try
        end repeat
        if wid = -1 then return
    end tell
    do shell script "sleep 0.5; /usr/bin/osascript <<'EOF' - " & wid & " " & savo & " &>/dev/null &
on run argv
    set {wid, savo} to argv
    set wid to wid as integer
    if savo = \"yes\" then
        set savo to yes
    else if savo = \"no\" then
        set savo to no
    else
        set savo to ask
    end if
    tell application id \"com.apple.ScriptEditor2\"
        close window id wid saving savo
    end tell
end run
EOF"
    (*
        [1] (path to me) for never-saved script is path to script editor.
            Because of this and the logic to determine the target window id, this handler won't close window of never-saved script.
    *)
end close_self


Message was edited by: Hiroto (change "unsaved" to "never-saved" in script comment)

Apr 10, 2014 11:21 AM in response to Mr. Latte

My pleasure. Glad to know it helps.


Here's some revisions. I noticed that sleep 0.5 as a guard interval in shell script is useless here because it is run in foreground and holds do shell script execution. Instead we'd need to use delay 0.5 in applescript run by osascript which is run in background. Corrected script is as follows.



display dialog "Good bye"
close_self({saving:yes})

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]
    tell application id "com.apple.ScriptEditor2"
        set wid to -1
        repeat with i in (get windows's id)
            set i to i's contents
            try
                if window id i's document's path = p2m then
                    set wid to i
                    exit repeat
                end if
            end try
        end repeat
        if wid = -1 then return
    end tell
    do shell script "/usr/bin/osascript <<'EOF' - " & wid & " " & savo & " &>/dev/null &
on run argv
    set {wid, savo} to argv
    set wid to wid as integer
    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\"
        close window id wid saving savo
    end tell
end run
EOF"
    (*
        [1] (path to me) for never-saved script is path to script editor.
            Because of this and the logic to determine the target window id, this handler won't close window of never-saved script.
    *)
end close_self



And indeed the code can be simplified as follows using document instead of window:


display dialog "Good bye"
close_self({saving:yes})

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


All the best,

H


Message was edited by: Hiroto (fixed the comment in the second script)

Apr 13, 2014 8:52 PM in response to Mr. Latte

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

AppleScript: How to self-close AppleScript Editor window after execution of its own script?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.