Search code examples
drawingautoit

How to redraw a curve to Photoshop with Autoit?


I want to draw a specific curve line to Photoshop or to mspaint. This drawing action should be saved for the possibility to redraw that curve in the exact same way. How can I do it with Autoit? Is there a recording and play mechanism? As far as I read, the AU3 recorder is not available anymore.

Photoshop is just an example. I want to be able to do that kind of drawing record for different purposes and programs. Maybe also for online image editors or something.

I am not that familiar with Autoit yet. I do not expect a full code example, maybe you can give me an idea - that would be very helpful.


Currently I tried a bit with mouse functions like MouseDown, MouseMove etc. and it is quite funny, but i do not really have a concept to record and redraw these mouse actions.


If I have to clarify more please let me know - i will do my best to be precise.


Solution

  • I recommend using two scripts, one for recording and the second to replay recorded actions.

    Code for the recording:

    ; declaration
    Global $sFileCoordinates            = @ScriptDir & '\RecordedMouseMoveCoordinates.txt'
    Global $iRecordingDurationInSeconds = 10
    Global $iXSave, $iYSave
    
    ; functions
    Func _recordMouseMoveCoordinatesToFile()
        Local $aPos = MouseGetPos()
    
        If $aPos[0] <> $iXSave Or $aPos[1] <> $iYSave Then
            FileWrite($hFile, $aPos[0] & ',' & $aPos[1] & @CRLF)
    
            Local $aPos = MouseGetPos()
            $iXSave = $aPos[0]
            $iYSave = $aPos[1]
        EndIf
    
        Sleep(80)
    EndFunc
    
    ; processing
    Sleep(4000) ; wait 4 seconds to place your mouse to the start position
    
    Global $hFile  = FileOpen($sFileCoordinates, 1 + 256)
    Global $hTimer = TimerInit()
    
    While Round((TimerDiff($hTimer) / 1000), 1) <= $iRecordingDurationInSeconds
        ToolTip(Round((TimerDiff($hTimer) / 1000), 1))
        _recordMouseMoveCoordinatesToFile()
    WEnd
    
    FileClose($hFile)
    

    Recording will start after a 4 second delay. This should allow to move your mouse to the start point of your drawing action.

    Global $iRecordingDurationInSeconds = 10 means your drawing action should be finished in 10 seconds (a tooltip displays remaining seconds). And here the seconds script.

    Code to redraw curve:

    ; declaration
    Global $sFileCoordinates = @ScriptDir & '\RecordedMouseMoveCoordinates.txt'
    
    ; functions
    Func _getFileContent($sFile)
        Local $hFile        = FileOpen($sFile, 256)
        Local $sFileContent = FileRead($hFile)
        FileClose($hFile)
        Return $sFileContent
    EndFunc
    
    Func _drawRecordedMouseMoveCoordinatesFromFile($sContent)
        Local $aFileContent = StringSplit($sContent, @CRLF, 1)
    
        Local $iX = StringSplit($aFileContent[1], ',')[1]
        Local $iY = StringSplit($aFileContent[1], ',')[2]
    
        MouseMove($iX, $iY, 4)
        MouseDown('left')
    
        For $i = 1 To $aFileContent[0] Step 1
            If $aFileContent[$i] <> '' Then
                Local $iX = StringSplit($aFileContent[$i], ',')[1]
                Local $iY = StringSplit($aFileContent[$i], ',')[2]
    
                MouseMove($iX, $iY, 4)
            EndIf
        Next
    
        MouseUp('left')
    EndFunc
    
    ; processing
    Sleep(2000) ; wait 2 seconds till start
    
    Global $sFileContent = _getFileContent($sFileCoordinates)
    _drawRecordedMouseMoveCoordinatesFromFile($sFileContent)
    

    There is a start delay of 2 seconds. All saved coordinates will be executed in the same way recorded. It starts with MouseDown('left'), then the mouse movements to MouseUp('left').

    Notice:

    This approach isn't really robust because of the coordinates which aren't relative to your window. Please see Opt('MouseCoordMode', 0|1|2) in the help file for more information. If you want to draw more than just one line or curve this approach isn't the best. But as your question describes only that requirement it should be fine.