2

My plan is to create an Autohotkey macro that saves the currently opened file in Notepad++ on my Desktop. I was able to make it open the "Save as" standard dialog box, but I don't know how I can make it go to the Desktop (Windows 7). I know the ClassNN of the combobox where you can select the folder, but when I send a 'D' for desktop keystroke it goes into one of my partitions rather than my Desktop.

A hack would be to send a click to the combobox that contains the main directories, then send 'up arrow' 50 times, then send a 'd', then {enter}, but this is not very elegant or stable I think.

Here is my current code:

#IfWinActive, ahk_class Notepad++
!F11::
    Send ^s
    WinWaitActive, Save As
    ;ControlSend, ComboBox1, {Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up}{Up} d, Save As
    ControlSend, Edit1, `%USERPROFILE`%\Desktop\%A_Hour% %A_Min% %A_Sec%, Save As
    ;ControlSend, Button2, {Space}, Save As 
Return
#IfWinActive

The following is are sample realizations that are contaminated still with SHIFT at random times:

%USERPROFILE%|DEsktop\02 27 40
5USERPROFILE%\desktop\02 30 25
1
  • 1
    You could also specify C:\Users\UserName\Desktop as the default open/save directory for Notepad++.
    – Karan
    Mar 8, 2013 at 20:07

2 Answers 2

1

Why not just give it the filename: %USERPROFILE%\Desktop\abc.txt

CODE:

MyFileName=abc
Send, `%USERPROFILE`%\Desktop\%MyFileName%`.txt

You have to escape the % signs and the dot.

You are not the only one with this problem. I can replicate it as well and there are several questions and no reliable answers around random shifts in a ControlSend on various fora.

One way to circumvent this is to use ControlFocus. Not as nice as ControlSend because you can potentially loose focus, but at least more reliable.

Sending a string is slow, so you could store the string in ClipBoard and send ^v to speed things up.

#IfWinActive, ahk_class Notepad
!F11::
    Send ^s
    WinWaitActive, Save As
    MyFileName=`%USERPROFILE`%\Desktop\%A_Hour% %A_Min% %A_Sec%
    ControlFocus, Edit1
    Send, %MyFileName%
    ;ControlSend, Button2, {Space}, Save As 
Return
#IfWinActive

OK, this is hopefully the last suggestion! This works flawlessly on my machine!

#IfWinActive, ahk_class Notepad
!F11::
    Send ^s
    WinWaitActive, Save As
        ControlSetText, Edit1, `%USERPROFILE`%\Desktop\%A_Hour% %A_Min% %A_Sec%, Save As
Return
#IfWinActive
7
  • i didnt know you could put an entire directory string in the filename. thanks again Mar 8, 2013 at 7:08
  • Actually, something odd is happening: The ControlSends are contaminated with keys that are SHIFT'ed. It appears SHIFT is active at random times, although it is not triggering the macro in any way (I also don't send any shift in the script itself): C:\uSers\, c:|USers\, C:|users, c:|uSers, c:|uSers\ c:\USers ....(in the US keyboard \ and | are the same key but the latter is activated with SHIFT). Any ideas what could be causing this? Mar 8, 2013 at 7:20
  • Check out my new code. Hope this solves your problem. Mar 8, 2013 at 7:25
  • thanks for the suggestion but it did not resolve the shift-issue. i have added sample output and my script in the original post. Mar 8, 2013 at 7:29
  • @Wuschelbeutel, Check out the modified code and let me know if this works better. Mar 8, 2013 at 8:00
0
  • I've written an AutoHotkey script that I've adapted for your purposes. It loops through the toolbar buttons and triggers the Desktop one. It sets the control text and applies select all.
  • It will also press the Desktop button, if you press F11, when you are already at the Save As Prompt, and update the timestamp. This additional functionality can be removed by commenting out the 'F11:: ;notepad++ (save as)' line.
  • The script can be adapted to work on other standard Windows Save As prompts, the old style like on Notepad++, and the newer ones like on Notepad (Windows 7).
  • Note: you will need to put the Acc.ahk file in your AutoHotkey\Lib folder. https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk
    [on right of screen right-click Raw, Save target as...]
  • Tested on Notepad++ 7.2.2.0 (Windows 7 64-bit).

-

;note: requires Acc.ahk library in AutoHotkey\Lib folder
;https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk
;on right of screen right-click Raw, Save target as...

#IfWinActive, ahk_class Notepad++
F11:: ;notepad++ - save, click Desktop button + set text
    Send ^s
    WinWaitActive, Save As

#IfWinActive, Save As ahk_class #32770 ahk_exe notepad++.exe
F11:: ;notepad++ - click Desktop button + set text

WinGet, hWnd, ID, A
WinGetClass, vWinClass, ahk_id %hWnd%
WinGet, vPName, ProcessName, ahk_id %hWnd%
if !(vWinClass = "#32770") OR !(vPName = "notepad++.exe")
Return

hCtl := ""

if !hCtl ;check for treeview e.g. Notepad Win 7
{
ControlGet, hCtl, Hwnd, , SysTreeView321, ahk_id %hWnd%
if hCtl
oAcc := Acc_Get("Object", "outline", 0, "ahk_id " hCtl)
}

if !hCtl ;check for toolbar e.g. Notepad Win XP
{
ControlGet, hCtl, Hwnd, , ToolbarWindow322, ahk_id %hWnd%
if hCtl
oAcc := Acc_Get("Object", "tool_bar", 0, "ahk_id " hCtl)
}

Loop, % oAcc.accChildCount
if (oAcc.accName(A_Index) = "Desktop")
if (1, oAcc.accDoDefaultAction(A_Index))
break

FormatTime, vDate, , HH mm ss
ControlSetText, Edit1, %vDate%, ahk_id %hWnd%
PostMessage, 0xB1, 0, -1, Edit1, ahk_id %hWnd% ;EM_SETSEL
Return
#IfWinActive

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .