topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 9:03 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Alt-LMBclick puts filename in GUI Edit element. Without using clipboard  (Read 5780 times)

huismus

  • Participant
  • Joined in 2012
  • *
  • default avatar
  • Posts: 7
    • View Profile
    • Donate to Member
I've created a small GUI with an Edit element where users can type a filename, this filename is submitted with a button and based on part of the filename string, the GUI gets expanded with different elements.

The problem is as follows: The typical filename to be typed is over 25 characters long and typing it is rather error prone. So... I'm working on adding a button that
hides the GUI, allows the user click to an explorer window and Alt-LMBclick on a filename. The filename then gets placed in the edit element and the GUI is unhidden. As the user may have valuable data on the clipboard, I do not want to use the clipboard for this.

I've been trying to use this code:
TitleCopyExplorer:
SetTimer, Explorecopy, 150
return
Explorecopy:
If WinActive ahk_group Explore
{
explorerClass = ExploreWClass

~!Lbutton::

WinGetClass explorerClass, A
ControlGet, selectedFile, List, Selected Col1, SysListView321,ahk_class %explorerClass%
msgbox %selectedFile%
}
return
(modified from: this forum topic)

However, this just returns an empty string in the msgbox.

Thanks for your help.
Huismus


MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
A simpler approach may be
Code: Autohotkey [Select]
  1. ClipSave := ClipboardAll
  2. ; do whatever then
  3. Clipboard := ClipSave

huismus

  • Participant
  • Joined in 2012
  • *
  • default avatar
  • Posts: 7
    • View Profile
    • Donate to Member
Thanks Miles, That's a good idea. But leads me to two new questions:
- Would this require a lot of memory?
- In VBA you'd set an object to Nothing as good coding practice. Is this possible in AHK?

Also, I still do not understand why my code snippet returns an empty string.

BTW: I also tried the following code of Skwire:
#c::
{
    SendInput, ^c ; Copy image path to clipboard.

    SplitPath, Clipboard, myFileName ; Get filename from the path.

    Clipboard := "http://www.site.com/" . myFileName ; Concatenate string and copy it to the clipboard.

}
Return

Copied from this forum topic; This also returns an empty string... I must be doing something very basic wrong. :huh:

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
See this link for freeing the saved clipboard memory
ClipSaved =

Also when you use Send ^c you need to give a delay to wait for the clipboard to have the new data.  I believe if you use no number it waits forever.  See ClipWait


huismus

  • Participant
  • Joined in 2012
  • *
  • default avatar
  • Posts: 7
    • View Profile
    • Donate to Member
Thanks for the help MilesAhead! I'm now using:
Send ^c
ClipWait, 1
clipboard = %clipboard%
MsgBox %clipboard%
The difference between an empty msgbox and a msgbox with the title was:
clipboard = %clipboard%

Case closed :up:

 >:( grmph...

Still having issues with it after I added the clipsaved option. The script works fine like this:
MButton::
;ClipSaved := ClipboardAll
;ClipWait, 0.2
SetTimer, CopyFileName, Off
;clipboard =
click
Send ^c
ClipWait, 0.2
clipboard = %clipboard%
MsgBox %clipboard%
;clipboard:= ClipSaved
Return

But I once again get an empty string in the message box if I uncomment the clipboard related lines (3x). Somehow it seems like messagebox is slow and the script goes ahead changing the content of the clipboard before the msgbox is rendered??  :huh:
BTW: As you see I changed the hotkey from !Lbutton to MButton.
« Last Edit: November 03, 2015, 07:02 AM by huismus »

MilesAhead

  • Supporting Member
  • Joined in 2009
  • **
  • Posts: 7,736
    • View Profile
    • Donate to Member
I think you may have better results if you give more of a max wait in ClipWait calls.  In most cases I use 2 seconds.  If the data is ready earlier it returns.  It rarely sits for 2 seconds but in those cases when there may be stuff happening on the system allowing up to 2 seconds seems to be enough at least on my Laptop.

Try using 2 or even 3 seconds and see if it is more reliable.

Another thing is it is a good idea to check results after calls.  Many scripts on boards or example code skip the error checking to keep it small and easily understood.  For instance you can check if you got nothing from the clipboard by

Code: Autohotkey [Select]
  1. ClipContents := Clipboard
  2. If (! ClipContents)
  3. {
  4.    ;do something here if I draw a blank
  5. }
  6. else
  7. {
  8.   ;process the stuff
  9. }

Also I think it is generally preferable to use expression mode when possible.  Stuff like
"clipsaved =" is ubiquitous and an easy way to clear memory.  But on the whole the more you can keep to expression mode the more stuff acts as you would expect.  Plus you can take some shortcuts similar to how it is done in C since an assignment is itself an expression you can init a bunch of variables in one line of code such as "x := y := w := h := -1" before using those in a call to WinGetPos and the like.

« Last Edit: November 03, 2015, 09:07 AM by MilesAhead »

huismus

  • Participant
  • Joined in 2012
  • *
  • default avatar
  • Posts: 7
    • View Profile
    • Donate to Member
Thank you MilesAhead,

Sorry for the late reply, I was distracted by other projects. It's good to know the clipwait returns earlier if the data is ready. Also, thank you for the tips for better code.

My code now works fine for it's original purpose, using GUIControl. However for MsgBox, i really need a clipwait of 2 seconds. It seems like MsgBox is much slower then GUIControl.

Thanks again!
Andre