Simpatie.ro - matrimoniale
Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Nou pe simpatie:
dannutzza
Femeie
25 ani
Mehedinti
cauta Barbat
25 - 52 ani
Mrrrr's Forum (VIEW ONLY)ReguliInregistrareLoginPozeNu sunteti logat. Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides /

[AUTOHOTKEY] Create Drop Target For Custom WinRAR Profile

Pagini: 1 Moderat de TRaP, TonyTzu
pus acum 44 ore#1
Mrrrr
AdMiN
Postari: 2390
I created a custom WinRAR profile which I use to add all files/folders to one archive with 5% recovery. Added it to right click menu, so whenever I want I can right click multiple files and add to archive with my custom profile.

Too easy.

I wanted a drop target so I can drop my files/folders onto it an skip the right click.

First, I created a .bat file that works with dropping the files/folders onto the .bat file itself. In gold color is the name of my custom WinRAR profile.

@echo off
cd /d "%~dp0"

:: Get the name of the folder containing the first item dropped
for %%P in ("%~dp1.") do set "ArchiveName=%%~nxP"

:: Create the archive using the parent folder's name
"C:\Program Files\WinRAR\WinRAR.exe" a "-cpRAR Store Recovery 5%% Add here" "%~dp1%ArchiveName%.rar" %*

Then I figured, well, this is not actually a drag & drop target now, is it?. I want something that appears on top of everything else, like a real drop target and where I can easily drop my folders to have them added to my desired WinRAR profile.

ChatGPT proposed the easiest solution - AutoHotkey.

Below is the code that creates the GUI and uses my custom RAR Profile named "RAR Store Recovery 5% Add here". I left all the comments from ChatGPT, that's why the code is that long (>500 lines):


#Requires AutoHotkey v2.0
#SingleInstance Force

; ============================================================
; RAR DROP TARGET
; ============================================================
; Drag files/folders onto this window to create a RAR archive.
;
; The archive is named after the folder containing the dropped
; items.
;
; Example:
;
;   D:\Recovery\Folder1        or        D:\Recovery\file1.ext
;   D:\Recovery\Folder2        or        D:\Recovery\file2.ext
;
; creates:
;
;   D:\Recovery\Recovery.rar
;
; ============================================================

; ============================================================
; CONFIGURATION
; ============================================================

; ------------------------------------------------------------
; WinRAR
; ------------------------------------------------------------

WinRAR := "C:\Program Files\WinRAR\WinRAR.exe"

; WinRAR profile to use.
RARProfile := "RAR Store Recovery 5% Add here"

; Archive extension
ArchiveExtension := ".rar"

; ------------------------------------------------------------
; Drop target window
; ------------------------------------------------------------

WindowWidth := 280
WindowHeight := 220

; 255 = completely opaque
WindowTransparency := 235


; ============================================================
; CREATE GUI
; ============================================================

MyGui := Gui(
    "+AlwaysOnTop +Resize",
    "RAR Drop Target"
)

MyGui.BackColor := "202020"


; ------------------------------------------------------------
; Main drop-area text
; ------------------------------------------------------------

TextControl := MyGui.AddText(
    "x10 y20 w260 h130 Center",
    "`n`nDROP FILES /`nFOLDERS HERE"
)

TextControl.SetFont("s14 Bold cFFFFFF")


; ------------------------------------------------------------
; Status text
; ------------------------------------------------------------

StatusControl := MyGui.AddText(
    "x10 y160 w260 h40 Center +0x200",
    "Ready"
)

StatusControl.SetFont("s9 cC0C0C0")


; ============================================================
; GUI EVENTS
; ============================================================

; Windows file/folder drag & drop
OnMessage(0x233, WM_DROPFILES)

; Window events
MyGui.OnEvent("Close", (*) => ExitApp())
MyGui.OnEvent("ContextMenu", ShowContextMenu)
MyGui.OnEvent("Size", GuiResize)


; ============================================================
; SHOW GUI
; ============================================================

MyGui.Show(
    "w" WindowWidth " h" WindowHeight
)

WinSetTransparent(
    WindowTransparency,
    MyGui.Hwnd
)


; ------------------------------------------------------------
; Tell Windows that this window accepts file drops
; ------------------------------------------------------------

DllCall(
    "Shell32\DragAcceptFiles",
    "Ptr", MyGui.Hwnd,
    "Int", true
)



; ============================================================
; DRAG & DROP
; ============================================================

WM_DROPFILES(wParam, lParam, msg, hwnd)
{
    global MyGui, StatusControl

    ; Ignore drops outside our window.
    if (hwnd != MyGui.Hwnd)
        return


    ; --------------------------------------------------------
    ; Get dropped items
    ; --------------------------------------------------------

    DroppedItems := GetDroppedItems(wParam)


    ; Tell Windows the drag/drop operation is finished.
    DllCall(
        "Shell32\DragFinish",
        "Ptr", wParam
    )


    ; Nothing was dropped.
    if (DroppedItems.Length = 0)
        return


    ; --------------------------------------------------------
    ; Create the archive
    ; --------------------------------------------------------

    CreateArchive(DroppedItems)
}



; ============================================================
; GET DROPPED ITEMS
; ============================================================

GetDroppedItems(wParam)
{
    Items := []


    ; Number of dropped items
    Count := DllCall(
        "Shell32\DragQueryFileW",
        "Ptr", wParam,
        "UInt", 0xFFFFFFFF,
        "Ptr", 0,
        "UInt", 0
    )


    if (Count = 0)
        return Items


    ; --------------------------------------------------------
    ; Retrieve every dropped path
    ; --------------------------------------------------------

    Loop Count
    {
        Index := A_Index - 1


        ; Get required character count
        Length := DllCall(
            "Shell32\DragQueryFileW",
            "Ptr", wParam,
            "UInt", Index,
            "Ptr", 0,
            "UInt", 0
        )


        ; Allocate Unicode buffer
        BufferPath := Buffer((Length + 1) * 2)


        ; Retrieve path
        DllCall(
            "Shell32\DragQueryFileW",
            "Ptr", wParam,
            "UInt", Index,
            "Ptr", BufferPath,
            "UInt", Length + 1
        )


        ; Windows returns UTF-16 text.
        Path := StrGet(
            BufferPath,
            Length,
            "UTF-16"
        )


        Items.Push(Path)
    }


    return Items
}



; ============================================================
; CREATE ARCHIVE
; ============================================================

CreateArchive(DroppedItems)
{
    global StatusControl, WinRAR


    ; --------------------------------------------------------
    ; Check WinRAR
    ; --------------------------------------------------------

    if !FileExist(WinRAR)
    {
        SetStatus(
            "WINRAR NOT FOUND!",
            "FF0000"
        )

        MsgBox(
            "WinRAR could not be found:`n`n"
            WinRAR,
            "RAR Drop Target",
            "Iconx"
        )

        return
    }


    ; --------------------------------------------------------
    ; Determine archive path
    ; --------------------------------------------------------

    ArchiveFile := GetArchivePath(
        DroppedItems[1]
    )


    ; --------------------------------------------------------
    ; Update status
    ; --------------------------------------------------------

    SetStatus(
        "Creating RAR... (" DroppedItems.Length " item(s))",
        "FFFF00"
    )


    ; --------------------------------------------------------
    ; Build WinRAR command
    ; --------------------------------------------------------

    Command := BuildWinRARCommand(
        DroppedItems,
        ArchiveFile
    )


    ; --------------------------------------------------------
    ; Run WinRAR
    ; --------------------------------------------------------

    try
    {
        ExitCode := RunWait(
            Command,
            ,
            ;"Hide" ; Uncomment the ;"Hide" line to hide the WinRAR GUI showing progress of the archiving process (it runs completely silent)
        )


        if (ExitCode = 0)
        {
            SetStatus(
                "RAR creation finished",
                "00FF00"
            )
        }
        else
        {
            SetStatus(
                "WinRAR finished with code " ExitCode,
                "FFA500"
            )
        }
    }
    catch as ErrorObject
    {
        SetStatus(
            "ERROR running WinRAR",
            "FF0000"
        )

        MsgBox(
            "Could not run WinRAR.`n`n"
            ErrorObject.Message,
            "RAR Drop Target",
            "Iconx"
        )
    }


    ; Return to Ready after 4 seconds.
    SetTimer(
        ResetStatus,
        -4000
    )
}

; ============================================================
; GET ARCHIVE PATH
; ============================================================

GetArchivePath(FirstItem)
{
    global ArchiveExtension

    ; Get the folder containing the dropped item
    SplitPath(FirstItem, , &ParentFolder)

    ; Check whether the parent is a drive root, e.g. D:\
    if RegExMatch(ParentFolder, "^[A-Za-z]:\\$")
    {
        ; Use the drive letter as the archive name
        DriveLetter := SubStr(ParentFolder, 1, 1)

        return ParentFolder DriveLetter ArchiveExtension
    }

    ; Otherwise use the name of the containing folder
    SplitPath(ParentFolder, , , , &ParentFolderName)

    return ParentFolder "\" ParentFolderName ArchiveExtension
}

; ============================================================
; BUILD WINRAR COMMAND
; ============================================================

BuildWinRARCommand(
    DroppedItems,
    ArchiveFile
)
{
    global WinRAR, RARProfile


    ; --------------------------------------------------------
    ; Base command
    ; --------------------------------------------------------

    Command :=
        '"' WinRAR '"'
        . ' a'
        . ' "-cp' RARProfile '"'
        . ' "' ArchiveFile '"'


    ; --------------------------------------------------------
    ; Add every dropped item
    ; --------------------------------------------------------

    for Item in DroppedItems
    {
        Command .=
            ' "' Item '"'
    }


    return Command
}



; ============================================================
; STATUS
; ============================================================

SetStatus(Text, Color)
{
    global StatusControl

    StatusControl.Text := Text
    StatusControl.SetFont(
        "s9 Bold c" Color
    )
}


ResetStatus()
{
    global StatusControl

    StatusControl.Text := "Ready"

    StatusControl.SetFont(
        "s9 cC0C0C0"
    )
}



; ============================================================
; GUI RESIZE
; ============================================================

GuiResize(
    guiObj,
    minMax,
    width,
    height
)
{
    global TextControl, StatusControl


    ; Don't resize controls while minimized.
    if (minMax = -1)
        return


    ; --------------------------------------------------------
    ; Main text
    ; --------------------------------------------------------

    TextControl.Move(
        10,
        20,
        width - 20,
        height - 80
    )


    ; --------------------------------------------------------
    ; Status
    ; --------------------------------------------------------

    StatusControl.Move(
        10,
        height - 55,
        width - 20,
        40
    )
}



; ============================================================
; RIGHT-CLICK MENU
; ============================================================

ShowContextMenu(
    guiObj,
    ctrl,
    item,
    isRightClick,
    x,
    y
)
{
    MenuObj := Menu()


    MenuObj.Add(
        "Open WinRAR",
        OpenWinRAR
    )

    MenuObj.Add(
        "Open script folder",
        OpenScriptFolder
    )

    MenuObj.Add()


    MenuObj.Add(
        "Reload drop target",
        ReloadScript
    )

    MenuObj.Add(
        "Exit",
        ExitScript
    )


    MenuObj.Show(
        x,
        y
    )
}



; ============================================================
; MENU ACTIONS
; ============================================================

OpenWinRAR(*)
{
    global WinRAR

    if FileExist(WinRAR)
        Run(WinRAR)
    else
        MsgBox(
            "WinRAR not found:`n`n" WinRAR,
            "RAR Drop Target",
            "Iconx"
        )
}


OpenScriptFolder(*)
{
    Run(A_ScriptDir)
}


ReloadScript(*)
{
    Reload()
}


ExitScript(*)
{
    ExitApp()
}


Source for all code: ChatGPT


_______________________________________


 
   
Pagini: 1  
Mergi la