Sign in to follow this  
Followers 0
user52

Read File and Create New 'Case' Accordingly

3 posts in this topic

question: how do i create unique cases in a loop?

the idea: check a folder for *.txt files, and create a newmenuitem for each item found so when a user selects the context menu item it will run that specific function (aka paste the selected text)

also: see the comments, and thanks for your help!

#include <GUIConstants.au3>
#include <File.au3>
;#NoTrayIcon

HotKeySet("!x", "Show")

Dim $Context
ContextMenu()

Func ContextMenu()
    $DW = (@DesktopWidth - 2)/2
    $DH = (@DesktopHeight - 2)/2
    $Context = GUICreate("Context Menu", 2, 2, -1, -1, $WS_POPUP)
    
    ;Inserts
        $contextmenu = GUICtrlCreateContextMenu()
        $insert = GUICtrlCreateMenu ("Insert", $contextmenu)
            
            ;loop to find files in a folder and create new menu items based on the the name of the files found
            ;also, create a unique 'Case' for when each new menu item found that, when selected, reads a specified file and pastes the read
                $search = FileFindFirstFile(@ScriptDir & "\*.txt")
                
                ; Check if the search was successful
                If $search = -1 Then
                    MsgBox(0, "Error", "No files found")
                    ;Exit
                EndIf
                
                While 1
                    $file = FileFindNextFile($search) 
                    If @error Then ExitLoop
                    
                    ;having trouble here giving a unique variable name each loop
                    $uniquenamegoeshere = GUICtrlCreateMenuitem($file,$insert)
                    
                    ;is this the right way to do this??? or should this go further below in the While loop
                    $msg = GUIGetMsg()
                    Select
                        Case $msg = $uniquenamegoeshere
                        ;read file to be inserted
                        $insert = @ScriptDir & "\a.txt"
                        
                        $file = FileOpen($insert,0)
                        $read = FileRead($insert)
                        FileClose($file)
                        Insert($read)
                    EndSelect
                    
                WEnd
                
                ; Close the search handle
                FileClose($search)
            
        $exititem = GUICtrlCreateMenuitem ("Exit", $contextmenu)
        
        $pos = MouseGetPos()
        $posH = $pos[0]
        $posV = $pos[1]
    
    GUISetState(@SW_HIDE, $Context)
    While 1
        $msg = GUIGetMsg()
        Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $exititem
            Exit
        Case $msg = $uniquenamegoeshere
            ;read file to set signature
            $file = @ScriptDir & "\a.txt"
            FileOpen($file,0)
            $sig = FileRead($file)
            FileClose($file)
            
            Insert($sig)
        Case Else
            ;;
        EndSelect
    WEnd
EndFunc ; => ContextMenu

Func Show()
    GUISetState(@SW_SHOW, $Context)
    ControlClick("Context Menu", "", "", "right", 1)
EndFunc

Func Hide()
    GUISetState(@SW_HIDE, $Context)
EndFunc

Func Insert($a)
    ;hide menu
    Hide()
    
    ;store user's current clipboard
    $temp = ClipGet()
    
    ;send signature text to clipboard
    ClipPut($a)
    
    ;print clipboard
    Send("^v")
    
    ;set user's clipboard back
    ClipPut($temp)
EndFunc

Share this post


Link to post
Share on other sites



Look into the Assign() function in the helpfile.

Hallman

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks for the help. When i run the program and use the Assign() function, it calls the Insert() function right away. I'm trying to make it call the Insert() function when the user clicks on a certain context menu item. Here is what i've been trying:

$uniquenamegoeshere = GUICtrlCreateMenuitem("awefljawef",$contextmenu)
Assign($uniquenamegoeshere,Insert($read))

Func Insert($a)
    ;hide menu
    Hide()
    
    ;store user's current clipboard
    $temp = ClipGet()
    
    ;send signature text to clipboard
    ClipPut($a)
    
    ;print clipboard
    Send("^v")
    
    ;set user's clipboard back
    ClipPut($temp)
EndFunc

.

Edited by user52

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