Help with Functions and Variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jp7
Posts: 5
Joined: 16 Oct 2018, 19:55

Help with Functions and Variables

16 Jun 2021, 08:42

First off, please forgive my ignorance: I use AHK all the time, but I have no background understanding of programming. I have collected scripts developed by others (and tweaked them to my needs) for my work as a translator, and now that I have a little time I'd like to clean up my master script and become a bit more proficient.*

Currently I use several variations of this script to search a selected word or phrase (in any program) on various websites (google, dictionaries, specialized websites, etc. etc.).

*I currently rewrite the entire script for each website.* But I know that there is a way to write it out once only, as a function?, and use variables? for the command and the website searched. That's what I would like to achieve.

So:

Code: Select all

#+g::  > search http www.google.ca /  Broken Link for safety
#+w:: > search https www.wordreference.com /fren/  Broken Link for safety
#+t:: > search https www.btb.termiumplus.gc.ca /tpv2alpha/alpha-eng.html?lang=eng  Broken Link for safety
(there are more, but that's the idea.)

ANY HELP APPRECIATED! THANKS!

Here is the search script I'm currently using:

Code: Select all

#+g::
;Copy Clipboard to prevClipboard variable, clear Clipboard.
prevClipboard := ClipboardAll
Clipboard =
;Copy current selection, continue if no errors.
SendInput, ^c
ClipWait, 2
if !(ErrorLevel) {
;Convert Clipboard to text, auto-trim leading and trailing spaces and tabs.
Clipboard = %Clipboard%
;Clean Clipboard: change carriage returns to spaces, change >=1 consecutive spaces to +
Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "\s+","+")
;Open URLs, Google non-URLs. URLs contain . but do not contain + or .. or @
if Clipboard contains +,..,@
Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" http www.google.ca /  Broken Link for safetysearch?q=%Clipboard%
else if Clipboard not contains .
Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" http www.google.ca /  Broken Link for safetysearch?q=%Clipboard%
else
Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %Clipboard%
}
;Restore Clipboard, clear prevClipboard variable.
Clipboard := prevClipboard
prevClipboard =
return
[Mod edit: [code][/code] tags added.]
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Help with Functions and Variables

16 Jun 2021, 08:55

i dont know what ur exact URLs are since the forum mangled them(possibly ull have to fix them up some more urself)
u extract the common bits into a function and parametrize on the leftover changing bits

Code: Select all

#+g::search("http://www.google.ca/")
#+w::search("https://www.wordreference.com/fren/")
#+t::search("https://www.btb.termiumplus.gc.ca/tpv2alpha/alpha-eng.html?lang=eng")

search(baseURL) {
	;Copy Clipboard to prevClipboard variable, clear Clipboard.
	prevClipboard := ClipboardAll
	Clipboard =
	;Copy current selection, continue if no errors.
	SendInput, ^c
	ClipWait, 2
	if !(ErrorLevel)
	{
		;Convert Clipboard to text, auto-trim leading and trailing spaces and tabs.
		Clipboard = %Clipboard%
		;Clean Clipboard: change carriage returns to spaces, change >=1 consecutive spaces to +
		Clipboard := RegExReplace(RegExReplace(Clipboard, "\r?\n"," "), "\s+","+")
		;Open URLs, Google non-URLs. URLs contain . but do not contain + or .. or @
		if Clipboard contains +,..,@
			Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %baseURL%search?q=%Clipboard%
		else if Clipboard not contains .
			Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %baseURL%search?q=%Clipboard%
		else
			Run, "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %Clipboard%
	}
	;Restore Clipboard, clear prevClipboard variable.
	Clipboard := prevClipboard
}
jp7
Posts: 5
Joined: 16 Oct 2018, 19:55

Re: Help with Functions and Variables

16 Jun 2021, 09:42

Thanks so much @swagfag ! :D

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 73 guests