AutoHotkey: Makedir and file create history: Difference between revisions

From TotalcmdWiki
Jump to navigation Jump to search
No edit summary
 
(Added category AutoHotkey scripts)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  /*
  /*
   
   
  This script allows use of directory creation  history in TC
  This script allows you to use directories and files creation
  dir creation dialog. History depth is defined by the "depth"
  history in the F7 and Shift-F4 dialogs respectively. History
  parameter in the ini-file created in the script dir.
  depth is defined by the "depth" parameters under appropriate
  sections in the ini-file created in the script dir.
   
   
  Up/Down keys change current dir name to appropriate from the
  Up/Down : scrolls history in the edit box.
  history.
  Alt-Down: shows a drop-down history list under the edit box.
   
   
  Alt+Down shows a drop-down history list
  Note: works ONLY via keypressing!
   
   
  (c) Jungle, February 2006.
  Written by Jungle, February 2006.
  Send your comments and suggestions to: jungle-80@mail.ru
  Send your comments and suggestions to: jungle-80@mail.ru
   
   
Line 17: Line 18:
  ;----------
  ;----------
  ;
  ;
  $Up::
  $+F4::
   ifWinActive, ahk_class TSTDTREEDLG, ,ftp
   ; create file dialog is being opened
   {
   if WinActive("ahk_class TTOTAL_CMD")
     inc = -1
     hist_sec = file
    GoSub take_hist_dir
  else ifWinNotActive, ahk_Class TSTDTREEDLG
     Return
     hist_sec =
  }
   
   
   Send {Up}
   Send +{F4}
  Return
  Return
  ;
  ;
  ;----------
  ;----------
  ;
  ;
  $Down::
  $F7::
   ifWinActive, ahk_class TSTDTREEDLG, ,ftp
   ; create dir dialog is being opened
   {
   if WinActive("ahk_class TTOTAL_CMD")
     inc = 1
     hist_sec = dir
    GoSub take_hist_dir
  else ifWinNotActive, ahk_Class TSTDTREEDLG
     Return
     hist_sec =
  }
   
   
   Send {Down}
   Send {F7}
  Return
  Return
  ;
  ;
  ;----------
  ;----------
  ;
  ;
  ~!Down::
  ~Up::
   ifWinActive, ahk_class TSTDTREEDLG, ,ftp
   if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
     GoSub tc_show_history_list
     ; scroll up history in the edit box
    take_hist( -1 )
  Return
  Return
  ;
  ;
  ;----------
  ;----------
  ;
  ;
  $Esc::
  ~Down::
   ; if history was loaded, but dir creation cancelled
   if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
  ifWinActive, ahk_class TSTDTREEDLG, ,ftp
    ; scroll down history in the edit box
     GoSub array_free
     take_hist( 1 )
   
   
   Send {Esc}
Return
;
;----------
;
~!Down::
   if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
    ; display history drop-down list
    GoSub tc_show_history_list
  Return
  Return
  ;
  ;
Line 68: Line 76:
   }
   }
   ; if Enter key pressed in dir creation dialog
   ; if Enter key pressed in dir creation dialog
   else
   else if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
   ifWinActive, ahk_class TSTDTREEDLG, ,ftp
   {
     GoSub write_dir_history
    ControlGetFocus, fc, A
    ; if Enter was pressed on the "Cancel" button
    if ( fc = "TButton1" )
      GoSub clear_mem
     else
      GoSub write_history
  }
   Send {Enter}
   Send {Enter}
  Return
  Return
Line 78: Line 91:
  ;
  ;
  ~LButton::
  ~LButton::
   ; if Button "OK" clicked instead of Enter
   if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
  ifWinActive, ahk_class TSTDTREEDLG, ,ftp
   {
   {
     MouseGetPos, , , ,m_ctrl
     MouseGetPos, , , ,m_ctrl
    ; if Button "OK" clicked instead of Enter
     if ( m_ctrl = "TButton2" )
     if ( m_ctrl = "TButton2" )
       GoSub write_dir_history
       GoSub write_history
    ; if Button "Cancel" clicked instead of Esc
    else if ( m_ctrl = "TButton1" )
      GoSub clear_mem
   }
   }
  Return
  Return
Line 89: Line 105:
  ;----------
  ;----------
  ;
  ;
  write_dir_history:
  $Space::
  if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
  {
    ControlGetFocus, fc, A
    ; if Space was pressed on the "OK" button
    if ( fc = "TButton2" )
      GoSub write_history
    ; if Space was pressed on the "Cancel" button
    else if ( fc = "TButton1" )
      GoSub clear_mem
  }
  Send {Space}
Return
;
;----------
;
$Esc::
  ; if Escape was pressed
  if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
    GoSub clear_mem
  Send {Esc}
Return
;
;----------
;
write_history:
   if ( dir_win_id <> space )
   if ( dir_win_id <> space )
     Return
     Return
Line 99: Line 142:
   
   
   ; Get new dir name...
   ; Get new dir name...
   ControlGetText, cur_dir, TEdit1
   ControlGetText, cur_text, TEdit1
   ; if empty then exit
   ; if empty then exit
   
   
   if ( cur_dir = space )
   if ( cur_text = space )
  {
    ; free memory
    GoSub array_free
     Return ; ...and exit if it is blank
     Return ; ...and exit if it is blank
  }
   
   
   ; if history is not loaded, load it
   ; if history is not loaded, load it
Line 115: Line 154:
   ; if history contains new dir name, then return
   ; if history contains new dir name, then return
   Loop, %item_cnt%
   Loop, %item_cnt%
     if ( cur_dir = dir_hist%A_Index% )
     if ( cur_text = hist_array%A_Index% )
       Return
       Return
   
   
   ; write history depth value into the ini file
   ; write history depth value into the ini file
   IniWrite, %hist_depth%, %A_ScriptDir%\mkdir_hist.ini, history, depth
   IniWrite, %hist_depth%, %A_ScriptDir%\history.ini, %hist_sec%, depth
   ; write first dir history entry into the ini file
   ; write first dir history entry into the ini file
   IniWrite, %cur_dir%, %A_ScriptDir%\mkdir_hist.ini, history, dir1
   IniWrite, %cur_text%, %A_ScriptDir%\history.ini, %hist_sec%, item1
 
   ; if history count is equal to history depth,
   ; if history count is equal to history depth,
   ; last history entry will be deleted and each
   ; last history entry will be deleted and each
Line 136: Line 175:
   Loop, %cnt%
   Loop, %cnt%
   {
   {
     IniWrite, % dir_hist%A_Index%, %A_ScriptDir%\mkdir_hist.ini, history, dir%pos%
     IniWrite, % hist_array%A_Index%, %A_ScriptDir%\history.ini, %hist_sec%, item%pos%
     pos++
     pos++
   }
   }
   
   
  GoSub array_free
 
  Return
  Return
  ;
  ;
  ;----------
  ;----------
  ;
  ;
  array_free:
  take_hist( inc )
   ; clear history array (free memory)
{
   Loop, %item_cnt%
   ; ensure all variables below are global
    dir_hist%A_Index% =
   global
   
   
  item_cnt =
  item_pos =
Return
;
;----------
;
take_hist_dir:
   ; if history was not loaded then try to load
   ; if history was not loaded then try to load
   if ( item_cnt = space )
   if ( item_cnt = space )
   {
   {
     GoSub read_hist_file
     GoSub read_hist_file
     if ( item_cnt < 1 )
     if ( item_cnt < 1 )
       Return
       Return
Line 175: Line 206:
   
   
   ; place history item to the edit
   ; place history item to the edit
   ControlSetText, TEdit1, % dir_hist%item_pos%
   ControlSetText, TEdit1, % hist_array%item_pos%
   ; set caret position to the end
   ; set caret position to the end
   ControlSend, TEdit1, {End}
   ControlSend, TEdit1, {End}
  Return
   
  Return
}
  ;
  ;
  ;----------
  ;----------
Line 196: Line 229:
     c_x += w_x
     c_x += w_x
     c_y += w_y + c_h
     c_y += w_y + c_h
    c_w -= 2
     Gui, Margin, 0, 0
     Gui, Margin, 0, 0
     Gui, -0x0C00000 +Delimiter`n
     Gui, -Caption +Border +Delimiter`n
     Gui, Add, ListBox, +AltSubmit w%c_w% h100 vDirChoice gHistBox, %dir_hist1%
     Gui, Add, ListBox, +AltSubmit -E0x200 w%c_w% h100 vDirChoice gHistBox, %hist_array1%
     pos = 2
     pos = 2
     Loop, % item_cnt - 1
     Loop, % item_cnt - 1
     {
     {
       GuiControl, ,ListBox1, % dir_hist%pos%
       GuiControl, ,ListBox1, % hist_array%pos%
       pos++
       pos++
     }
     }
Line 217: Line 251:
  read_hist_file:
  read_hist_file:
   ; read history depth value from the ini-file
   ; read history depth value from the ini-file
   IniRead, hist_depth, %A_ScriptDir%\mkdir_hist.ini, history, depth, 20
   IniRead, hist_depth, %A_ScriptDir%\history.ini, %hist_sec%, depth, 20
   if (hist_depth = space)
   if (hist_depth = space)
     hist_depth = 20
     hist_depth = 20
Line 226: Line 260:
   Loop, %hist_depth%
   Loop, %hist_depth%
   {
   {
     IniRead, dir, %A_ScriptDir%\mkdir_hist.ini, history, dir%A_Index%
     IniRead, item, %A_ScriptDir%\history.ini, %hist_sec%, item%A_Index%
     ; skip empty entries
     ; skip empty entries
     if ( dir <> "ERROR" AND dir <> space)
     if ( item <> "ERROR" AND item <> space)
     {
     {
       item_cnt++
       item_cnt++
       dir_hist%item_cnt% = %dir%
       hist_array%item_cnt% = %item%
     }
     }
   }
   }
   
   
   if ( item_cnt > 0 )
   if ( item_cnt > 0 )
  ; current history item position
    ; current history item position
     item_pos = 0
     item_pos = 0
   
   
Line 292: Line 326:
     ; clear windows id variable
     ; clear windows id variable
     dir_win_id =
     dir_win_id =
    Gosub clear_mem
   }
   }
  Return
  Return
;
;----------
;
clear_mem:
  ; clear history array (free memory)
  Loop, %item_cnt%
    hist_array%A_Index% =
  ; clear other vars
  item_cnt =
  item_pos =
  hist_sec =
Return
Warning: Redefining the Enter and Space keys results in that they will not work as a terminator for hotstrings, see here: http://www.autohotkey.com/forum/viewtopic.php?t=4232




Back to [[AutoHotkey]]
{{translated|AutoHotkey: Historie beim Erstellen neuer Verzeichnisse oder Dateien|AutoHotkey}}
[[Category:AutoHotkey scripts|Makedir and file create history]]

Latest revision as of 00:12, 1 June 2008

/*

This script allows you to use directories and files creation
history in the F7 and Shift-F4 dialogs respectively. History
depth is defined by the "depth" parameters under appropriate
sections in the ini-file created in the script dir.

Up/Down : scrolls history in the edit box.
Alt-Down: shows a drop-down history list under the edit box.

Note: works ONLY via keypressing!

Written by Jungle, February 2006.
Send your comments and suggestions to: jungle-80@mail.ru

*/
;
;----------
;
$+F4::
  ; create file dialog is being opened
  if WinActive("ahk_class TTOTAL_CMD")
    hist_sec = file
  else ifWinNotActive, ahk_Class TSTDTREEDLG
    hist_sec =

  Send +{F4}
Return
;
;----------
;
$F7::
  ; create dir dialog is being opened
  if WinActive("ahk_class TTOTAL_CMD")
    hist_sec = dir
  else ifWinNotActive, ahk_Class TSTDTREEDLG
    hist_sec =

  Send {F7}
Return
;
;----------
;
~Up::
  if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
    ; scroll up history in the edit box
    take_hist( -1 )

Return
;
;----------
;
~Down::
  if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
    ; scroll down history in the edit box
    take_hist( 1 )

Return
;
;----------
;
~!Down::
  if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
    ; display history drop-down list
    GoSub tc_show_history_list
Return
;
;----------
;
$Enter::
  ; if Enter key pressed in dir history list
  if WinActive("ahk_class AutoHotkeyGUI")
  {
    GoSub GuiSubmit
    Return
  }
  ; if Enter key pressed in dir creation dialog
  else if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
  {
    ControlGetFocus, fc, A
    ; if Enter was pressed on the "Cancel" button
    if ( fc = "TButton1" )
      GoSub clear_mem
    else
      GoSub write_history
  }
  Send {Enter}
Return
;
;----------
;
~LButton::
  if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
  {
    MouseGetPos, , , ,m_ctrl
    ; if Button "OK" clicked instead of Enter
    if ( m_ctrl = "TButton2" )
      GoSub write_history
    ; if Button "Cancel" clicked instead of Esc
    else if ( m_ctrl = "TButton1" )
      GoSub clear_mem
  }
Return
;
;----------
;
$Space::
  if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
  {
    ControlGetFocus, fc, A
    ; if Space was pressed on the "OK" button
    if ( fc = "TButton2" )
      GoSub write_history
    ; if Space was pressed on the "Cancel" button
    else if ( fc = "TButton1" )
      GoSub clear_mem
  }

  Send {Space}
Return
;
;----------
;
$Esc::
  ; if Escape was pressed
  if WinActive("ahk_class TSTDTREEDLG") AND ( hist_sec <> space )
    GoSub clear_mem

  Send {Esc}
Return
;
;----------
;
write_history:
  if ( dir_win_id <> space )
    Return

  ; get dir create dialog id
  WinGet, dir_win_id, ID, A
  ; and wait until dialog is closed
  SetTimer, wait_dlg_close, 100

  ; Get new dir name...
  ControlGetText, cur_text, TEdit1
  ; if empty then exit

  if ( cur_text = space )
    Return ; ...and exit if it is blank

  ; if history is not loaded, load it
  if ( item_cnt = space )
    GoSub read_hist_file

  ; if history contains new dir name, then return
  Loop, %item_cnt%
    if ( cur_text = hist_array%A_Index% )
      Return

  ; write history depth value into the ini file
  IniWrite, %hist_depth%, %A_ScriptDir%\history.ini, %hist_sec%, depth
  ; write first dir history entry into the ini file
  IniWrite, %cur_text%, %A_ScriptDir%\history.ini, %hist_sec%, item1

  ; if history count is equal to history depth,
  ; last history entry will be deleted and each
  ; entry will be replaced by the previous one
  if ( item_cnt >= hist_depth )
    cnt := hist_depth - 1
  else
    cnt := item_cnt

  pos = 2

  ; save the rest dir history back to the ini-file
  Loop, %cnt%
  {
    IniWrite, % hist_array%A_Index%, %A_ScriptDir%\history.ini, %hist_sec%, item%pos%
    pos++
  }

Return
;
;----------
;
take_hist( inc )
{
  ; ensure all variables below are global
  global

  ; if history was not loaded then try to load
  if ( item_cnt = space )
  {
    GoSub read_hist_file

    if ( item_cnt < 1 )
      Return
  }

  ; calculate history item position
  item_pos += inc

  if ( item_pos > item_cnt )
    item_pos = 1
  else if ( item_pos < 1 )
    item_pos := item_cnt

  ; place history item to the edit
  ControlSetText, TEdit1, % hist_array%item_pos%
  ; set caret position to the end
  ControlSend, TEdit1, {End}

  Return
}
;
;----------
;
tc_show_history_list:
  ; if Alt+Down pressed in the dir creation dialog
  ; build history
  if ( item_cnt = space )
    GoSub read_hist_file

  ; if history is not empty, build history listbox
  if item_cnt > 0
  {
    ; create GUI listbox... (menu using causes some problems)
    WinGetPos, w_x, w_y, , ,ahk_Class TSTDTREEDLG
    ControlGetPos, c_x, c_y, c_w, c_h, TEdit1
    c_x += w_x
    c_y += w_y + c_h
    c_w -= 2
    Gui, Margin, 0, 0
    Gui, -Caption +Border +Delimiter`n
    Gui, Add, ListBox, +AltSubmit -E0x200 w%c_w% h100 vDirChoice gHistBox, %hist_array1%
    pos = 2
    Loop, % item_cnt - 1
    {
      GuiControl, ,ListBox1, % hist_array%pos%
      pos++
    }
    ; ...and show it under the edit field
    Gui, Show, x%c_x% y%c_y%
    ; wait for GUI to become inactive
    SetTimer, WaitGuiInactive, 100
    GuiControl, Choose, ListBox1, 1
  }
Return
;
;----------
;
read_hist_file:
  ; read history depth value from the ini-file
  IniRead, hist_depth, %A_ScriptDir%\history.ini, %hist_sec%, depth, 20
  if (hist_depth = space)
    hist_depth = 20

  ; history items counter
  item_cnt = 0
  ; trying to build history array
  Loop, %hist_depth%
  {
    IniRead, item, %A_ScriptDir%\history.ini, %hist_sec%, item%A_Index%
    ; skip empty entries
    if ( item <> "ERROR" AND item <> space)
    {
      item_cnt++
      hist_array%item_cnt% = %item%
    }
  }

  if ( item_cnt > 0 )
    ; current history item position
    item_pos = 0

Return
;
;----------
;
HistBox:
  ; if not Left Mouse button pressed then return
  hk := A_ThisHotKey
  if (hk <> "~LButton")
    Return
;
;----------
;
GuiSubmit:
  ; get current selection into variable
  GuiControlGet, item_pos, ,ListBox1
  GuiControl, -AltSubmit, ListBox1
  Gui, Submit

  ; wait for dir creation dialog to become active
  WinWaitActive, ahk_class TSTDTREEDLG
  ; Set edit text to the history entry
  ControlSetText, TEdit1, %DirChoice%
  ; Set caret position to the end of text
  ControlSend, TEdit1, {End}
  ; free memory
  DirChoice =
GuiEscape:
GuiClose:
  Gui, Destroy
Return
;
;----------
;
WaitGuiInactive:
  ; if GUI is inactive then destroy it
  ifWinNotActive, ahk_class AutoHotkeyGUI
  {
    Gui, Destroy
    ; turn off timer
    SetTimer, WaitGuiInactive, Off
  }
Return
;
;----------
;
wait_dlg_close:
  ; if dir creation dialog doesn't exist
  ifWinNotExist, ahk_id %dir_win_id%
  {
    ; turn off timer
    SetTimer, wait_dlg_close, Off
    ; clear windows id variable
    dir_win_id =

    Gosub clear_mem
  }
Return
;
;----------
;
clear_mem:
  ; clear history array (free memory)
  Loop, %item_cnt%
    hist_array%A_Index% =

  ; clear other vars
  item_cnt =
  item_pos =
  hist_sec =
Return


Warning: Redefining the Enter and Space keys results in that they will not work as a terminator for hotstrings, see here: http://www.autohotkey.com/forum/viewtopic.php?t=4232



Back to AutoHotkey