AppleScript 编程/示例程序/Safari 退出警告
外观
< AppleScript 编程 | 示例程序
注意:这个脚本对于它的目的来说已经过时了,因为现在 Safari 在退出时,如果有多个窗口/标签打开,会提示用户确认。但由于代码本身有效,可以作为一般示例保留。
这与 Firefox 在你尝试使用多个窗口/标签打开浏览器时弹出的警报框非常相似。
我使用外部工具 (iKey) 强制这个脚本在 Safari 是活动应用程序时按 cmd+Q 键时运行。我只是讨厌当我不小心按错它而不是 cmd+W 或 opt+Q 时。
(*
I made the code ignore window "Downloads", as it is relatively
irrelevant, especially since Safari already alerts if you got downloads
going. Plus, if you once open that window, Safari will count it open
even when you close it - until you quit Safari.
If you don't use English as OS language, then change it to represent
the window name in the language you use - in variable setting just
right after this note ends - if you want to (not necessary if you
don't care what counts the alert gives).
Enjoy your browsing. :)
F-3000
*)
set wDowns to "Downloads"
tell application "Safari"
set wCounter to 0 -- Window Counter
set bCounter to 0 -- Button Counter
set wList to name of every window
repeat with vWindow in wList
-- Counts the amount of windows
set wCounter to wCounter + 1
(* Counts the amount of tabs.
I did put it into try-block so that "Downloads"-window wont
mess this script up, since it doesn't have "group 3"-item.
Luckily - regarding easiness of tab-counting. *)
try
tell application "System Events" to tell process "Safari" to set bCounter to ¬
bCounter + (count (every button in group 3 of window vWindow))
end try
end repeat
-- Causes "Downloads"-window to be ignored in counting
if wList contains wDowns then set wCounter to wCounter - 1
if wCounter > 0 then
-- If there is more than 1 window open...
display dialog "Do you really want to quit Safari?" & ¬
return & "You have " & wCounter & " windows and " & bCounter & " tabs open." buttons ¬
{"Yes yes yes!", "No, forget it"} default button 2 with icon stop with title "Oops!"
if button returned of result is "Yes yes yes!" then quit
else
-- If no windows are open...
quit
end if
end tell