Last active
June 2, 2025 15:31
-
-
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
#!/usr/bin/env python | |
import tkinter as tk | |
from tkinter import messagebox, filedialog | |
import webbrowser | |
import sys | |
import re | |
def extract_urls(text): | |
# More comprehensive URL pattern that handles complex URLs | |
url_pattern = r'https?://[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&//=]*)' | |
return re.findall(url_pattern, text) | |
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: | |
content = file.read() | |
return extract_urls(content) | |
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 and extract URLs | |
text_content = text_box.get("1.0", tk.END) | |
links = extract_urls(text_content) | |
if links: | |
open_links(links) | |
else: | |
messagebox.showinfo("No Links", "No valid HTTP(S) links found in the text.") | |
def on_text_paste(event=None): | |
# Schedule the extraction to happen after the paste is complete | |
root.after(10, extract_links_from_textbox) | |
def extract_links_from_textbox(): | |
# Get text content after paste is completed | |
text_content = text_box.get("1.0", tk.END) | |
links = extract_urls(text_content) | |
if links: | |
text_box.delete("1.0", tk.END) # Clear the text box | |
text_box.insert(tk.END, "\n".join(links)) # Insert only the extracted links | |
# Create the main window | |
root = tk.Tk() | |
root.title("Link Extractor and Opener") | |
# Create a label | |
label = tk.Label(root, text="Paste text with links or enter URLs (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) | |
# Bind paste event | |
text_box.bind('<<Paste>>', on_text_paste) | |
# Create a button to parse links manually | |
parse_button = tk.Button(root, text="Parse Links", command=extract_links_from_textbox) | |
parse_button.pack(pady=5) | |
# Create a button to open links | |
open_button = tk.Button(root, text="Open Links", command=on_open_links) | |
open_button.pack(pady=5) | |
# 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=5) | |
# 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.
Updated the script so it parses the links out of text, such as an email.
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