Jump to content

HotKeySet with Sleep causing inaccessible variables


Synaps3
 Share

Recommended Posts

Hi,

I got a simple script here that does a little link manipulation for google images. I posted it in the example scripts. Even though it is working perfectly, I was a little annoyed that it was using a full core of my CPU. You can see, the entire script just uses a HotKeySet for operation. The while 1 loop is unecessary, but I had to put it there to make the script keep running. This is what eats the CPU. So I put a sleep(10) in there, but then the script starts acting strange saying that certain variables are innaccessable which makes no sense to me. Why would a short sleep make some variables get corrupted. I could understand that maybe the hotkeyset would not trigger, but it triggers just fine every time, it's just that the code in the function acts strange.

So how can I idle the CPU without breaking my script?

#include <Misc.au3>
#include <MsgBoxConstants.au3>
#include <AutoItConstants.au3>
#include <Clipboard.au3>
#include <Array.au3>
#include <String.au3>

Local $clipB
Local $urlArray
Local $theLink

HotKeySet("\", "OpenImage")

Func DecodeUrl($src)
    Local $i
    Local $ch
    Local $buff

    ;Init Counter
    $i = 1

    While ($i <= StringLen($src))
        $ch = StringMid($src, $i, 1)
        ;Correct spaces
        If ($ch = "+") Then
            $ch = " "
        EndIf
        ;Decode any hex values
        If ($ch = "%") Then
            $ch = Chr(Dec(StringMid($src, $i + 1, 2)))
            $i += 2
        EndIf
        ;Build buffer
        $buff &= $ch
        ;Inc Counter
        $i += 1
    WEnd

    Return $buff
EndFunc   ;==>DecodeUrl

Func OpenImage()
   MouseClick($MOUSE_CLICK_RIGHT)
   Send("A")
   Sleep(100)
   $clipB = _ClipBoard_GetData($CF_TEXT)
   $theLink = DecodeUrl($clipB)
   If StringInStr($theLink, "&url=") = 0 Then
      $urlArray = _StringBetween($theLink, "=", "&")
      If StringInStr($urlArray[0], "?") <> 0 Then ;THIS IS WHERE IT BREAKS WHEN I SLEEP BELOW - MAKES NO SENSE
         $urlArray = _StringBetween($theLink, "=", "?")
      EndIf
   Else
      $urlArray = _StringBetween($theLink, "url=", "&")
   EndIf
   ShellExecute($urlArray[0])
EndFunc

While 1
   ; WHAT TO DO WITH THIS? i DONT NEED IT
Wend

 

Link to comment
Share on other sites

Hi,

All your script is based on the data you get from the clipboard, so if that data is empty or not corresponding to an url formatted as you expect then it will fail.

And of course you need your while 1 loop if you want your script to keep running, that's the same for every programming language :)

You need to take errors and debugging into account in your script.

So something like this:

$clipB = _ClipBoard_GetData($CF_TEXT)
if stringinstr($clipB, "http") = 0 Then
    msgbox("","Error", "the clipboard data is not an url" & @CRLF & "Clipboard data: " & @CRLF & $clipB)
Else
    $theLink = DecodeUrl($clipB)
       If StringInStr($theLink, "&url=") = 0 Then
          $urlArray = _StringBetween($theLink, "=", "&")
          if IsArray($urlArray) Then
                if StringInStr($urlArray[0], "?") <> 0 Then 
                    $urlArray = _StringBetween($theLink, "=", "?")
                    ShellExecute($urlArray[0])
                EndIf
          Else
                msgbox("","Error", "No link found between = and & in the url" & "Clipboard data: " & @CRLF & $clipB & @CRLF & "decoded url: " & @CRLF & $theLink)
          EndIf
       Else
          $urlArray = _StringBetween($theLink, "url=", "&")
          if IsArray($urlArray) then 
              ShellExecute($urlArray[0])
          Else
              msgbox("","Error", "No link found between = and & in the url" & "Clipboard data: " & @CRLF & $clipB & @CRLF & "decoded url: " & @CRLF & $theLink)
          EndIf
       EndIf
EndIf

 

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...