why is it not checking for the PC name?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

why is it not checking for the PC name?

Post by newcod3r » 27 Dec 2023, 19:48

as per script below - it works on all my PCs instead of just Acer. Why is that so?

Code: Select all

if (A_ComputerName = "Acer")
{
msgbox, % A_ComputerName
^+n::
SendInput !hn
return
}

gregster
Posts: 9074
Joined: 30 Sep 2013, 06:48

Re: why is it not checking for the PC name?

Post by gregster » 27 Dec 2023, 19:53

See #If (mind the # which makes it a directive) for context-sensitive hotkeys.

User avatar
mikeyww
Posts: 27153
Joined: 09 Sep 2014, 18:38

Re: why is it not checking for the PC name?

Post by mikeyww » 27 Dec 2023, 19:54

If is not #If. Hotkeys defined by :: are never subject to If statements or control flow.

Read here: :arrow: #If.

EDIT: same as gregster's info here....

Example

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: why is it not checking for the PC name?

Post by newcod3r » 28 Dec 2023, 18:18

hi guys,

I added # and it still doesn't work though.. 😅

User avatar
mikeyww
Posts: 27153
Joined: 09 Sep 2014, 18:38

Re: why is it not checking for the PC name?

Post by mikeyww » 28 Dec 2023, 18:20

Show your code.

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: why is it not checking for the PC name?

Post by newcod3r » 28 Dec 2023, 20:34

mikeyww wrote:
28 Dec 2023, 18:20
Show your code.

Code: Select all

#If A_ComputerName = "Acer" 
{
msgbox, % A_ComputerName
^+n::
SendInput !hn
return
}


User avatar
mikeyww
Posts: 27153
Joined: 09 Sep 2014, 18:38

Re: why is it not checking for the PC name?

Post by mikeyww » 28 Dec 2023, 21:18

"Doesn't work" is neither a description of what happens nor a description of what does not happen. It is also not a description of what should happen.

1. What actually happens?

2. What should happen instead?

3. Does the hotkey trigger? How do you know?

4. What MsgBox is displayed?

5. The following post shows an example of how the #If directive can be used.

viewtopic.php?f=76&t=114488&p=509932#p509932

A couple of things are notable: first, the directive uses no braces. Second, the directive is used only with hotkeys or hotstrings.

Code: Select all

#Requires AutoHotkey v1.1.35
MsgBox % A_ComputerName

#If (A_ComputerName = A_ComputerName)
a::MsgBox
#If

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: why is it not checking for the PC name?

Post by newcod3r » 29 Dec 2023, 01:04

mikeyww wrote:
28 Dec 2023, 21:18
"Doesn't work" is neither a description of what happens nor a description of what does not happen. It is also not a description of what should happen.

1. What actually happens?

2. What should happen instead?

3. Does the hotkey trigger? How do you know?

4. What MsgBox is displayed?

5. The following post shows an example of how the #If directive can be used.

viewtopic.php?f=76&t=114488&p=509932#p509932

A couple of things are notable: first, the directive uses no braces. Second, the directive is used only with hotkeys or hotstrings.

Code: Select all

#Requires AutoHotkey v1.1.35
MsgBox % A_ComputerName

#If (A_ComputerName = A_ComputerName)
a::MsgBox
#If
the msgbox appears, which means it's working as it should, but after putting in my computer name, there's no msgbox, so it could be that I keyed in the wrong "computer name", which I referenced from This PC > Properties. after checking it doesn't appear to be the case too.

Code: Select all

msgbox, % A_ComputerName
#If (A_ComputerName = A_ComputerName)
a::MsgBox % A_ComputerName ;RYZEN5600 was displayed

Code: Select all

#If (A_ComputerName = RYZEN5600)
a::msgbox ; no msgbox comes out
Return

gregster
Posts: 9074
Joined: 30 Sep 2013, 06:48

Re: why is it not checking for the PC name?

Post by gregster » 29 Dec 2023, 04:13

newcod3r wrote:
29 Dec 2023, 01:04

Code: Select all

#If (A_ComputerName = RYZEN5600)
Like always in expressions, literal strings should be quoted (in contrast to variable names):

Code: Select all

#If (A_ComputerName = "RYZEN5600")

User avatar
mikeyww
Posts: 27153
Joined: 09 Sep 2014, 18:38

Re: why is it not checking for the PC name?

Post by mikeyww » 29 Dec 2023, 05:30

In follow-up to gregster: this issue pertains to the confusion surrounding legacy syntax, which I tend to avoid. The script here exemplifies the problem that you had with it. The documentation explains it. AHK v2 avoids this problem by eliminating the legacy syntax altogether.

Before you run the script, you can guess what it will do, and then see whether your guess is correct.

Code: Select all

#Requires AutoHotkey v1.1.35
f := "x"
If f = "x"
 MsgBox 1
If f = x
 MsgBox 2
If (f = "x")
 MsgBox 3
If (f = x)
 MsgBox 4
MsgBox 64, Status, Done!

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

Re: why is it not checking for the PC name?

Post by boiler » 29 Dec 2023, 07:04

And it is worth noting that the #If directive will behave the same whether or not there are parentheses around the condition because it is always expecting an expression, unlike If, which in the examples shown would invoke the expression version of If when parentheses are used and the legacy version of If when they are not.

That is to say that, unlike with If, this:

Code: Select all

#If A_ComputerName = "RYZEN5600"
…is the equivalent of this:

Code: Select all

#If (A_ComputerName = "RYZEN5600")
…and removing the parentheses does not make the following correct:

Code: Select all

#If A_ComputerName = RYZEN5600
(unless RYZEN5600 happened to be a variable name containing the desired value, which is not the case here)

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: why is it not checking for the PC name?

Post by newcod3r » 29 Dec 2023, 20:37

gregster wrote:
29 Dec 2023, 04:13
newcod3r wrote:
29 Dec 2023, 01:04

Code: Select all

#If (A_ComputerName = RYZEN5600)
Like always in expressions, literal strings should be quoted (in contrast to variable names):

Code: Select all

#If (A_ComputerName = "RYZEN5600")
then in fact my original code was correct all along? but I still can't tell why it's not activating properly.

User avatar
mikeyww
Posts: 27153
Joined: 09 Sep 2014, 18:38

Re: why is it not checking for the PC name?

Post by mikeyww » 29 Dec 2023, 20:55

You have a test script that works. Change only one thing at a time, and retest. You will then know exactly when the script stops working, and what stopped it.

Post Reply

Return to “Ask for Help (v1)”