Run this script on Cmd + W to prevent closing the last tab in Safari
tell application "Safari"
set numWin to count of tabs of window 1
if numWin > 1 then
close current tab of front window
end if
if numWin = 1 then
tell application "Safari" to activate
tell application "System Events"
keystroke "t" using command down
end tell
tell window 1 of application "Safari"
close (tabs where index < (get index of current tab))
end tell
end if
end tell