[Solved] What I need to use instead of "if (ErrorLevel = 0)"?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

[Solved] What I need to use instead of "if (ErrorLevel = 0)"?

17 Jul 2022, 05:06

In AHK 1.1, there was

Code: Select all

if (ErrorLevel = 0) {
  ...
}
It seems v2 doesn't have ErrorLevel, so how I should change the snippet above?

The real code:

Code: Select all

; For ahk v1.1. Need to make it work for ahk v2
CopyToPseudoClipboard(ClipWaitTimeout := "") {
    OriginalClipboard := ClipboardAll
    Clipboard := ""
    Send ^c

    ClipWait ClipWaitTimeout
    If (ErrorLevel == 0)
        PseudoClipboard := Clipboard

    Clipboard := OriginalClipboard
    Return PseudoClipboard
}
Last edited by john_c on 19 Jul 2022, 07:44, edited 1 time in total.
User avatar
boiler
Posts: 17399
Joined: 21 Dec 2014, 02:44

Re: What I need to use instead of "if (ErrorLevel = 0)"?

17 Jul 2022, 06:26

The ClipWait documentation is a good place to start, especially the example.
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: What I need to use instead of "if (ErrorLevel = 0)"?

17 Jul 2022, 15:18

The v1 to v2 convertor would also have given you the answer, the only thing that you need to add is to set the var PseudoClipboard to empty if ErrorLevel is not 0

https://github.com/mmikeww/AHK-v2-script-converter

Code: Select all

CopyToPseudoClipboard(ClipWaitTimeout := "") {
    OriginalClipboard := ClipboardAll()
    A_Clipboard := ""
    Send("^c")

    Errorlevel := !ClipWait(ClipWaitTimeout)
    If (ErrorLevel == 0)
        PseudoClipboard := A_Clipboard
    Else{
        PseudoClipboard := "" ; added manually
    }

    A_Clipboard := OriginalClipboard
    Return PseudoClipboard
}
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: What I need to use instead of "if (ErrorLevel = 0)"?

17 Jul 2022, 22:47

In general, instead of

Code: Select all

if (ErrorLevel = 0) {
  ...
}
you use Because if an error occurred, your code wouldn't be executing!

In all seriousness, if your code handled the case where ErrorLevel indicates an error occurred, usually you would replace it with try-catch (and can do so in v1 as well).

However, there are some cases where ErrorLevel was misused to return a value, rather than reporting an error. ClipWait is one of those. In those cases, you now use the return value like you would any other function.
Errorlevel := !ClipWait(ClipWaitTimeout)
If (ErrorLevel == 0)
This is a bad suggestion, even if it is the result of machine translation. If you are going to use a variable, use a more appropriate name.

As per the example in the documentation, you can just use ClipWait(ClipWaitTimeout) directly in the if statement. The example uses !Clipwait() because to detect when the function times out, but to replace If (ErrorLevel == 0), you can just use If ClipWait(ClipWaitTimeout).

This difference of behaviour is also specifically documented under Changes from v1.1.
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: What I need to use instead of "if (ErrorLevel = 0)"?

18 Jul 2022, 05:34

Lexikos is of course right.

The code above was just generated by the convertor to a working code, It is adviced to optimise the code afther conversion. In this case there is no need to have a variable.

The convertor uses the varable ErrorLevel because it is later used in the code.

Code: Select all

CopyToPseudoClipboard(ClipWaitTimeout := "") {
    OriginalClipboard := ClipboardAll()
    A_Clipboard := ""
    Send("^c")

    If (ClipWait(ClipWaitTimeout) != 0)
        PseudoClipboard := A_Clipboard
    Else{
        PseudoClipboard := ""
    }

    A_Clipboard := OriginalClipboard
    Return PseudoClipboard
}
sirksel
Posts: 224
Joined: 12 Nov 2013, 23:48

Re: [Solved] What I need to use instead of "if (ErrorLevel = 0)"?

23 Jul 2022, 23:00

If you're interested in doing more than directly translating from v1 to v2, you could even factor out the clipboard backup/restore wrapper code into its own function, like this:

Code: Select all

WithPseudoClipboard(ClipAction, p*) {
    OriginalClipboard := ClipboardAll()
    PseudoClipboard := ClipAction(p*)
    A_Clipboard := OriginalClipboard
    Return PseudoClipboard
}
Then you can reuse this same code for copy (^c) or cut (^x) or maybe some other hotkeys that temporarily need to use the clipboard. You just pass in a function object as ClipAction that provides the "middle part" of your code, and this wrapper calls it in between the appropriate setup and teardown code. Things like passing functions and fat arrow notation are where v2 really makes things better (at least for me).

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: just me and 51 guests