One common task where I spent years using a clunky keyboard automation solution as part of my scripts to fetch the URL from the active tab in Chrome.
This is a much cleaner solution using AppleScript and Python.
import subprocess
def get_chrome_active_tab_url():
try:
script = '''
tell application "Google Chrome"
set activeTabUrl to URL of active tab of front window
return activeTabUrl
end tell
'''
result = subprocess.run(['osascript', '-e', script], capture_output=True, text=True)
url = result.stdout.strip()
print(f"Active tab URL: {url}")
return url
except Exception as e:
print(f"Error: {e}")
return None
# Run the function
get_chrome_active_tab_url()