Ok, so I’ve got this AutoHotkey snippet:
; Google text from any app
; from http://superuser.com/questions/7271/most-useful-autohotkey-scripts/165220#165220
#s::
MyClip := ClipboardAll
clipboard = ; empty the clipboard
Send, ^c
ClipWait, 2
if ErrorLevel ; ClipWait timed out.
return
Run http://www.google.com/#hl=en&q=%clipboard%
Clipboard := MyClip
It works fine, but I’d like to improve it to handle the case where you highlight a URL and automatically run the URL itself (Run %clipboard%) instead of searching Google for it. How can I get AutoHotkey to detect if a string is a URL?
Seems like I could use StringLeft or SubStr to extract the first few characters and see if they match http:// or www., or maybe something more robust using regular expressions? I don’t really understand AHK’s syntax, though.
This script checks the clipboard for URLs, apparently using StringGetPos, but I don’t want to detect if www appears anywhere in the string. Only if it appears at the beginning.
Here’s your script with code to check if the copied text is a URL via a regular expression. If it’s a URL it just runs the text, otherwise it uses the old behaviour of searching Google for the copied text.
Edit: Changed code to also match web addresses starting with just ‘www’.