--------------------------------------------------------------------------------
--
--  Licensed to the Apache Software Foundation (ASF) under one or more
--  contributor license agreements.  See the NOTICE file distributed with
--  this work for additional information regarding copyright ownership.
--  The ASF licenses this file to You under the Apache License, Version 2.0
--  (the "License"); you may not use this file except in compliance with
--  the License.  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
--
--------------------------------------------------------------------------------

-- tell Safari to close all windows that have the specified URL
tell application "Safari"
	-- 'closed' keeps track of whether we have closed any documents
	set closed to false

	set done to false
	repeat until done
		set done to true
		repeat with w in windows
			try
				repeat with t in tabs of w
					if URL of t is item 1 of argv then
						close t
						set closed to true

						-- since we have closed a document, we must restart the loop
						set done to false
						exit repeat
					end if
				end repeat
			end try
		end repeat
	end repeat

	-- if we closed at least one Safari window, and no more are
	-- open, then tell Safari to exit
	if closed and (count of documents) is 0 then		
		quit
		set closed to "appquit"
	end if

	-- return true if we closed at least one window, false if not
	closed
end tell
