how to quit a script?

hi there,

seems silly, but i have a fundamental problem.
i wrote a program that can only run as a script in Script Editor.
How do i quit it?
I know i can use return in the main code.
however i would like to quit the script from a sub-routine.
i only want to quit the script, not the Script Editor application itself.
how?

thank you!
Oliver.

Hi Oliver,

you can target the script itself with the word “me”

tell me to quit

Thanks Stephan,

that’s what I thought,
so something like this?

my quitme()
on quitme()
tell me to quit
end quitme

however this quits the APPLICATION Script Editor,
however I just want to quit the script itself (keeping the script open).

Do you get my problem?

Thank you, Oliver.

This is a contradiction in terms, you can not quit the script keeping it open.
The point is, if Script Editor is open, the “me” statement refers to the application Script Editor.
If you save your script as an application or run it from a menu, the “me” statement
refer to the script itself

Understood. Let’s say then, i want to exit the script. have it stop running. baut stay open. Thanks Stefan!

I think you just want to stop the script while it’s running in the Script Editor. If so, then:


tell application "System Events" to tell process "Script Editor.app" to keystroke "." using command down
delay 0.5
display dialog "I never get here, because I stopped."

Thank you!
I had thought of exactly the same but found it a bit silly.
How odd! and i thought, there must be a neat way to simply interrupt the execution of a script.

so yes, this works:

tell application “iTunes”
display dialog “now quit”
my quitme()
display dialog “didn’t work” – will not show
end tell

on quitme()
tell application “System Events” to set frontmost of process “Script Editor” to true
tell application “System Events” to keystroke “.” using command down
delay 0.5
end quitme

however, at times I get an error message like : “System Events got an error: User canceled out of wait loop for reply or receipt.”
not very nice
and I won’t be able to switch back to iTunes

Is there no neater way to do this? without leaving iTunes?

Oliver.

Then use simply return from the top level, not within a subroutine.

PS: The quit command is only useful in a script saved as a stay open application

hi Stefan,

understood. Seems the only way. Just thought there must be an easier way to which i don’t know the god-damn syntax.
Your approach is hardly feasable in my case because
a) There are many points within my script where I would want to end the script
b) i would want to end the script from within subroutines (that might be called by other subroutines)

i’ll do it like this:

–code
–code
if my quitme() then return
– code
on quitme()
– do things
return true
end quitme

If you saw my code you would understand…
Thank you both - Stefan and Adam for your immediat help!
Oliver

you can also try to use this, when you call the script from a menu

--code
--code
quit me
-- code
on quit
-- do things
    continue quit
end quit

here the best solution i found to the problem (however it still won’t work for me):

tell application “iTunes”
display dialog “quit now”
my quitme()
end tell
on quitme()
try
with timeout of 0.1 seconds
tell application “iTunes” to display dialog “quitting now.” default button 1
end timeout
end try
tell application “System Events” to keystroke return
end quitme

it works as is, however in my script it doesn’t. quitme() returns to its caller → the script continues anyway. Donno why…
Anyhow, maybe the above helps somebody else
regards,
Oliver

Hi groovy,

Use ‘error number -128’.

gl,

:):):slight_smile:
Hi Kel,
that’s the one!
Thank you sooooo much.
Oliver.