컴퓨터 Tip

사파리 탭 보호하기(일정 개수 닫기 금지) : Preventing closing a tab on safari

achivenKakao 2020. 9. 15. 18:34

사파리는 크롬 보다 extension이 몇개 없다. 

대신에.. applescript를 제공하는데...(이딴거 필요 없고 그냥 extension을 풀라고...ㅅㅂ)

그것을 이용하면 tab을 원하는 개수보다 적을 경우 닫지 않게 할 수 있다.

_preventing_count 뒤에 숫자를 변경하면 탭 개수를 제한할 수 있다. (난 6개나 쓴다...)

그리고 탭이 6개 이하이면 hide 되도록 했습니다.(최소화와 비슷한 기능... 보통 command + H)

 

주석을 제거하면 팝업을 통해서 탭을 닫을지 선택하게 할 수 있는데..

맥 내부적으로 방지코드가 있는지 팝업이 뜨고 선택지를 하는데 딜레이 있다. 

그래서 저는 그냥 주석으로 해놨습니다.

그리고 저는 BetterTouchTool 을 통해서 commend + w를 이 script가 실행하도록 해놨다. 

동작은 잘 됩니다. 크롬, 사파리 두개를 올립니다.


tell application "Safari"
	set _window_count to count windows
	set _tab_count to 0
	set _preventing_count to 6
	
	repeat with _w in every window
		set _tab_count to _tab_count + (count tabs of _w)
	end repeat
	
	-- Make a string like "1 window containing 3 tabs."
	if _window_count is 1 then
		set _msg to _window_count & " window containing " as string
	else
		set _msg to _window_count & " windows containing " as string
	end if
	
	if _tab_count < _preventing_count then
		--		set _msg to _msg & _tab_count & " tab." as string
		--		display alert ¬
		--			"Are you sure you want to close tab?" message _msg ¬
		--			buttons {"Cancel", "Close tab"} ¬
		--			giving up after 10
		--		if button returned of result is "Close tab" then
		--			close current tab of front window without saving
		--		end if
		tell application "System Events" to set visible of process "Safari" to false
	else
		tell application "Safari"
			close current tab of front window without saving
		end tell
	end if
end tell

preventing tabs on chrome.scpt
0.01MB
preventing tabs on safari.scpt
0.01MB