AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Why doesn't this rename hotkey always work?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
tinaa



Joined: 25 Nov 2004
Posts: 41

PostPosted: Tue Jan 15, 2008 5:51 pm    Post subject: Why doesn't this rename hotkey always work? Reply with quote

I wrote a hotkey for renaming files in Windows explorer. Normally, pressing F2 will issue the rename command, but by default the entire filename is selected. I wanted a version where the filename, but not the extension, is selected. In some cases, the extension is not exactly 3 characters, so I wrote a version that will copy the filename to the clipboard so it can determine where the last period in the filename is, and highlight up to there. I also try and save and restore the current clipboard contents so it doesn't interfere with whatever is already there. Here's the hotkey:

Quote:
#F2::
ClipSaved := ClipboardAll ; Save the entire clipboard to a variable.

SendInput, {F2}^c ; Start rename/highlight name (if not already done so) and temporarily copy name to clipboard
oldfilename := Clipboard
StringGetPos, periodpos, oldfilename, ., R1
periodfromright := StrLen(oldfilename) - periodpos
SendInput, +{left %periodfromright%} ; unhighlight the filename up to the first period from the right

Clipboard := ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved = ; Free the memory in case the clipboard was very large.
return


Well, it seems to work sometimes, but sometimes it doesn't. I think there is a problem with the saving and restoring of the clipboard. If there's just some text in the clipboard, it usually seems to work. But if there's something else (eg. some graphics from Adobe Photoshop) then there seems to be a delay, then only the last character gets unhighlighted, and the clipboard contents are lost entirely.

Anyone know why it doesn't work consistently? Is there a way to fix it or a better way to implement this? (Preferably without having to use the clipboard.)

Thanks!
Back to top
View user's profile Send private message
Murp|e



Joined: 12 Jan 2007
Posts: 261
Location: Norway

PostPosted: Tue Jan 15, 2008 7:28 pm    Post subject: Reply with quote

I'd love to give you some debugging advice, the only problem is that your script worked like a charm on my computer (WinXP SP2). Two things though:

1. You could add the line "#IfWinActive, ahk_class CabinetWClas" before your first line "#F2::", so that the hotkey will only run if the active window is an explorer window.

2. You could consider using an alternative to the standard windows explorer. There are a whole bunch available and many of them have some very nice features that the standard explorer doesn't. Personally I like XYplorer, it happens to have the exact functionality that you're creating by default.
Back to top
View user's profile Send private message Visit poster's website
ManaUser



Joined: 24 May 2007
Posts: 906

PostPosted: Tue Jan 15, 2008 8:09 pm    Post subject: Reply with quote

This might make the script more reliable:
Code:
#F2::
ClipSaved := ClipboardAll ; Save the entire clipboard to a variable.

Clipboard = ; Clear clipboard
SendInput, {F2}^c ; Start rename/highlight name (if not already done so) and temporarily copy name to clipboard
ClipWait 1 ; Wait up to one second for text in the clipboard
oldfilename := Clipboard
StringGetPos, periodpos, oldfilename, ., R1
periodfromright := StrLen(oldfilename) - periodpos
SendInput, +{left %periodfromright%} ; unhighlight the filename up to the first period from the right

Clipboard := ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved = ; Free the memory in case the clipboard was very large.
return
Back to top
View user's profile Send private message
Murp|e



Joined: 12 Jan 2007
Posts: 261
Location: Norway

PostPosted: Tue Jan 15, 2008 8:59 pm    Post subject: Reply with quote

Nice advice ManaUser.
Back to top
View user's profile Send private message Visit poster's website
tinaa



Joined: 25 Nov 2004
Posts: 41

PostPosted: Tue Jan 15, 2008 10:17 pm    Post subject: Reply with quote

Thanks for the tips. Here's my current version:

Code:
#IfWinActive ahk_class CabinetWClass
   F2::   ;rename without modifying extension
      ClipSaved := ClipboardAll ; Save the entire clipboard to a variable.
      Clipboard = ; Clear clipboard
      SendInput {F2}^c ; Start rename/highlight name (if not already done so) and temporarily copy name to clipboard
      ClipWait 1 ; Wait up to one second for text in the clipboard
      oldfilename := Clipboard
      StringGetPos, periodpos, oldfilename, ., R1
      if (NOT ErrorLevel) {   ; the period was found
         periodfromright := StrLen(oldfilename) - periodpos
         SendInput, +{left %periodfromright%} ; unhighlight the filename up to the first period from the right
      }
      Clipboard := ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
      ClipSaved = ; Free the memory in case the clipboard was very large.
   return



This does seem to work better. I now don't have a problem with it not highlighting the correct part of the name. However, it seems depending on what's in the clipboard, there will be a pause before the name gets highlighted and the clipboard contents will be lost. Again, the limited tests I've tried seem to be no problem for text in the clipboard, but a Photoshop object will cause the pause and will be lost.

I'm wondering if

Code:
ClipSaved := ClipboardAll


just won't work for some types of clipboard objects. Here's a quote from the help file:

Quote:
If ClipboardAll cannot retrieve one or more of the data objects (formats) on the clipboard, they will be omitted but all the remaining objects will be stored. Retrieval is reattempted according to #ClipboardTimeOut (rather than being attempted only once).


although I'm not 100% I'm sure what this means. So are there some types of clipboard data that simply can't be saved by AutoHotKey?

Thanks!
Back to top
View user's profile Send private message
ManaUser



Joined: 24 May 2007
Posts: 906

PostPosted: Tue Jan 15, 2008 10:47 pm    Post subject: Reply with quote

Perhaps increasing #ClipboardTimeOut would help then. (Default is 1000, one second.)

Alternatively, there's another way you could get the text without using the clipboard at all.
Code:
#IfWinActive ahk_class CabinetWClass
   F2::   ;rename without modifying extension
      SendInput {F2} ; Start rename/highlight name (if not already done so)
      sleep 100
      ControlGetFocus WhichControl, A
      ControlGetText oldfilename, %WhichControl%, A

      StringGetPos, periodpos, oldfilename, ., R1
      if (NOT ErrorLevel) {   ; the period was found
         periodfromright := StrLen(oldfilename) - periodpos
         SendInput, +{left %periodfromright%} ; unhighlight the filename up to the first period from the right
      }
   return

Incidentally, In adition to CabinetWClass you probably want to make this work in ExploreWClass and Progman (Windows Explorer and the desktop respectively). See GroupAdd for how to make a hotkey apley to more than one class.
Back to top
View user's profile Send private message
tinaa



Joined: 25 Nov 2004
Posts: 41

PostPosted: Wed Jan 16, 2008 4:03 am    Post subject: Reply with quote

The #ClipboardTimeOut didn't help. I set it as long as 1 minute which just caused a minute pause before highlighting the filename but the Photoshop clipboard data was still lost. I guess Autohotkey can't read that data for some reason (or if it can, takes too long to be practical in this application).

However, I tried your other method and it seems to do the trick! I'll also add your suggestion about the groupadd. Thanks for your help!
Back to top
View user's profile Send private message
tank



Joined: 21 Dec 2007
Posts: 1033

PostPosted: Wed Jan 16, 2008 5:03 am    Post subject: Reply with quote

photoshop doesnt use the windows clipboard tho it can paste from it it uses a scratch file
_________________
Read this
Com
Automate IE7 with Tabs
Back to top
View user's profile Send private message
tinaa



Joined: 25 Nov 2004
Posts: 41

PostPosted: Wed Jan 16, 2008 6:33 am    Post subject: Reply with quote

tank wrote:
photoshop doesnt use the windows clipboard tho it can paste from it it uses a scratch file


Well, that may be true, but it certainly puts something in the clipboard. For instance, if I select some part of an image in Photoshop I can past it into a cell in Excel and the graphic appears as an embedded object in the Excel spreadsheet. Also, after running the previous version of the script above, a section of an image copied in Photoshop will no longer be pastable even from within Photoshop. It's possible Photoshop doesn't actually put the graphics in the clipboard, but there's at least some kind of pointer in there...
Back to top
View user's profile Send private message
Sakurako



Joined: 10 May 2007
Posts: 149
Location: China/ Canada

PostPosted: Thu Jan 17, 2008 4:44 pm    Post subject: Reply with quote

Try avoiding using the clipboard, and get the text of Edit4 instead ~
Code:
~F2::
Loop, 50
{
 ControlGetFocus, ControlName, A
 If (ControlName = "Edit4")
 {
  ControlGetText, FileName, Edit4, A
  KeyPressNum := StrLen(FileName) - InStr(FileName, ".", false, 0) + 1
  If InStr(FileName, ".") = 0
  KeyPressNum := 0
  SendInput, {End}^a+{Left %KeyPressNum%}
  break
 }
}
return
Edit: Sorry, didn't check carefully ... But this one should still work on operating system as Windows Vista ~
Back to top
View user's profile Send private message Visit poster's website
Murp|e



Joined: 12 Jan 2007
Posts: 261
Location: Norway

PostPosted: Thu Aug 21, 2008 4:19 pm    Post subject: Reply with quote

I stumbled across this link...
http://www.freewaregenius.com/2008/08/15/phlox-use-f2-in-xp-to-rename-files-without-overwriting-file-extensions/
...and thought it fit nicely to this thread.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group