Skip to content

get_clipboard() drops leading plus sign ('+') if clipboard contents is a valid number #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
e-r-n-i-e opened this issue Jan 18, 2023 · 1 comment

Comments

@e-r-n-i-e
Copy link

Phone numbers usually look like this:

  • +49-1234-56 78 90
  • +491234567890

Problem: When you copy the second number to the clipboard (manually or programmatically) and afterwards try to receive it with get_clipboard(), you will only get 491234567890 instead of +491234567890 (spot the missing +). The first number is copied as-is.

You might find the following examples useful.

BUG_clipboard.py:

import ahkpy as ahk


@ahk.hotkey('^p')
def print_clipboard():
    print(ahk.get_clipboard())


def test_clipboard(test_str: str):
    print('----------')

    ahk.set_clipboard(test_str)
    ahk.sleep(.5)
    clipboard = ahk.get_clipboard()

    if clipboard == test_str:
        print("OK:", test_str)
    else:
        print("ERROR:")
        print(test_str, "!=")
        print(clipboard, type(clipboard))


# Failing:
test_clipboard('+491234567890')
test_clipboard(' +491234567890 ')
test_clipboard('+49.1234567890')

# OK:
test_clipboard('+49.1234.567890')
test_clipboard('+49-1234-56 78 90')
test_clipboard('+49 1234 56 78 90')
test_clipboard('-491234567890')
test_clipboard('Phone: +491234567890')
test_clipboard('+491234567890 x')

It looks like as if somewhere on it's way from AHK to Python the clipboard content get's interpreted as a number (if possible) and as consequence looses it's plus sign.

The following Autohotkey script works fine:

phone := "+491234567890"

clipboard := phone

MsgBox % clipboard
@Perlence
Copy link
Owner

Hello @e-r-n-i-e, thank you for investigating! I think the integer conversion happens here:

} else if value is integer
return PyLong_FromLongLong(value)
else if value is float
return PyFloat_FromDouble(value)

[Var is integer] if Var is non-empty and contains a purely numeric string (decimal or hexadecimal) without a decimal point. Leading and trailing spaces and tabs are allowed. The string may start with a plus or minus sign.
https://www.autohotkey.com/docs/v1/lib/IfIs.htm

One option is making AHKToPython return a string and converting the value in the Python code whenever ahk_call is invoked. This will break the code that uses ahk_call directly.

Another option is annotating the return types of the AHK functions and converting their results based on that.

Yet another option is not converting the value if its numerical representation differs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants