Jump to content

Recommended Posts

Posted

This function worked perfectly for years and now it does not.  Its part of a larger program and for the life of me I can't figure out what the deal is.  I'm not a programmer so I'm a super noob at scripting.

 

#include <Misc.au3>                     ;  necessary for the SendEx function to avoid key sticking
#include <String.au3>                   ;  for string encryption
#include <FileConstants.au3>            ;  for file handling

HotKeySet("^{`}", "macfix")             ;  format MAC addresses

While 1
   Sleep (10)
WEnd

Func macfix()
    ClipPut ("")
    _SendEx("^c")
    $mac = ClipGet()
    $mac = StringLower($mac) ; convert to lower case if necessary
    $mac = StringRegExpReplace ($mac, "[^0-9a-f]", "")  ; remove anything besides numbers and letters a-f

    If (StringLen ($mac) <> 12) Then
        MsgBox (0, "Error", "Invalid MAC address")  ; check for correct number of characters
    Else
        $macSplit = StringSplit($mac, "")  ;  split mac apart, every character to a different position of the array
        Sleep (20)
        $mac = $macSplit[1] & $macSplit[2] & $macSplit[3] & $macSplit[4] & "." & $macSplit[5] & $macSplit[6] & $macSplit[7] & _
                $macSplit[8] & "." & $macSplit[9] & $macSplit[10] & $macSplit[11] & $macSplit[12]  ;  recombine them into a valid mac address
        _SendeX ($mac)  ;  replace highlighted text with corrected mac address
        ClipPut ($mac)  ;  put new mac address in clipboard for easy pasting
    EndIf
EndFunc

Func _SendEx($ss, $warn = "")
    ;Send the string $ss after the Shift Alt and Ctrl keys are released. Optionally give a warning after 1 sec if any of those keys are still down.
    ;Requires misc.au3 to be included in the script for the _IsPressed function.

    Local $iT = TimerInit()

    While _IsPressed("10") Or _IsPressed("11") Or _IsPressed("12")
        If $warn <> "" And TimerDiff($iT) > 1000 Then
            MsgBox(262144, "Warning", $warn)
        EndIf
        Sleep(50)
    WEnd
    Send($ss)
EndFunc;==>_SendEx

When I hit the ctrl+~ it will copy the MAC address but will return my error message "Invalid MAC address" every time, even when the string its copying is actually valid.

 

Posted (edited)

add some logging statements along the way for debug purposes. where is the source of the mac address supposed to be coming from? you need to supply the whole things code for answers. this is not even testable code.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Posted

The source is a highlighted MAC address in a notepad.  How it worked was I double click on a MAC address in my notepad (sublime text) such as aa:aa:bb:bb:cc:cc.  Hit 'ctrl ~ '  to execute the function which copies the whole thing to the clipboard, converts the case to lower, removes anything not a letter or number then adds '.' in the appropriate places and pastes it back to the notepad replacing the original.  So aa:aa:bb:bb:cc:cc becomes aaaa.bbbb.cccc.  It used to work perfectly. But now it doesn't, the only thing that has changed is possibly windows updates. (Windows 10).  


The whole program is just a handful of other keyboard shortcut things that don't have anything to do with this particular function, I can't paste it all because it contains sensitive information.

Posted

A bit streamlined:

Func macfix()
    $mac = "5e:4B:04:3f:0D:e3"
    $mac = StringRegExpReplace($mac, "[^[:xdigit:]]", "")  ; remove anything besides numbers and letters a-f

    If (StringLen($mac) <> 12) Then
        MsgBox (0, "Error", "Invalid MAC address")  ; check for correct number of characters
    Else
        $mac = StringLeft(StringRegExpReplace($mac, "([[:xdigit:]]{4})", "$1."), 14)
ConsoleWrite($mac & @LF)
    EndIf
EndFunc

But your code should work provided a valid input.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...