Python Script to Open Browsers in Grid Layout with Confirmation
Here's a Python script that opens browsers in a grid layout (2x2) based on URLs from a text file, with confirmation popups between each set of four browsers:
import webbrowser
import time
import tkinter as tk
from tkinter import messagebox
import os
import sys
def open_browsers_in_grid(urls):
# Define screen positions (left, top, width, height)
positions = [
(0, 0, 960, 540), # Top-left
(960, 0, 960, 540), # Top-right
(0, 540, 960, 540), # Bottom-left
(960, 540, 960, 540) # Bottom-right
]
# Open browsers in grid positions
for i, url in enumerate(urls[:4]): # Only open up to 4 at a time
if i < len(positions):
try:
# For Windows
if sys.platform == 'win32':
os.system(f'start "" "{url}"')
# Alternative method for more control (requires Chrome)
# webbrowser.get('chrome').open(url, new=2)
# For macOS
elif sys.platform == 'darwin':
os.system(f'open -a "Google Chrome" --args --new-window "{url}" --window-position={positions[i][0]},{positions[i][1]} --window-size={positions[i][2]},{positions[i][3]}')
# For Linux
else:
os.system(f'google-chrome --new-window "{url}" --window-position={positions[i][0]},{positions[i][1]} --window-size={positions[i][2]},{positions[i][3]} &')
print(f"Opened {url} at position {i+1}")
except Exception as e:
print(f"Error opening {url}: {e}")
def show_confirmation():
root = tk.Tk()
root.withdraw() # Hide the main window
response = messagebox.askyesno(
"Continue?",
"Do you want to open the next set of browsers?"
)
root.destroy()
return response
def read_urls_from_file(filename):
with open(filename, 'r') as file:
urls = [line.strip() for line in file if line.strip()]
return urls
def main():
# Read URLs from text file
filename = input("Enter the path to the text file containing URLs: ").strip()
try:
urls = read_urls_from_file(filename)
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
return
except Exception as e:
print(f"Error reading file: {e}")
return
if not urls:
print("No URLs found in the file.")
return
print(f"Found {len(urls)} URLs in the file.")
# Process URLs in batches of 4
for i in range(0, len(urls), 4):
batch = urls[i:i+4]
print(f"\nOpening batch {i//4 + 1}:")
open_browsers_in_grid(batch)
# Wait a bit before showing confirmation
time.sleep(2)
# Show confirmation for next batch if there are more URLs
if i + 4 < len(urls):
if not show_confirmation():
print("Operation cancelled by user.")
break
print("\nAll URLs processed.")
if __name__ == "__main__":
main()
How to Use This Script:
- Create a text file with one URL per line (e.g.,
urls.txt).
- Run the script and provide the path to your text file when prompted.
- The script will open the first 4 URLs in a 2x2 grid layout.
- After opening each set of 4 browsers, it will show a confirmation popup asking if you want to continue with the next set.
- If you click "Yes", it will proceed with the next set of 4 URLs. If you click "No", it will stop.
Notes:
- This script works best with Chrome on Windows, macOS, and Linux.
- The exact positioning might vary depending on your screen resolution and operating system.
- You may need to adjust the window positions and sizes in the
positions list to match your screen.
- For Windows, the positioning might not work perfectly as Windows doesn't have direct command-line support for window positioning in all browsers.
- Make sure you have Chrome installed as the default browser for best results.
Alternative Approach:
If you need more precise control over window positioning, you might want to consider using a browser automation tool like Selenium, though that would require more complex setup.