Sign in to follow this  
Followers 0
Maffe811

Automating a GIMP task

12 posts in this topic

Opt("WinTitleMatchMode", 4)

;Variables
$WindowHandle = WinGetHandle("GNU Image Manipulation Program") ;Get handle of the main GIMP window.
$ToolboxHandle = WinGetHandle("Toolbox") ;Get handle of the GIMP Toolbox.
$WinTitle1 = WinGetTitle($WindowHandle) ;Get title of main GIMP window to test that it is the correct window.
$WinTitle2 = WinGetTitle($ToolboxHandle) ; Get title of GIMP Toolbox to test that it is the correct window.

;Hotkey
HotKeySet ("^m", "fractal")

;The hotkey function
Func fractal()
    MsgBox(0, "0", $WinTitle1 & " " & $WindowHandle & @CRLF & $WinTitle2 & " " & $ToolboxHandle) ;Present a message box to confirm title and check handle against the Window Info Program


    ;ControlFocus($WindowHandle, "", "[CLASS:gdkWindowChild; INSTANCE:2]") ;Focus on the main GIMP window

    ControlClick ($WindowHandle, "", "[CLASS:gdkWindowChild; INSTANCE:17]" , "main", 1 ) ;Click File

    ControlClick ($WindowHandle, "", "[CLASS:gdkWindowChild; INSTANCE:19]" , "main", 1 ) ;Click Open as Layer...

EndFunc

;Keeping the script alive
While 1
    Sleep(100)
WEnd

This is the scrpit i am writing for a friend.

Im trying to make it click the menu then click "open as layer"

A couple of snags and questions:

1. The script needs gimp to be open, before starting to get the handle.

How do i make it so the script will automaticly get the handle without gimp needing to be opened first.

I was thinking winwaitactive, but wouldnt that make the script refresh the handle everytime gimp is active ?

Thats not what i want. But WinGetHandle returns blank string if nothing found.

Does that mean i can do a:

$string = ""

If $string = "" Then
    $string = WinGetHandle("GNU Image Manipulation Program");If it fails it will still be an empty string.
    Sleep (1000)
EndIf

2.The script only works once.

I dont understand why.

Stuff that happens:

Start gimp > wait for it to start up.

Start script > click Ctrl+m

MessgeBox appears, tells me handle info > Click OK

GIMP menu is clicked and menu appears floating on the screen

Then Ctrl+m again

MessageBox appears again > Click OK

Nothing happens...

I would like the script to be able to do the same thing over and over again.

So whats wrong and how do i fix it?

3. It doesnt click the "open as layers"

What am i doing wrong?

But ive been scripting for 2 hours now, and i think i deserve a break :huh2:


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites



you will have a handle only when the windows is open... :S the handles are created every time a window appear and not are the same...

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I know the handle is individual for each start, thats why it checks for it.

But i want to be able to start the script, and then the script will find the handle on its own whenever the program start.

I just had an idea!

I can ask the user to set the gimp.exe location, then make the script run gimp prior to extracting the handle!

That should work? right?

Edited by Maffe811

[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

aaaaa i know

WinActive() will do, it only retrives the handle if the windows not exists it return 0

Share this post


Link to post
Share on other sites

yes, but the problem is if the program doesnt exist it tries to finish the script without the correct variables.

So i need it to check everysecond, but stop when it finds it!

Opt("WinTitleMatchMode", 4)

$WindowHandle = ""


While 1
    If $WindowHandle = "" Then
        $WindowHandle = WinGetHandle("GNU Image Manipulation Program") ;Get handle of the main GIMP window.
        $ToolboxHandle = WinGetHandle("Toolbox") ;Get handle of the GIMP Toolbox.
        $WinTitle1 = WinGetTitle($WindowHandle) ;Get title of main GIMP window to test that it is the correct window.
        $WinTitle2 = WinGetTitle($ToolboxHandle) ; Get title of GIMP Toolbox to test that it is the correct window.
        Sleep (2000)
        MsgBox(0, "banana", $WindowHandle)
    EndIf
WEnd

This ^ Does the trick!

It will keep looking for the Handle and put a blank string in the message box until gimp starts.

Then it will present the string in the messagbox once.


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Opt("WinTitleMatchMode", 4)

While 1
    If WinGetHandle("[CLASS:gdkWindowToplevel]") Then
        _GetHandles()
        HotKeySet("m", "fractal")
    Else
        HotKeySet("m")
        Sleep(100)
    EndIf
WEnd
Func _GetHandles()
    ;Variables
    global $WindowHandle = WinGetHandle("Programa de manipulación de imágenes de GNU") ;Get handle of the main GIMP window.
    global $ToolboxHandle = WinGetHandle("Caja de herramientas") ;Get handle of the GIMP Toolbox.
    global $WinTitle1 = WinGetTitle($WindowHandle) ;Get title of main GIMP window to test that it is the correct window.
    global $WinTitle2 = WinGetTitle($ToolboxHandle) ; Get title of GIMP Toolbox to test that it is the correct window.
EndFunc   ;==>_GetHandles
Func fractal()
    MsgBox(0, "0", $WinTitle1 & " " & $WindowHandle & @CRLF & $WinTitle2 & " " & $ToolboxHandle) ;Present a message box to confirm title and check handle against the Window Info Program
    ControlFocus($WindowHandle, "", "[CLASS:gdkWindowChild; INSTANCE:2]") ;Focus on the main GIMP window
    ControlClick($WindowHandle, "", "[CLASS:gdkWindowChild; INSTANCE:17]", "main", 1) ;Click File
    ControlClick($WindowHandle, "", "[CLASS:gdkWindowChild; INSTANCE:19]", "main", 1) ;Click Open as Layer...
EndFunc   ;==>fractal

I have the spanish version... i guess that i can use the class to identify the window and then get the child and the parent to get the main window and the Toolbox window

edit: Also you can add a couple of variables to check when the handles are stored then control the handle getting to do not getting all the time the same handles... i forgot to change the hotkey from "m" to "^m"

Edited by monoscout999

Share this post


Link to post
Share on other sites

Yes, i take spanish at school (not real good at it) and i saw that you were from Argentina so i guessed you spoke spanish.

But its the same problem with your script as with mine.

You have to reopen gimp everytime you want to restart the process!


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

Also is it possible to have a GUI be in the lower right corner of the screen (above the taskbar)?

I want to pause the script until the user presses a button in a small GUI.


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

Opt("WinTitleMatchMode", 4)

$WindowHandle = ""
$Counter = 0
$Seconds = 60

While 1
    If $WindowHandle = "" Then
        $WindowHandle = WinGetHandle("GNU Image Manipulation Program") ;Get handle of the main GIMP window.
        $ToolboxHandle = WinGetHandle("Toolbox") ;Get handle of the GIMP Toolbox.
        $WinTitle1 = WinGetTitle($WindowHandle) ;Get title of main GIMP window to test that it is the correct window.
        $WinTitle2 = WinGetTitle($ToolboxHandle) ; Get title of GIMP Toolbox to test that it is the correct window.
        Sleep (1000);Sleep for 10 seconds
        ;MsgBox(0, "banana", $WindowHandle) ; Messagebox for testing purposes
            If $WindowHandle = "" Then
                TrayTip("Please start GIMP", "The script needs gimp to be open." & @CRLF & "This script will stop in " & $Seconds & " seconds, if gimp is not opened.", 1)
                    If $Counter = 60 Then
                        Exit
                    Else
                        $Counter = $Counter + 1
                        $Seconds = $Seconds - 1
                    EndIf
            EndIf
    EndIf
WEnd

There is probably a counter function somewhere, but it was first meant to put out the tray tip every tenth second.

But now i only need to fix that you can only run the script once when you start gimp and then you need to restart it.


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

So there is no interest in this?

Should i give up ?


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

Share this post


Link to post
Share on other sites

probably you will need to read the manual: Window Titles and Text (Basic)

run("C:\Program Files\GIMP-2.0\bin\gimp-2.6.exe")

$t=winwait("Gimp","",20)
if $t=0 Then
    consolewrite("we failed" & @CRLF)
Else
    consolewrite("we passed" & @CRLF)
EndIf

$t=winwaitactive("GNU Image Manipulation","",20)
if $t=0 Then
    consolewrite("we failed" & @CRLF)
Else
    consolewrite("we passed" & @CRLF)
EndIf

$t=winwait("Gereedschapskist","",20)
if $t=0 Then
    consolewrite("we failed" & @CRLF)
Else
    consolewrite("we passed" & @CRLF)
EndIf

Share this post


Link to post
Share on other sites

Dont fully understand what you are trying to point me at.

But... Uhm... I dont know why, but the script is now... kinda working...

I fixed it(dont know how)so it can now be ran multiple times without having to restart gimp.

I didnt notice that something in the info window changed when i went from a button do a dropdown menu.

Imm gonna see if that applies to the File menu aswell, if it does, that will be fixed.

Why is my script acting all funny?

Currently it duplicates the layer and makes a new one, but when i add the code to make it move it down in the layer dialog, the make a new layer code stops working.

I dont understand!


[font="helvetica, arial, sans-serif"]Hobby graphics artist, using gimp.Automating pc stuff, using AutoIt.Listening to music, using Grooveshark.[/font]Scripts:[spoiler]Simple ScreenshotSaves you alot of trouble when taking a screenshot!Don't remember what happened with this, but aperantly the exe is all i got.If you don't want to run it, simply don't._IsRun UDFIt figures out if the script has ben ran before based on the info in a ini file.If you don't want to use exactly what i wrote, you can use it as inspiration.[/spoiler]

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