autohotkeyahk2

Autohotkey v2 script | copy, open an application, paste


I would like to select text, press a hotkey to trigger an application open, paste the selected text into the newly opened application. In my case, my application is called everything, a file locator on windows. It was stored under D drive, and updated to the latest version.

Here is my code:

#f::
{
    A_Clipboard := ""
    ; Copy selected text to clipboard
    Send "^c"
    ClipWait ; Wait for 2 seconds for clipboard to contain text
    SetTitleMatchMode 2
    
    if WinExist("*everything*")
        {
        WinActivate 
        MsgBox "Everything was found."
        }
        ; Use the window found by WinExist.
    else {
        Run "D:\Downloads\everything\Everything.exe"
        Sleep 1000  
        SendInput A_Clipboard
        Send "^v"
        MsgBox "Everything was not found."
        }    
}

This script copies the selected text, opens the application. The problem is that it does not paste the selected text into Everything.

If I press ctrl+V manually, the selected text will paste into the search window in Everything. WinExist can not find Everything, I tested with a message box. The winTitle of Everything is "everything.exe".

Please help. Thank you.


Solution

  • Try it this way:

    #Requires AutoHotkey v2.0
    
    #f::
    { 
        A_Clipboard := ""
        ; Copy selected text to clipboard
        Send "^c"
        if !ClipWait(2)
        {
            MsgBox "The attempt to copy text onto the clipboard failed."
            return
        }   
        Run "D:\Downloads\everything\Everything.exe" " -s `"" A_Clipboard "`""
    }