Opening file in program by key - how to check file no exist?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MIRKOSOFT
Posts: 334
Joined: 23 Sep 2014, 14:29

Opening file in program by key - how to check file no exist?

23 Dec 2017, 18:29

Hi!

On this forum helped me one member with creating part of script which after calling scans selected file(s) and when I press hotkey they're opened in program associated with hotkey.

Example of hotkey:

Code: Select all

vk7Fsc067::
this_path := Selected_Files()        ; calling selected file scan
Run, paint.exe "%this_path%"         ; opening file in MS Paint
return
But if is file not selected it asks me for create or program starts and after error message program ends.
I need to detect if is file not selected by return string something like "<nofile>" and avoid ask or program termination.

Here's code of scan routine (has two parts - one for Explorer and one for Desktop):

Code: Select all

Selected_Files()
{
  ; Handles Explorer
  IfWinActive, ahk_class CabinetWClass
  {
    for window in ComObjCreate("Shell.Application").Windows
      if window.HWND = WinExist("A")
        this_window := window
    
    ; If multiple Items selected
    if(this_window.Document.SelectedItems.Count > 1)
    {
      these_files := ""
      for item in this_window.Document.SelectedItems
        these_files .= item.Path . "`n"
      
      return these_files
    }
    else
      return this_window.Document.FocusedItem.Path
  }
  
  ; Handles Desktop
  if(WinActive("ahk_class Progman") || WinActive("ahk_class WorkerW"))
  {
    ControlGet, selectedFiles, List, Selected Col1, SysListView321, A
    
    ; If multiple Items selected
    if InStr(selectedFiles, "`n")
    {
      these_files := ""
      Loop, Parse, selectedFiles, `n, `r
        these_files .= A_Desktop . "\" . A_LoopField . "`n"
    
      return these_files
    }
    else
      return A_Desktop . "\" . selectedFiles
  }
  else
    return false
}
Can anybody help me with? I'm not good in AHK script yet.
Thank you for all.

Miro
Noesis
Posts: 301
Joined: 26 Apr 2014, 07:57

Re: Opening file in program by key - how to check file no exist?

24 Dec 2017, 02:28

Seems to me the code assumes files are selected, i.e you're not actually checking to see if anything was selected.

Since this_path is coming from selected files those actual files (if any) will be valid (i.e. it would be safe to assume the selected files do exist on the disk, so you don't need to check that part, but it's not safe to assume that a selection existed in the first place, so you do need to check that something was selected i.e. returned from the SelectedFiles() function).

The easy fix is something like this in the hotkey:

Code: Select all

vk7Fsc067::
this_path := Selected_Files()        ; calling selected file scan
if (this_path)
	Run, paint.exe "%this_path%"         ; opening file in MS Paint
else
	MsgBox, Nothing Selected
return
The other potential issue in this code is only part of your SelectedFiles() function checks if something was selected, the other section (when an explorer window) returns the FocusedItem.Path even if "this_window.Document.SelectedItems.Count" is 0, this may or may not be an issue depending on what it actually returns when there isn't something selected.
Another closely related potential issue in this section, is what is returned under those conditions, as you return the focused item in this section if one or no file was selected, and while focused items and selected items are often the same in the case of one selected item, they could be two different things when nothing is selected. So this area would need to be tested, as it may in fact work fine, I don't know, but if not just put in checks differentiating nothing selected and and 1 thing selected and return nothing if nothing was selected in that section.
MIRKOSOFT
Posts: 334
Joined: 23 Sep 2014, 14:29

Re: Opening file in program by key - how to check file no exist?

24 Dec 2017, 05:45

Thank you for help.
I'll test it after Christmas Eve and wish you pretty Christmas.

Miro
MIRKOSOFT
Posts: 334
Joined: 23 Sep 2014, 14:29

Re: Opening file in program by key - how to check file no exist?

24 Dec 2017, 09:01

Thank you very much again!
Tested and works!

Miro

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 343 guests