Jump to content

Get code to wait


Olson
 Share

Recommended Posts

I have a little code to manipulate .jpg files.  The code is not allowing the previous instructions to finish.  I tried:

Send("!I")
    Send("E")
    Send("6")
    WinWaitActive("Resize")
    Send("+R")
    WinWaitActive("Resize")
        Send("+S")

And it only executes the first 3 key presses.

I've tried:

Send("!I")
    Send("E")
    Send("6")
    sleep(5000)
    Send("+R")
    sleep(5000)
        Send("+S")

And this is very slow for some images and not slow enough for larger images.

Any ideas?

Link to comment
Share on other sites

  • Moderators

Instead of a long sleep, have you tried much shorter sleeps between each step? Without seeing precisely what you're doing with the file, or in what app, it is difficult to offer suggestions.

"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

Yes, I have tried shorter sleeps and it still skips over the next code if it's not ready.

I'm using Paint Shop Pro v8.1.  The above code:

1. Changes the image to 16 million colors.

2. Crops to what is selected.

3. Opens the Resize window.

Just telling you that helps me with one thing. WinWaitActive("Resize") doesn't work in the first 2 because "Resize" isn't open yet.  I guess I would need to do WinWaitActive ("Jasc Paint Shop Pro")

Link to comment
Share on other sites

  • Moderators

WinWaitActive means you're waiting until that window is present and has focus. If you just want to wait until the window exists, just use WinWait.

It looks like you're running into problems because PSP takes a while to convert the image to 16 million colors. I would look into whether it spawns another process to make this conversion; then you could wait for the process to close.

"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

Use the win*, and control* functions (look in the help file).  They will be much more reliable, accurate, and timely than sends and sleeps.

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

I just don't know what to wait for or what control to look at.  For instance, if I'm changing the amount of colors from 256 to 16 million, the program "thinks" for a few seconds while doing this.  I looked at the processes in Task Manager and I didn't see a new one pop up while the program is processing the instructions.

Link to comment
Share on other sites

Something must change on the gui.  How do you, as an observer, know it's done?

What is the code you were using to get the wingetstate?

What is the code you were using to search for the other processes?

You can also use this function, to see if any new handles are produced (would need to run ALL these options in loops).._WinAPI_EnumProcessWindows($iPid)

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

I just don't know what to wait for or what control to look at.  For instance, if I'm changing the amount of colors from 256 to 16 million, the program "thinks" for a few seconds while doing this.  I looked at the processes in Task Manager and I didn't see a new one pop up while the program is processing the instructions.

 

Try using winwait("resize") as suggested. Also try changing the option for window title matching.

Link to comment
Share on other sites

Why not use PSP's own recording function, in "File/Script/Start recording", I use an old version of PSP also (9) and use the recording function frequently with good results.

Some guy's script + some other guy's script = my script!

Link to comment
Share on other sites

I have used PSP recording, but I can't seem to figure out how to make the code work for a variety of original photo sizes.

The code, as it stands is this:

HotKeySet("{F7}", "size")

 While 1
    Sleep(100)
WEnd

Func size()
    Send("!i")
    Send("e")
    Send("6")
    sleep("3000")
    Send("+r")
    sleep("3000")
    Send("+s")
    WinWaitActive("Resize")
    $resolution = ControlGetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:5]");reads the current resolution, change resolution with the correct control
    If $resolution <> "100.000" Then ControlSetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:5]", "100");set the resolution control, change resolution with the correct control
    $width= ControlGetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:1]");reads the current width, change width with the correct control
    $height= ControlGetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:2]");reads the current height, change height with the correct control
    $height=$height+1;
    $width=$width+1
    sleep("1000")
    If $width<$height Then; compare the 2
        ControlSetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:1]", "4000");set the width control, change width with the correct control
    Else
        ControlSetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:2]", "4000");set the height control, change height with the correct control
    EndIf
    ControlClick("Resize", "", "[CLASS:Button; INSTANCE:14]")
    sleep("2000")   
    Send("!a")
    Send("s")
    Send("h")
    sleep("2000")   
    Send("!a")
    Send("s")
    Send("h")
EndFunc
Link to comment
Share on other sites

 

I have used PSP recording, but I can't seem to figure out how to make the code work for a variety of original photo sizes.

The code, as it stands is this:

HotKeySet("{F7}", "size")

 While 1
    Sleep(100)
WEnd

Func size()
    Send("!i")
    Send("e")
    Send("6")
    sleep("3000")
    Send("+r")
    sleep("3000")
    Send("+s")
    WinWaitActive("Resize")
    $resolution = ControlGetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:5]");reads the current resolution, change resolution with the correct control
    If $resolution <> "100.000" Then ControlSetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:5]", "100");set the resolution control, change resolution with the correct control
    $width= ControlGetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:1]");reads the current width, change width with the correct control
    $height= ControlGetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:2]");reads the current height, change height with the correct control
    $height=$height+1;
    $width=$width+1
    sleep("1000")
    If $width<$height Then; compare the 2
        ControlSetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:1]", "4000");set the width control, change width with the correct control
    Else
        ControlSetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:2]", "4000");set the height control, change height with the correct control
    EndIf
    ControlClick("Resize", "", "[CLASS:Button; INSTANCE:14]")
    sleep("2000")   
    Send("!a")
    Send("s")
    Send("h")
    sleep("2000")   
    Send("!a")
    Send("s")
    Send("h")
EndFunc

 

So, For one, Your script is sending keys to the active window. It would be helpful to everyone if you wrote a check to make sure the Window Exists, Then WinActivate it before sending keys. Also, To ensure focus is not pulled away from it, You can make use of BlockInput. Which requires Admin Privileges. Hope this helps at some level.

Also, I took it upon myself to partially tidy up your code and, add what I mentioned above;

HotKeySet("{F7}", "Size")

Call("AdminCheck")

While 1
   Sleep(100)
WEnd

Func AdminCheck()
;-----------------------------------------------------
; Request Admin Privileges
;-----------------------------------------------------
#RequireAdmin
$Admin = IsAdmin()
If $Admin = 1 Then
   Return   
ElseIf $Admin = 0 Then
   MsgBox(0, "Error", "This application requires Administrative Privleges to function properly," & @CRLF & "Please restart the application to proceed.")
   Exit
EndIf
EndFunc

Func Size()
   If WinExists("Resize" Then
       WinActivate("Resize")
         WinWaitActive("Resize")
            BlockInput(1)
               Send("!i")
               Send("e")
               Send("6")
               Sleep("3000")
               Send("+r")
               Sleep("3000")
               Send("+s")
            BlockInput(0)
         WinWaitActive("Resize")
            $resolution = ControlGetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:5]");reads the current resolution, change resolution with the correct control
            If $resolution <> "100.000" Then 
               ControlSetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:5]", "100");set the resolution control, change resolution with the correct control
               $width = ControlGetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:1]");reads the current width, change width with the correct control
               $height = ControlGetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:2]");reads the current height, change height with the correct control
               $height = Number($height + 1)
               $width = Number($width + 1)
               Sleep("1000")
               If $width < $height Then; compare the 2
                  ControlSetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:1]", "4000");set the width control, change width with the correct control
               Else
                  ControlSetText("Resize", "", "[CLASS:AfxWnd42; INSTANCE:2]", "4000");set the height control, change height with the correct control
               EndIf
            Else
               BlockInput(1)
               ControlClick("Resize", "", "[CLASS:Button; INSTANCE:14]")
               Sleep("2000")   
               Send("!a")
               Send("s")
               Send("h")
               Sleep("2000")   
               Send("!a")
               Send("s")
               Send("h")
               BlockInput(0)
            EndIf
   ElseIf WinExists("Resize") = "0" Then
      MsgBox(0, "Error", "Resize Window does not exist", 25)
   EndIf
EndFunc

/Edit: Fixed my example

/Edit2: Added AdminCheck Function for BlockInput purposes

Edited by BlackDawn187
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...