跳轉到內容

AppleScript 程式設計/示例程式/Safari 退出警告

來自華夏公益教科書,開放的書籍,開放的世界

注意:這個指令碼對於它的目的來說已經過時了,因為現在 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
下一頁: MS Word 2008 | 上一頁: 計算器
主頁: AppleScript 程式設計/示例程式
華夏公益教科書