Running Code with Different Conditions Specified after Initial Hotstring

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
WizardHacker
Posts: 4
Joined: 18 Jul 2018, 12:14

Running Code with Different Conditions Specified after Initial Hotstring

18 Jul 2018, 12:37

I'm currently trying to create a script that would run certain condition after a initial hotstring. For example, the hotstring could be (FYI: In these examples, I will be using Chrome and Mozilla):

Typing "runbrowser" in any txt document and it would run a certain browser. To choose which browser it would run, (this is the part that I'm trying to accomplish :P ), you would type something like "runbrowser:chrome" or "runbrowserchrome"

The trick is, I'm trying to accomplsih this with only one hotstring to be checked, not two, such as two seperate hotstrings: runbrowsermozilla & runbrowserchrome.

In conlusion, it should probably look something like this:

Code: Select all

:*:runbrowser:: (ADD CODE TO CHECK ANY FOLLOWING CONDITIONS: such as chrome or mozilla)

if mozilla (CHECK CONDITIONS HERE)
(RUN MOZILLA)
else if chrome (CHECK CONDITION IF PREVIOUS CONDITION IS NOT MET)
(RUN CHROME)
[etc...]

AND NOT...

:*:runbrowsermozilla::
(RUN MOZILLA)

:*:runbrowserchrome::
(RUN MOZILLA)
Feel free to also leave suggestions for different ways to accomplish the same thing without using this specific hotstring syntax or any other way to quickly launch this program WITHOUT USING A HOTKEY OR HOTKEY COMBINATION.

Thank you so much :oops: :D,
WizardHacker
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Running Code with Different Conditions Specified after Initial Hotstring

18 Jul 2018, 13:05

Well, this is still using two hotstrings, but this is rather short:

Code: Select all

:*:runbrowsermozilla::
:*:runbrowserchrome::
cmd := A_thishotkey = ":*:runbrowsermozilla" ? "firefox.exe" : "chrome.exe"
msgbox run %cmd%		; remove msgbox to actually run the program (you might need to add the actual file paths)
return
I don't see why using two hotstrings should be bad, anyway. But if you are actually interested in more flexible hotstrings (without defining them all explicitly), you should look into user scripts and functions like:
https://autohotkey.com/board/topic/9872 ... otstrings/
https://autohotkey.com/board/topic/1147 ... otstrings/
etc.

I have never used these, so I don't know how reliable they are...

Another way could be to combine a :*:runbrowser:: hotstring subroutine with the Input command... :think:
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Running Code with Different Conditions Specified after Initial Hotstring

18 Jul 2018, 13:26

To get back to my last idea - perhaps something like this (options like trigger key(s) can be tweaked, backspacing - depending on the typed string length - could be added):

Code: Select all

:*B0:run::
Input, cmd, V, {Space}
If (cmd = "mozilla")
	cmd := "firefox.exe" 
else if (cmd = "chrome")
	cmd:= "chrome.exe"
else 
	return
msgbox run %cmd%		 ; remove msgbox to actually run the program (you might need to add the actual file paths)
return
Type either runmozilla or runchrome and then Space.

With backspacing... of course without the V and B0 options, this wouldn't be necessary - depends on if you want visual feedback for the typing...

Code: Select all

:*B0:run::
Input, cmd, V, {Space}
Length := StrLen(cmd) + 4	; run + {Space} = 4
If (cmd = "mozilla")
	cmd := "firefox.exe" 
else if (cmd = "chrome")
	cmd := "chrome.exe"
else 
	return
SendInput {BS %length%}
msgbox run %cmd%		 ; remove msgbox to actually run the program (you might need to add the actual file paths)
return
This is only superficially tested :)

Edited multiple times
Last edited by gregster on 25 Jul 2018, 11:10, edited 1 time in total.
WizardHacker
Posts: 4
Joined: 18 Jul 2018, 12:14

Re: Running Code with Different Conditions Specified after Initial Hotstring

19 Jul 2018, 14:12

Thank you so much! This looks exactly about something I wanted! Just to clarify, what does the B0 do before the imitating hot string? (Sorry, I’m kinda new to AHK) And also, for your second example you would type run (a space) and then one of the browsers that are included, right? (Please help me with the order of events..) Thanks anyways! I will definitely try it tonight!
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Running Code with Different Conditions Specified after Initial Hotstring

19 Jul 2018, 15:10

You can find B0 in the Hotstrings 'Options' section:
B0 (B followed by a zero): Automatic backspacing is not done to erase the abbreviation you type.
Without it, it would erase the string run before you can type which browser you want (The V option for Input will also give visual feedback for your typing of the second part, after the run). It shouldn't change how the trigger works, though. It's just for visual feedback (if you want it).
With B0 you will see the typed string runchrome, for example, before the whole string will be erased (or not, depending on which example you use) and the action will be triggered. And of course, you wouldn't be able to use the word run anymore in other contexts, because it would (almost) always be erased without B0.
And also, for your second example you would type run (a space) and then one of the browsers that are included, right? (Please help me with the order of events..)
Actually no, I meant it like I wrote it. At the moment, you can type (literally) runchrome or runmozilla and then a space to trigger an action. Typing run and a space would do nothing special (at least in my superficial testing; this is the else ... return part). Of course, this could be changed... well, it probably depends what you want. :D In your initial post, you seemed to prefer it as one word...

Well, the whole thing will need some testing anyway. I don't use such kinds of dynamic hotstrings yet (but I see some use cases), and put it together ad hoc.
WizardHacker
Posts: 4
Joined: 18 Jul 2018, 12:14

Re: Running Code with Different Conditions Specified after Initial Hotstring

25 Jul 2018, 10:39

I tested out the bits of code you provided: Even with leaving the msgbox command, it still didn’t seem to work for me. I tried to mess around with the code but couldn’t figure out a working version of the code. If you could just help make a working version, that would be awesome!

Thank you again!
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Running Code with Different Conditions Specified after Initial Hotstring

25 Jul 2018, 11:03

What does that mean? What is happening? What didn't work? What did you type? Didn't you see the msgboxes? Or, didn't it run the browsers (after removing the msgbox command)...? Like I wrote, you might need to add the actual file paths to your browsers (not in my case, though). In which contexts did you actually try them? How about in notepad?
If, by any chance, you are using the hotstrings in a program with elevated rights, you will need to run the AHK script as admin.

So, about what version of the code are we talking and what is not working? It is well possible that they need some tweaking - but that they didn't work at all, seems very strange, because they all work here... just in case: by 'removing the msgbox', I mean replace msgbox run %cmd% by run %cmd%

I just switched two lines (in the script with the backspacing, but this shouldn't affect the general working of things - it should just make the backspacing part more reliable)
WizardHacker
Posts: 4
Joined: 18 Jul 2018, 12:14

Re: Running Code with Different Conditions Specified after Initial Hotstring

25 Aug 2018, 00:40

Sorry for taking so long to respond :oops:! Wow, I've actually had to redo most of this post because once I'd type runchrome and a space afterward it worked!. Thank you! I'm sorry, I don't know what happened before!

Also what option do you use to highlight words and certain lines, like you did for runchrome and runmozilla? I tried to figure it out but I couldn't find it! Thank you again!
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: Running Code with Different Conditions Specified after Initial Hotstring

25 Aug 2018, 01:14

Glad it worked!
WizardHacker wrote:Also what option do you use to highlight words and certain lines, like you did for runchrome and runmozilla? I tried to figure it out but I couldn't find it! Thank you again!
If you create a post here and click Full Editor & Preview, you will find some buttons above the text field to insert some tags that will create bold text, or that will allow to choose text size and color etc.

There is also one button labeled just c which will insert tags to create these little boxes around the words that you write in between these tags... but it is not 'what you see is what you get', it's more like if you edit html. You can click Preview to see the effect of the tags, though, before actually posting... I hope that helps!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Giresharu, Google [Bot], inseption86, jomaweb, KruschenZ, mikeyww, Rohwedder, Swiftly9767 and 298 guests