Advertisement
Najeebsk

MoveFileHere.ahk

Nov 26th, 2022
1,327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; <COMPILER: v1.1.34.04>
  2. #NoEnv
  3. #SingleInstance, Force
  4. SetBatchLines, -1
  5. ;#NoTrayIcon
  6. SetWorkingDir %A_ScriptDir%  
  7. FileInstall , F:\APPS\MOVEFILEHERE\MoveFileHere.ahk,MoveFileHere.ahk
  8. FileSetAttrib +H, %A_ScriptDir%\MoveFileHere.ahk, 2
  9. ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  10. ListLines, Off
  11. SetWinDelay, -1
  12. SetControlDelay, -1
  13. ; AutoHotkey tool to quickly move newest file from Downloads to the active File Explorer folder in Windows 7&10.
  14. ; Version 2022-01-09  -  NSK Software World v3  -  by Najeeb Shah Khan  
  15. ; Read settings from ini file
  16. SplitPath, A_ScriptName, , , , vNameNoExt
  17. vIniFile := A_ScriptDir "\" vNameNoExt ".ini"
  18. IniRead, vHotkey      , % vIniFile, Settings, Hotkey      , %A_Space%
  19. IniRead, vSourceFolder, % vIniFile, Settings, SourceFolder, %A_Space%
  20. IniRead, vStartMessage, % vIniFile, Settings, StartMessage, %A_Space%
  21. ; Default values
  22. if !vHotkey
  23.   vHotkey       := "F7"
  24. if !vSourceFolder
  25.   vSourceFolder := "C:\Users\" A_UserName "\Downloads"
  26. if (vStartMessage != "off")
  27.   vStartMessage := ""
  28.  
  29. ; create hotkey for when Explorer is active
  30. Hotkey, IfWinActive, ahk_class CabinetWClass
  31. Try
  32. {
  33.   Hotkey, %vHotkey%, move_file_here, on
  34. }
  35. catch
  36. {
  37.   ; on error open ini file for editing hotkey and show error popup
  38.   Run % vIniFile
  39.   MsgBox, 5, % vNameNoExt, % "Error: Invalid hotkey: " vHotkey
  40.   IfMsgBox, Retry
  41.   {
  42.     Reload
  43.     Return
  44.   }
  45.   else
  46.     ExitApp
  47. }
  48.  
  49. ; trim any trailing backslash in folder path
  50. vSourceFolder := RTrim(vSourceFolder, "\")
  51.  
  52. ; verify source folder exists
  53. if !InStr(FileExist(vSourceFolder), "D")
  54. {
  55.   Run % vIniFile
  56.   MsgBox, 5, % vNameNoExt, % "Not found: `n" vSourceFolder
  57.   IfMsgBox, Retry
  58.   {
  59.     Reload
  60.     Return
  61.   }
  62.   else
  63.     ExitApp
  64. }
  65.  
  66. ; write settings
  67. IniWrite, % vHotkey      , % vIniFile, Settings, Hotkey
  68. IniWrite, % vSourceFolder, % vIniFile, Settings, SourceFolder
  69.  
  70. ; tray menu
  71. Menu, Tray, Icon, MoveFileHere.exe
  72. Menu, Tray, NoStandard
  73. Menu, Tray, Add, About, menu_about
  74. Menu, Tray, Default, About
  75. Menu, Tray, Add, Settings, menu_settings
  76. Menu, Tray, Add, Exit, menu_exit
  77.  
  78. vMessage =
  79. (
  80. In File Explorer press [%vHotkey%] to move the latest file from [%vSourceFolder%] to the active folder.
  81.  
  82. Edit [%vNameNoExt%.ini] to change hotkey and source folder.
  83.  
  84. https://github.com/nod5/MoveFileHere
  85. )
  86.  
  87. ; show message on first run only
  88. if (vStartMessage != "off")
  89. {
  90.   IniWrite, % "off", % vIniFile, Settings, StartMessage
  91.   MsgBox,, % vNameNoExt, % vMessage
  92. }
  93. return
  94.  
  95. menu_about:
  96.  MsgBox,, % vNameNoExt, % vMessage
  97. Return
  98.  
  99. menu_settings:
  100.  Run % vIniFile
  101.   sleep 100
  102.   MsgBox, 1. Edit settings`n2. Save and close file`n3. Click OK
  103.   Reload
  104. Return
  105.  
  106. menu_exit:
  107.  ExitApp
  108. Return
  109.  
  110. move_file_here:
  111.  if vLock
  112.     goto cancel_move
  113.   ; lock hotkey until this file move finishes
  114.   vLock := 1
  115.  
  116.   ; clear tooltip, timers and variables from previous run
  117.   ToolTip
  118.   SetTimer, tooltip_off, off
  119.   SetTimer, cancel_move, off
  120.   vDate := vFile := ""
  121.  
  122.   ; get latest (most recently modified) file in source folder
  123.   Loop, Files, %vSourceFolder%\*.*
  124.   {
  125.     loopfile := A_LoopFileFullPath
  126.     if (A_LoopFileTimeModified > vDate)
  127.     {
  128.       vDate := A_LoopFileTimeModified
  129.       vFile := A_LoopFileFullPath
  130.     }
  131.   }
  132.  
  133.   ; cancel if no file to move
  134.   if !vFile
  135.   {
  136.     Tooltip,  `n   No file to move    `n  `n  
  137.     ToggleHotkeys("off")
  138.     SetTimer, tooltip_off, 2000
  139.     return
  140.   }
  141.  
  142.   ; show tooltip with file path
  143.   ToolTip,  `n   Move:`n   %vFile%   `n  `n  
  144.  
  145.   ; enable hotkeys: confirm move (Space, Enter, Left Click), cancel move (Esc)
  146.   ; function: enable/disable hotkeys for while confirm/cancel tooltip shows
  147.   ToggleHotkeys("on")
  148.   ; time out action after 2s
  149.   SetTimer, cancel_move, 2000
  150. return
  151.  
  152.  
  153.  
  154. ; move file to current folder in active Explorer window
  155. hotkey_enter:
  156. hotkey_lbutton:
  157. hotkey_space:
  158.  ; File Explorer must be active
  159.   If !WinActive("ahk_class CabinetWClass")
  160.     return
  161.   ; get current folder path
  162.   vFolder := ActiveExplorerPath()
  163.   ; trim any trailing backslash (in case active folder is a root drive e.g. C:\ )
  164.   vFolder := RTrim(vFolder, "\")
  165.  
  166.   If !FileExist(vFolder) or (vFolder = vSourceFolder)
  167.   {
  168.     Tooltip,  `n   Error: cannot move to %vSourceFolder%    `n  `n  
  169.     ToggleHotkeys("off")
  170.     SetTimer, tooltip_off, -2000
  171.     return
  172.   }
  173.  
  174.   ; disable timeout timer and confirm/cancel hotkeys
  175.   SetTimer, cancel_move, Off
  176.   ToggleHotkeys("off")
  177.  
  178.   ; prompt for confirmation if overwrite
  179.   SplitPath, vFile, vFilename
  180.   if FileExist(vFolder "\" vFilename)
  181.   {
  182.     MsgBox,1,Overwrite?, % folder "\" vFilename "`n`nFile already exists. Overwrite?"
  183.     IfMsgBox, Cancel
  184.       goto cancel_move
  185.   }
  186.  
  187.   ; move file
  188.   FileMove, % vFile , % vFolder, 1
  189.   Tooltip % "  `n   ** " (ErrorLevel ? "Move Error!" : "Moved" ) " **   `n  "
  190.   SetTimer, tooltip_off, -1500
  191.   ; select moved file in active Explorer window
  192.   SelectNamedFileExplorer(vFilename)
  193.   ; all finished, so remove lock on hotkey
  194.   vLock := ""
  195. return
  196.  
  197. hotkey_esc:
  198. cancel_move:
  199.  ToggleHotkeys("off")
  200.   SetTimer, cancel_move, off
  201. tooltip_off:
  202.  ToolTip
  203.   vLock := ""
  204. return
  205.  
  206.  
  207. ; function: enable/disable hotkeys for the confirm/cancel tooltip
  208. ToggleHotkeys(vState := "on")
  209. {
  210.   Hotkey, IfWinActive, ahk_class CabinetWClass
  211.   Hotkey, Space   , hotkey_space    , % vState
  212.   Hotkey, Enter   , hotkey_enter    , % vState
  213.   Hotkey, Lbutton , hotkey_lbutton  , % vState
  214.   Hotkey, Esc     , hotkey_esc      , % vState
  215. }
  216.  
  217.  
  218. ; function: get active window File Explorer folder path
  219. ; ref https://docs.microsoft.com/en-us/windows/win32/shell/folderitem-path
  220. ActiveExplorerPath()
  221. {
  222.   vHwnd := WinActive("ahk_class CabinetWClass")
  223.   if (vHwnd)
  224.     for window in ComObjCreate("Shell.Application").Windows
  225.     {
  226.       vWindowHwnd := ""
  227.       try vWindowHwnd := window.HWND
  228.       if (vWindowHwnd = vHwnd)
  229.       {
  230.         path := window.Document.Folder.Self.Path
  231.         ; if start with "::" (for example Control Panel window) then return nothing
  232.         return SubStr(path, 1, 2) = "::" ? "" : path
  233.       }
  234.     }
  235. }
  236.  
  237.  
  238. ; function: SelectNamedFileExplorer(filename, [hwnd])
  239. ; select a single filename in File Explorer window using ComObj and Folder.ParseName
  240. ; acts on HWND Explorer window or (default) active Explorer window
  241. ; much faster than for-loop over each FolderItem if folder has many files
  242. ; https://docs.microsoft.com/en-us/windows/desktop/shell/folder
  243. ; https://docs.microsoft.com/en-us/windows/desktop/shell/folder-parsename
  244. SelectNamedFileExplorer(vFilename, vHwnd := "")
  245. {
  246.   ; HWND from parameter or (default) active Explorer
  247.   vHwnd := vHwnd ? vHwnd : WinExist("A")
  248.   if !vHwnd
  249.     return
  250.  
  251.   ; must be Explorer
  252.   WinGetClass, vClass, % "ahk_id " vHwnd
  253.   if (vClass != "CabinetWClass")
  254.     return
  255.  
  256.   for window in ComObjCreate("Shell.Application").Windows
  257.   {
  258.     vWindowHwnd := ""
  259.     try vWindowHwnd := window.HWND
  260.     if (vWindowHwnd != vHwnd)
  261.       continue
  262.  
  263.     sfv   := window.Document
  264.     ; https://docs.microsoft.com/en-us/windows/desktop/shell/folder
  265.     ; https://docs.microsoft.com/en-us/windows/desktop/shell/folderitems
  266.     item := sfv.Folder.ParseName(vFilename)
  267.     ; select item (1), deselect all other (4), ensure item in view (8), focus item (16)
  268.     ; ref https://docs.microsoft.com/en-us/windows/desktop/shell/shellfolderview-selectitem
  269.     ; use try to avoid exception if file already deleted/renamed
  270.     try sfv.SelectItem(item, 1+4+8+16)
  271.     sfv := item := ""
  272.     break
  273.   }
  274.   window := ""
  275.   return
  276. }
  277. ;-=-=-=-=-=-=-=-=-=-=-=-=-=-HELP-=-=-=-=-=-=-=-=-=-=-=-=-=-
  278. ^H::
  279.     MsgBox, , About MOVE Downloads Folder To,
  280.     (
  281.         This program will MOVE Downloads Folder To, via hotkey.
  282.  
  283.         The defined hotkeys are:
  284.        F7    AutoHotkey tool to quickly move newest file from Downloads Folder            to the Active File Explorer folder in Windows 7&10
  285.  
  286.         Works on both Windows XP and Windows 7
  287.         By Najeeb Shah Khan 2022
  288.     )
  289. Return
  290. ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  291. #R::Reload  ;<--  ~ Reload Script ~
  292. #S::Suspend ;<--  ~ Suspend Script ~
  293. #P::Pause   ;<--  ~ Pause Script ~
  294. #M::WinMinimize, ;<--  ~ Minimize Script ~
  295. ESC::ExitApp     ;<--  ~ Exit Script ~
  296. ;=-=-=-=-=-=-=-=-=-=-=-=-=-=-END=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement