Sign in to follow this  
Followers 0
cal

Works every 2nd time?

4 posts in this topic

I came across Autoit on friday becasue my other tool is having issues with NT.

After playing aound for a couple of days I've managed to move my most important macros over to this tool. I'm sure they are a bit sloppy but they work.

I'm having problems with one however that I can't figure out.

This is normally called in a loop untill a certain txt string is found but I took the loop out to better show the problem I'm having.

It seems to work, but only part time. Its activated by the hotkey Alt z.

It works once and then not the second time. then works the 3rd time and not the forth and so on and so on.

Here is the script as it is without the looping.

HotKeySet("!z", "znum")

;Send("#r")
;WinWaitActive("Run")
;Send("notepad.exe ""c:\xmltv\Copy of 2 grep the listings.bat""{Enter}")


While 1
  Sleep(100)
WEnd


Func znum()


; WinWaitActive("Copy of 2 grep the listings")

Send("{LALT}")

  Send("{HOME}{CTRLDOWN}{RIGHT 3}{CTRLUP}{RIGHT}{CTRLDOWN}{SHIFTDOWN}{RIGHT}{CTRLUP}{LEFT}{SHIFTUP}")
  Send("^c")
  $z1 = ClipGet()
  $z2 = Number($z1)
  $z2 = $z2 - 1
  $z2 = String($z2)
  ClipPut ($z2)
  Send("^v")
  Send("{DOWN}")


EndFunc

It should read from an open text file and find the z number in the line. ie. z43 in the first line below.

Then reduce the number by one and paste it back into the line.

Then drop down to the next line.

a part of the lines of text its supposed to work on are

xmltv tv_grep --output z43 -i --title young --title frankenstein guide.xml

xmltv tv_grep --output z44 -i --title charmed --sub-title blue guide.xml

xmltv tv_grep --output z45 -i --title saving --title ryan guide.xml

xmltv tv_grep --output z46 -i --title astro --title boy guide.xml

xmltv tv_grep --output z47 -i --title the --title boy ! --title jimmy guide.xml

If I dont have the

Send("{LALT}")

in there it seems to spin around in the file-edit-search-help menu in notepad.

I think this is somehow connected to my problem.

Any Ideas.

Share this post


Link to post
Share on other sites



Interesting style....

I notice you use "{CTRLDOWN}{SHIFTDOWN}{RIGHT}{CTRLUP}{LEFT}{SHIFTUP}"

It might be better to re-arrange those so that the Ctrl/Shift up and downs are balanced: {SHIFTDOWN}{CTRLDOWN}{RIGHT}{CTRLUP}{LEFT}{SHIFTUP}"

However, I'd like to offer some alternative methods:

The Send("#r")...Send("notepad....") could be replaced with

RunWait('notepad "c:\xmltv\Copy of 2 grep the listings.bat"')

It would probably be much better to use FileReadLine and parse the file and then write the output to a new file.... when finished, replaced the old file with the new one:

$filename = "D:\Desktop\SampleIn.txt"
$tempFile = "D:\Desktop\SampleOut.txt"

$inputHandle = FileOpen($filename, 0) ;read mode
$outputHandle = FileOpen($tempFile, 1);append mode
If $inputHandle = -1 or $outputHandle = -1 Then
   MsgBox(4096,"Error","Could not open file.... quitting...")
   Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
   $line = FileReadLine($inputHandle)
   If @error = -1 Then ExitLoop
   $newLine = ParseLine($line)
   FileWriteLine($outputHandle, $newLine)
Wend

FileClose($inputHandle)
FileClose($outputHandle)

MsgBox(4096,"Note", "The last step would be to delete the original and rename the temp file...")
;commented out for safety when testing ....
;FileDelete($filename)
;FileMove($tempFile, $filename)
Exit

; Return line with the number decremented... this will be easier
; once AutoIt gets full regular exp[b][/b]ression searching....
Func ParseLine($line)
  ;break up the line into an array based on spaces
   Local $i, $stuff, $newStuff;declare local variables
   $stuff = StringSplit($line, " ")
   For $i = 1 to $stuff[0];loop thru each element until find right z##
      If Stringleft($stuff[$i],1) = "z" And StringIsDigit(StringTrimLeft($stuff[$i],1)) Then
         $newStuff = "z" & (Number(StringTrimLeft($stuff[$i],1)) - 1)
         Return StringReplace($line, $stuff[$i], $newStuff)
      EndIf
   Next
EndFunc

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

I like the runwait line. I'll be changing that for sure. I must have missed that command when I was pokeing around the help file.

Reading it from a file directly isn't going to work in this case as I'm modifing and adding/deleting tv_grep lines as I go. Although I can think of a different script I have yet to write that might benifit from your code. I'll hang onto it for later.

This is just a hot key that reads whatever line I'm currently on and changes the number by one.

I took it out of loop but when in the loop it reeds all lines from the currrent one down to a special line of text that tells it to stop, and changing all the numbers as it goes.

I'll balance it in the morning when I think I'll have some more time for this and see what that does.

Your replacement of the number part looks good. I'll have to have a good look at that part in the morning too.

Any idea about the

Send("{LALT}")

Line?

I have no idea why I need that. The examples that write to notepad in the autotoit dir dont have that.

Anyway. I'll have another look at this in the morning. Thanks.

Share this post


Link to post
Share on other sites

I found the problem with it only working every second time.

Its two lines

Send("^c")

$z1 = ClipGet()

They don't play nice together.

Putting a

Sleep(200)

between them makes it work every time. without it, it works every 2nd time.

I finally figured it out by putting a few MegBox commands in various places and noticed the returned results where not what I was expecting every 2nd time through.

The second problem is still there. The Alt in the hotkey screws stuff up.

I still want to use Alt z as the hotkey, but it looks like it gets passed though to the app instead of being captured like it says hotkeys are supposed to.

Anyone have any ideas how I can use Alt z instead of the {NUMPADSUB} that I've got it currenty set to.

Now I can put it back into the loops and have it function like it was ment to.

Here is the code as is at the moment.

;HotKeySet("!z", "znum")
HotKeySet("{NUMPADSUB}", "znum")
HotKeySet("{esc}", "getout")


;Send("#r")
;WinWaitActive("Run")
;Send("notepad.exe ""c:\xmltv\Copy of 2 grep the listings.bat""{Enter}")


While 1
  Sleep(100)
WEnd

Func getout()
  Exit
EndFunc


Func znum()

;Send("{LALT}")
;Send("{ALTDOWN}")
;Send("{ALTUP}")

  Send("{HOME}{CTRLDOWN}{RIGHT 3}{CTRLUP}{SHIFTDOWN}{RIGHT 3}{SHIFTUP}")
  Send("^c")
  Sleep(200)
  $z1 = ClipGet()
  $z1 = StringMid ($z1, 2, 2 )
  
  $z2 = Number($z1)
  $z2 = $z2 - 1
  $z2 = String($z2)
  $z2 = "z" & $z2
  
  ClipPut($z2)
  Send("^v")
  Send("{DOWN}")


 ;$msg = StringFormat("Z1 is %s, Z2 is %s", $z1, $z2)
 ;MsgBox (0, "Z Values", $msg)

Share this post


Link to post
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
Sign in to follow this  
Followers 0