Last active
April 24, 2025 11:55
-
-
Save svandragt/5cae29a6d27e7a7f2b45c12a7bfffc04 to your computer and use it in GitHub Desktop.
Open text links in browser tabs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
from tkinter import messagebox, filedialog | |
import webbrowser | |
import sys | |
def open_links(links): | |
# Open each link in the default web browser | |
for link in links: | |
if link: # Check if the link is not empty | |
webbrowser.open(link) | |
# Show a message box when done | |
messagebox.showinfo("Done", "All links have been opened.") | |
def load_links_from_file(file_path): | |
with open(file_path, 'r') as file: | |
return file.read().strip().splitlines() | |
def on_open_file(): | |
file_path = filedialog.askopenfilename(title="Select a Text File", filetypes=[("Text Files", "*.txt")]) | |
if file_path: | |
links = load_links_from_file(file_path) | |
text_box.delete("1.0", tk.END) # Clear the text box | |
text_box.insert(tk.END, "\n".join(links)) # Insert links into the text box | |
def on_open_links(): | |
# Get the input from the text box | |
links = text_box.get("1.0", tk.END).strip().splitlines() | |
open_links(links) | |
# Create the main window | |
root = tk.Tk() | |
root.title("Link Opener") | |
# Create a label | |
label = tk.Label(root, text="Enter links (one per line):") | |
label.pack(pady=10) | |
# Create a text box for user input | |
text_box = tk.Text(root, width=50, height=15) | |
text_box.pack(pady=10) | |
# Create a button to open links | |
open_button = tk.Button(root, text="Open Links", command=on_open_links) | |
open_button.pack(pady=10) | |
# Create a button to load links from a file | |
load_button = tk.Button(root, text="Load Links from File", command=on_open_file) | |
load_button.pack(pady=10) | |
# Check for command line arguments | |
if len(sys.argv) > 1: | |
file_path = sys.argv[1] | |
try: | |
links = load_links_from_file(file_path) | |
text_box.insert(tk.END, "\n".join(links)) # Insert links into the text box | |
except Exception as e: | |
messagebox.showerror("Error", f"Could not load file: {e}") | |
# Start the GUI event loop | |
root.mainloop() |
The code is AI generated. Fafi is my side project. vandragt.com is my personal website.
Updated it to load links from a text file either via the shell or with the file picker.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Screen.recording-2025-04-24_10.45.09.mp4