You can store a reference to the window you make in a variable, and use that variable to close it later. The following opens two windows (to Google and Yahoo), waits three seconds, the closes the Google window:
tell application "Google Chrome"
set windowGoog to make new window
tell windowGoog to open location "https://google.com"
set windowYah to make new window
tell windowYah to open location "http://Yahoo.com"
end tell
(*
You can do whatever you need to do here. I've added a three second delay just
to give a sense of time, but that's just for show.
*)
delay 3
tell application "Google Chrome"
close windowGoog
end tell
If you want to keep a reference to the window across multiple runs of the script (e.g., you run the script once to open the window, then run it again later to close it) make the variable a property
, and use if
statement to check if it has a value, like so:
(*
These two lines set 'windowGoog' and 'windowYah' to 'missing value' on the
first run, and then remember whatever value you set until the next time
you recompile the script
*)
property windowGoog : missing value
property windowYah : missing value
tell application "Google Chrome"
if windowGoog is missing value then
set windowGoog to make new window
tell windowGoog to open location "https://google.com"
else
close windowGoog
set windowGoog to missing value
end if
if windowYah is missing value then
set windowYah to make new window
tell windowYah to open location "http://Yahoo.com"
else
close windowYah
set windowYah to missing value
end if
end tell