How to use a block code in a switch case? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Wpq
Posts: 14
Joined: 02 Mar 2021, 04:15
Location: France

How to use a block code in a switch case?

Post by Wpq » 04 Aug 2022, 04:06

I am trying to run a block of code as part of a switch case:

Code: Select all

#F9::
{
	Action := InputBox("action")
	switch Action {
		case "todo":
			{
				obsidianWindowName := "ahk_exe Obsidian.exe"
				 DetectHiddenText True
				if (WinExist(obsidianWindowName)) {
					WinActivate(obsidianWindowName)
				} else {
					Run "C:\Users\yop\AppData\Local\Obsidian\Obsidian.exe"
					Run "obsidian://open?vault=Obsidian&file=TODO.md"
				}
				WinWaitActive(obsidianWindowName)
			}
		default:
			Run "notepad"
	}
}
The idea is to pop up an input form and based on what is input various actions can be taken. Specifically in this snippet, a note-taking application is started when typing todo

Unfortunately when I type todo the default action is ran (Notepad opens). What am I missing?

Note: I tried to remove the block delimiters ({}) around the todo block but the result is the same

User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: How to use a block code in a switch case?

Post by boiler » 04 Aug 2022, 04:29

Can you post your InoutBox() function? The variable Action must not contain todo. You can demonstrate that yourself by running this simplified version of your script:

Code: Select all

Action := "todo"
	switch Action {
		case "todo":
			{
				Msgbox, todo
			}
		default:
			Msgbox, default
	}

The braces after the first case aren’t needed, but I left them in to show that’s not the issue.

Wpq
Posts: 14
Joined: 02 Mar 2021, 04:15
Location: France

Re: How to use a block code in a switch case?

Post by Wpq » 04 Aug 2022, 06:18

boiler wrote:
04 Aug 2022, 04:29
Can you post your InoutBox() function?
I am not sure I understand. You mean the InputBox() function? This is https://lexikos.github.io/v2/docs/commands/InputBox.htm
The variable Action must not contain todo. You can demonstrate that yourself by running this simplified version of your script:

Code: Select all

Action := "todo"
	switch Action {
		case "todo":
			{
				Msgbox, todo
			}
		default:
			Msgbox, default
	}

The braces after the first case aren’t needed, but I left them in to show that’s not the issue.
By "must not contain" do you mean "probably does not contain", or "it is forbidden that it contains"? (I guess the former)

User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: How to use a block code in a switch case?

Post by boiler » 04 Aug 2022, 06:31

I didn’t notice that you were posting v2 since you didn’t post in the v2 section of “Ask For Help” (topic moved).

Regarding the braces, it’s not that they must not contain, but they are superfluous (i.e., they add nothing). There are actually cases where it will cause error to add superfluous braces to your script, so don’t put them where they are not needed and add no value.

Wpq
Posts: 14
Joined: 02 Mar 2021, 04:15
Location: France

Re: How to use a block code in a switch case?  Topic is solved

Post by Wpq » 04 Aug 2022, 06:36

OK, got it. I just realized that the return value from InputBox is an OBJECT, so I have to use Action.Value

Wpq
Posts: 14
Joined: 02 Mar 2021, 04:15
Location: France

Re: How to use a block code in a switch case?

Post by Wpq » 04 Aug 2022, 06:41

boiler wrote:
04 Aug 2022, 06:31
I didn’t notice that you were posting v2 since you didn’t post in the v2 section of “Ask For Help” (topic moved).
Oh sorry, I did not realize that there was a split in the forum (which of course makes sense). Thanks for moving the topic.
Regarding the braces, it’s not that they must not contain, but they are superfluous (i.e., they add nothing). There are actually cases where it will cause error to add superfluous braces to your script, so don’t put them where they are not needed and add no value.
I was not talking about the braces but about your sentence "The variable Action must not contain todo".

I eventually found out that I was using the whole value returned by InputBox (which is an object), instead of just the Value property.

User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: How to use a block code in a switch case?

Post by boiler » 04 Aug 2022, 06:44

Wpq wrote:
04 Aug 2022, 06:41
I was not talking about the braces but about your sentence "The variable Action must not contain todo".

I eventually found out that I was using the whole value returned by InputBox (which is an object), instead of just the Value property.
Oh, right. By “must not contain”, I meant ”clearly does not contain”, and you have found the reason why it didn’t.

Post Reply

Return to “Ask for Help (v2)”