Jump to content

New to AutoIt, help with basic script!


Go to solution Solved by Exit,

Recommended Posts

Hi!
So I have this really short script I've written that doesn't work the way I want it to.
 

local $var=ClipGet()
WinActivate("skjema.txt - Notisblokk")
winwaitactive("skjema.txt - Notisblokk")
send("^{end}+^{left}^c")
if StringInStr($var, "asd")=0 Then
   send("+{home}{delete}")
   Else
   send("testtest")
EndIf

 
What I want the script to do:
1. Tab into my already active Notepad window
2. Go to end of pre-written text in notepad and copy the last word/character into the clipboard
3. If the copied text is "asd" then delete the whole row, if not send "testtest".
 
What script is doing:
1. Working
2. Working (I think)
3. ONLY WORKING after I've already done the script once before with the same last word in notepad

Problem: It seems that the script checks the last copied item in the clipboard and not the current, which would explain why I must run it twice before it works.

Example:

I type "hi" at end of notepad and run the script: replies with "testtest" ("asd" not found)

I now type "asd" at end of notepad and run the script: replies with "testtest" again (which I think is because the last item copied into the clipboard was "hi" from previous run)

This works both ways.

Sidequestion: Not sure how windows clipboard works, can it only hold 1 item at a time?

Thanks in advance!

Link to comment
Share on other sites

  • Moderators

Hi, sackjarrow. I would start with reading the file into an array, rather than opening it and using send:

#include <File.au3>

$sFile = @DesktopDir & "\skjema.txt"
Local $aArray
    _FileReadToArray($sFile, $aArray)

Then, if there are carriage returns, you could do something like this:

#include <File.au3>

$sFile = @DesktopDir & "\skjema.txt"
Local $aArray
    _FileReadToArray($sFile, $aArray)
    $x = $aArray[0]
    If StringRight($aArray[$x], 3) = "asd" Then
        ;do something
    Else
        ;do something else
    EndIf

If it is one continuous string of text, you could do something like this:

#include <File.au3>

$sFile = @DesktopDir & "\skjema.txt"
Local $aArray
    _FileReadToArray($sFile, $aArray)
        $sString = _ArrayToString($aArray, "")
            If StringRight($sString, 3) = "asd" Then
                ;do something
            Else
                ;do something else
            EndIf

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

you should read the clipboard AFTER the send.

First:

send("^{end}+^{left}^c")

then:

local $var=ClipGet()

 

That's what the "^c" (control-C) is doing at the end of the send already.

I'd recommend working with the file directly as JLogan has recommended.  If for some reason you need to do this with notepad, I would work directly with the control, and not bother with send().

Something like this:

Local $sText = ControlGetText("[TITLE:skjema.txt - Notisblokk; CLASS:Notepad]", "", "[CLASS:Edit; INSTANCE:1]")
Local $aText = StringSplit($sText, ' ')
Local $aTextLF = StringSplit($sText, @LF)

If $aText[$aText[0]] = 'asd' Then
    $sText = StringReplace($sText, $aTextLF[$aTextLF[0]], '', -1)
Else
    $sText &= 'testtest'
EndIf

ControlSetText("[TITLE:skjema.txt - Notisblokk; CLASS:Notepad]", "", "[CLASS:Edit; INSTANCE:1]", $sText)
Edited by danwilli
Link to comment
Share on other sites

No, 'control-c' copies data to the clipboard.

Then, clipget() can Transfer this data from the clipboard to the variable.

LOL, yep you're absolutely right, I thought you tossed in a clipput() when I skimmed the code... Should have paid more attention.

Best option for the OP would be to deal with the file directly or if for some reason this needs to be done in notepad, use the edit control directly.

Edited by danwilli
Link to comment
Share on other sites

Woah thanks for awesome and fast responses! :D

Thanks danwilli and jlogan3o13 for suggestions, will have to read about arrays and controls, haven't gotten that far yet but awesome to post here and get better variants back :)

Thanks Exit for solving my script! For some reason I thought one had to define all the variables at the beginning of a script, this obviously does not seem to be the case. Working as a charm now (Y)

(this might solve many of the other problems I've had as well)

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...