I’m closing an application, let’s say TextEdit, and I want it to tell it to quit, but if a save dialog pops up I want it to set a variable to true so I can run actions later in my script. The problem is I tried “ignoring application responses” but I have a horrible time figuring out if the save dialog pops up, and without ignoring responses the script hangs until TextEdit’s save dialog is dealt with. Is there some middle ground, some simple way around this? Thanks for the help!
Hi Jamie,
you can detect whether the saving sheet is present with this code
tell application "TextEdit" to activate
tell application "System Events"
tell process "TextEdit"
if (exists sheet 1 of window 1) then
-- do something or
-- click button "Cancel" of sheet 1 of window 1
end if
end tell
end tell
but you can also close a file without saving with this
tell application "TextEdit" to close document 1 saving no
If I use that I need a delay because with ignoring responses it moves on before TextEdit shoes a dialog. Is there a better way?
A delay routine can be like this:
tell application "TextEdit" to activate
tell application "System Events"
tell process "TextEdit"
set {t, flag} to {1, false}
repeat until t = 10 -- delay max. 5 sec
if (exists sheet 1 of window 1) then
set flag to true
exit repeat
end if
delay 0.5
set t to t + 1
end repeat
if flag then
-- do things when printing sheet exists
end if
end tell
end tell
That is a good way to do it, thanks very much. I’ve really learned a lot from this forum, it is so nice to hear all of the unique ideas people have on scripting techniques. Have a good weekend!