How to store last active folder's filepath and paste it when the "Save as" window pops up

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

How to store last active folder's filepath and paste it when the "Save as" window pops up

28 Dec 2023, 11:55

I want a script that will do the following:
  1. Run in the background and whenever an Explorer folder is activated, store that folder's filepath in a variable. If a new folder is activated, the new filepath replaces the previous one, so there is always just one filepath stored.
  2. Whenever the "Save as" window pops up, the default save location (which is almost never what I need it to be) will be changed to the last stored filepath. The "Save as" window doesn't need to already show up on the desired location, something like Ctrl + L to select the address bar and pasting the stored filepath would be enough.
I'm still a noob in v1 and know next to nothing about v2, but I would like this to be written in v2 to get more used to it, so any help is appreciated.
User avatar
WarlordAkamu67
Posts: 220
Joined: 21 Mar 2023, 06:52

Re: How to store last active folder's filepath and paste it when the "Save as" window pops up

28 Dec 2023, 13:15

Hello.

This worked for me. Tested trying to save Adobe Illustrator files.

Code: Select all

#Requires AutoHotkey v2.0+
#SingleInstance

SetTimer(checkforFolder, 100)

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;
; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

get_Active_Explorer_Path() {
  explorerHwnd := WinActive("ahk_class CabinetWClass")
  if (explorerHwnd) {
    for window in ComObject("Shell.Application").Windows {
      if (window.hwnd==explorerHwnd) {
        return(window.Document.Folder.Self.Path)
}
}
}
  return(0)
}

; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ; ------------------------- ;

checkforFolder() {
  static folderPath := ""
  if (pathString := get_Active_Explorer_Path()) {
    folderPath := pathString
} else if (WinActive("Save As")) {
    Send("^l")
    Sleep(100)
    SendText(folderPath)
    Sleep(100)
    Send("{enter}")
    WinWaitClose("Save As")
}
  return
}
User avatar
WarlordAkamu67
Posts: 220
Joined: 21 Mar 2023, 06:52

Re: How to store last active folder's filepath and paste it when the "Save as" window pops up

28 Dec 2023, 13:35

I receive an error when closing the window. Will update when I can, or if someone else has a solution? I didn't want to just throw it into a try block.
Attachments
error.png
error.png (13.58 KiB) Viewed 770 times
User avatar
rommmcek
Posts: 1478
Joined: 15 Aug 2014, 15:18

Re: How to store last active folder's filepath and paste it when the "Save as" window pops up

29 Dec 2023, 15:37

Saving the path is not needed.
Spoiler
Attachments
nil.jpg
nil.jpg (631 Bytes) Viewed 141 times
Last edited by rommmcek on 18 Feb 2024, 07:48, edited 29 times in total.
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: How to store last active folder's filepath and paste it when the "Save as" window pops up

02 Jan 2024, 15:04

rommmcek wrote:
29 Dec 2023, 15:37
Saving the path is not needed.
Spoiler
Edited four times the post/code in the spoiler!
Thanks for the script, but it is way too advanced for me. Could you explain me in general terms what it is doing, particulartly why I need the Ctrl+F3 hotkey and why it sends Alt+d?
User avatar
rommmcek
Posts: 1478
Joined: 15 Aug 2014, 15:18

Re: How to store last active folder's filepath and paste it when the "Save as" window pops up

03 Jan 2024, 06:11

I asked ChatGPT...
Spoiler
P.s.: !d does essentially the same as ^l, but now I know how to do it without any of these.
P.p.s.: Script has potential flaws have new versions, no time now, report issues.
Last edited by rommmcek on 24 Jan 2024, 21:25, edited 1 time in total.
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: How to store last active folder's filepath and paste it when the "Save as" window pops up

24 Jan 2024, 08:44

rommmcek wrote:
29 Dec 2023, 15:37
Saving the path is not needed.
Spoiler
Edited four times the post/code in the spoiler!
Unfortunately it's not working for me. I made some changes to the script, but still no luck. Here is the exact same script I'm using:

Code: Select all

#Requires AutoHotkey v2+
#SingleInstance
SetTitleMatchMode 2

loop {
  WinWaitActive "Save ahk_class #32770"
  if (explrPath:=get_Active_Explorer_Path())&&!InStr(explrPath, "{") {
    Send "^l" ; !d does not select the address bar in my computer.
  try While !ControlGetVisible("Edit2", "A")
    Continue
  While explrPath!=ControlGetText("Edit2", "A")
    ControlSetText explrPath, "Edit2", "A" ;Address bar is more responsive
  ControlSend "{Enter}", "Edit2", "ahk_class #32770" ;Using Class is less general
  try While !ControlGetVisible("ToolbarWindow324", "A")
    Continue
  ControlFocus "Edit1", "A"
  } WinWaitNotActive "Save ahk_class #32770"
}

^F2:: { ;the hotkey for some 'default' path. >>> I already use ^F3 for something else, so I changed it to ^F2
  Static defaultPath:= "C:\Users\flima\Downloads" ;set your 'default' path
  if !FileExist(defaultPath)
    Return
  Send "^l"
  try While !ControlGetVisible("Edit2", "A")
    Continue
  While defaultPath!=ControlGetText("Edit2", "A")
    ControlSetText defaultPath, "Edit2", "A"
  ControlSend "{Enter}", "Edit2", "ahk_class #32770" ;Using Class is less general
  try While !ControlGetVisible("ToolbarWindow324", "A")
    Continue
  ControlFocus "Edit1", "A"
}

get_Active_Explorer_Path() {
  explorerHwnd:= WinExist("ahk_class CabinetWClass")
  if (explorerHwnd) {
    for window in ComObject("Shell.Application").Windows {
      if (window.hwnd==explorerHwnd) {
        return(window.Document.Folder.Self.Path)
      }
    }
  } return(0)
}
Perhaps I need a Sleep between lines 6 and 7? Right now the script doesn't even select the address bar of the "Save as" window when I try to download a chrome page. I checked it and it's class #32770.
User avatar
rommmcek
Posts: 1478
Joined: 15 Aug 2014, 15:18

Re: How to store last active folder's filepath and paste it when the "Save as" window pops up

24 Jan 2024, 21:48

For me on Win10 works your version too. On Win11 I can test it only in the weekend.
Now we have to debug it. First try to run the script as Administrator.
Then, start by sections as declared by ChatGPT (I edited it to be better shown).
E.g.:
Last edited by rommmcek on 28 Jan 2024, 06:37, edited 1 time in total.
User avatar
rommmcek
Posts: 1478
Joined: 15 Aug 2014, 15:18

Re: How to store last active folder's filepath and paste it when the "Save as" window pops up

28 Jan 2024, 05:03

Indeed on Win11 in some applications there was a problem and even on Win10 on some "asleepy" browser (that was not used for a prolonged period of time) the FileName Control did not focus after the Path setting...
I edited the code to be more robust yet still fast and updated the post above.

P.s.: Since e.g. launching Win Explorer from batch file, doesn't seem to support COM, I rewrote code based only on ControlGet/ControlSet. Sending Enter is dangerous, so is changing Folder via File-name Control.
Updated the post above, it should work on Win11 too (now only one safe version, but working on Open dialog too).
Last edited by rommmcek on 18 Feb 2024, 07:35, edited 1 time in total.
User avatar
rommmcek
Posts: 1478
Joined: 15 Aug 2014, 15:18

Re: How to store last active folder's filepath and paste it when the "Save as" window pops up

18 Feb 2024, 07:23

Finished the script above.
Hope I'll get some feedback from both Win10 & Win11.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: FalseShepard and 42 guests