opening a file type with specific program

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

opening a file type with specific program

Post by tatagi » 23 Mar 2023, 11:27

to put it simply, what I want to achieve is:

1. open the windows file explorer
2. locate the file with specific file type(e.g. jpg or png)
3. click on one or more files for selection
4. press hotkey (e.g. ctrl + shift + 0)
5. the selected file is opened using a program that is not the default for that file type

I do step 1 to 3 manually, and I want the autohotkey to do the rest.

thank you in advance and if my explanation doesn't suffice, please let me know.

User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: opening a file type with specific program

Post by mikeyww » 23 Mar 2023, 13:24

Code: Select all

#Requires AutoHotkey v1.1.33
app := A_WinDir "\System32\notepad.exe"

#If WinActive("ahk_class CabinetWClass")
^+0::
For each, item in getSelected() {
 Run % app " """ item """"
 Sleep 200
}
Return
#If

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows {
   Try window.hwnd
   Catch
    Return
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
  }
 Return selection
}

tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

Re: opening a file type with specific program

Post by tatagi » 23 Mar 2023, 18:08

mikeyww wrote:
23 Mar 2023, 13:24
many thanks. that works smoothly for the most part but can I get this triggered only when specific file type(mostly images) are selected? the program I open these files in supports only a few of them so there's time I highlight the wrong files by mistake and it gives obvious error pop up.

User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: opening a file type with specific program

Post by mikeyww » 23 Mar 2023, 19:21

Code: Select all

#Requires AutoHotkey v1.1.33
app   := "d:\utils\irfanView64\i_view64.exe"
image := "acr|ai|aif|ani|arw|asf|au|avi|avif|awd|b3d|bmp|cam|cgm|cin|clp|cpt|cr2|cr2|cr3|cr3|crw|crw|cur|"
       . "dcm|dcr|dcx|dds|dib|djvu|dll|dng|dpx|dwg|dxf|dxf|ecw|emf|eps|erf|exr|fits|flif|flv|fpx|"
       . "g3|gif|hdp|heic|hpgl|icl|ico|ics|iff|ima|img|iw44|j2k|jls|jng|jp2|jpc|jpeg|jpg|jpm|jxl|jxr|"
       . "kdc|lbm|m2t|m2ts|m2v|m4v|macpict|med|mid|mkv|mng|mos|mov|mp3|mp4|mpeg|mpg|mrsid|mrw|mts|"
       . "nef|nrw|ogg|orf|pbm|pcd|pcx|pdn|pef|pgm|png|ppm|ps|psd|psp|pvr|qoi|qtif|"
       . "ra|raf|ras|raw|rgb|rle|rw2|rwl|sff|sfw|sgi|sid|sif|snd|srf|srw|sun|svg|swf|"
       . "tga|tif|tiff|ts|ttf|wad|wal|wav|wbc|wbmp|wbz|wdp|webm|webp|wma|wmf|wmv|wsq|x3f|xbm|xcf|xpm|yuv"
       
#If WinActive("ahk_class CabinetWClass")
^+0::
For each, item in getSelected() {
 SplitPath item,,, ext
 If InStr("|" image "|", "|" ext "|") {
  Run % app " """ item """"
  Sleep 200
 }
}
Return
#If

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows {
   Try window.hwnd
   Catch
    Return
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
  }
 Return selection
}

tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

Re: opening a file type with specific program

Post by tatagi » 25 Mar 2023, 04:33

mikeyww wrote:
thank you very much. does this script involve any looping operation even when not in use?

User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: opening a file type with specific program

Post by mikeyww » 25 Mar 2023, 07:34

No. The loops are executed only when File Explorer is active and the hotkey is triggered.

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: opening a file type with specific program

Post by ahk7 » 25 Mar 2023, 08:15

Perhaps of interest, https://github.com/hi5/F4MiniMenu, focused on Total Commander, but should work with Explorer as well - you can also show a menu so you can select a specific application for selected files, so you can install multiple image applications and then open them in a specific one if need be (or a variety of text/code editors etc)

GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: opening a file type with specific program

Post by GEV » 25 Mar 2023, 09:40

mikeyww wrote:
23 Mar 2023, 19:21

Code: Select all

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows {
   Try window.hwnd
   Catch
    Return
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
  }
 Return selection
}
This can get the path of selected files and folders both in Explorer and on the Desktop:

Code: Select all

getSelected(){
	; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=60403&p=255273#p255256
	WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
	if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
		Return
	shellWindows := ComObjCreate("Shell.Application").Windows
	if (winClass ~= "Progman|WorkerW")
		shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
	else {
	for window in shellWindows
		try  if (hWnd = window.HWND) && (shellFolderView := window.Document)
			break
	}
	for item in shellFolderView.SelectedItems
		result .= (result = "" ? "" : "`n") . item.Path
	if !result
		result := shellFolderView.Folder.Self.Path
	Return result
}
Last edited by GEV on 06 Sep 2023, 08:48, edited 3 times in total.

User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: opening a file type with specific program

Post by mikeyww » 25 Mar 2023, 09:57

Thanks for that update, GEV!

tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

Re: opening a file type with specific program

Post by tatagi » 26 Mar 2023, 01:25

mikeyww wrote:
25 Mar 2023, 07:34
No. The loops are executed only when File Explorer is active and the hotkey is triggered.
Sounds great. I am trying to add your script into mine, but I have some problem that should be dealt with.

I have zero idea what to change at all, please help.

Code: Select all

#NoEnv
#Warn
;SendMode Input
SetWorkingDir %A_ScriptDir%
#SingleInstance force
SetStoreCapsLockMode, Off 
SetCapsLockState, Off

area := {x: 0, y: 0, w: 2000, h: 1000}
winTitle := "ahk_class Notepad"
return

#InputLevel 1
CapsLock::vkFF	;remaps CapsLock into unassigned vkFF
#InputLevel 0   ;instead of: "CapsLock & ..."  use: "vkFF & ..."

vkFF & 1::      ;turns Capslock on and off
SetCapsLockState % !GetKeyState("CapsLock", "T") 
return

vkFF & 2::ExitApp

OnActivate(hMatch) { ; https://docs.microsoft.com/en-us/windows/win32/menurc/using-cursors#confining-a-cursor
	local
	global area
	static rcOldClip := ""
	if (hMatch) {
		VarSetCapacity(rcOldClip, 16, 0) ; previous area for ClipCursor
		DllCall("User32.dll\GetClipCursor", "Ptr", &rcOldClip) ; Record the area in which the cursor can move.
		VarSetCapacity(rcClip, 16, 0) ; new area for ClipCursor
		; for each, value in area {
			; NumPut(value, &rcClip, (a_index -1) * 4, "UInt")
		; }
		NumPut(area.x, &rcClip, 0, "UInt")
		NumPut(area.y, &rcClip, 4, "UInt")
		NumPut(area.w, &rcClip, 8, "UInt")
		NumPut(area.h, &rcClip, 12, "UInt")
		DllCall("User32.dll\ClipCursor", "Ptr", &rcClip) ; Confine the cursor to the application's window.
	} else {
		(rcOldClip && DllCall("User32.dll\ClipCursor", "Ptr", &rcOldClip), rcOldClip:="") ; Restore the cursor to its previous area.
	}
}
; =====================================
shellMessage(wParam, lParam) { ; based on https://autohotkey.com/board/topic/80644-how-to-hook-on-to-shell-to-receive-its-messages/
	local
	global winTitle
	static f := (Func("OnActivate")
			, DllCall("RegisterShellHookWindow", "Ptr", A_ScriptHwnd)
			, OnMessage(DllCall("RegisterWindowMessage", "Str", "SHELLHOOK"), "shellMessage")
			, OnExit("shellMessage"))
	static iRcOldClip := ""
	static _ := (VarSetCapacity(iRcOldClip, 16, 0) ; the very initial area for ClipCursor
			, DllCall("User32.dll\GetClipCursor", "Ptr", &iRcOldClip)) ; Record the initial area in which the cursor can move.
	Critical
	if (wParam = A_ExitReason) {
		return 0, DllCall("DeregisterShellHookWindow", "UInt", A_ScriptHwnd), DllCall("User32.dll\ClipCursor", "Ptr", &iRcOldClip) ; Restore the cursor to its very initial area.
	}
	if ((wParam = 32772) or (wParam = 4)) { ; HSHELL_WINDOWACTIVATED := 4, HSHELL_RUDEAPPACTIVATED := 32772
		fn := f.bind(WinActive(winTitle))
		SetTimer % fn, -1
	}
}


;your script from here
app   := "c:\myfolder\irfanView64\i_view64.exe"
image := "jpg|png|gif|bmp|tiff|webp"
       
#If WinActive("ahk_class CabinetWClass")
^+0::
For each, item in getSelected() {
 SplitPath item,,, ext
 If InStr("|" image "|", "|" ext "|") {
  Run % app " """ item """"
  Sleep 200
 }
}
Return
#If

getSelected() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 by teadrinker
 hwnd := WinExist("A"), selection := []
 WinGetClass, class
 If (class ~= "(Cabinet|Explore)WClass")
  For window in ComObjCreate("Shell.Application").Windows {
   Try window.hwnd
   Catch
    Return
   If (window.hwnd = hwnd)
    For item in window.document.SelectedItems
     selection.Push(item.Path)
  }
 Return selection
}

I get two error dialogs: https://imgur.com/a/3fEqq0b

Please get me in the right direction.

tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

Re: opening a file type with specific program

Post by tatagi » 26 Mar 2023, 01:29

GEV wrote:
25 Mar 2023, 09:40
thank you! I will for sure check that out and if you know how to merge two scripts into one, please help.
viewtopic.php?f=76&t=115352#p514389

tatagi
Posts: 181
Joined: 23 Aug 2018, 11:17

Re: opening a file type with specific program

Post by tatagi » 26 Mar 2023, 01:31

ahk7 wrote:
25 Mar 2023, 08:15
Perhaps of interest, https://github.com/hi5/F4MiniMenu, focused on Total Commander, but should work with Explorer as well - you can also show a menu so you can select a specific application for selected files, so you can install multiple image applications and then open them in a specific one if need be (or a variety of text/code editors etc)
oh I might be able to find some use for that program but for now I prefer to minimize the number of programs used in windows, Thank you for the recommendations though :)

ahk7
Posts: 575
Joined: 06 Nov 2013, 16:35

Re: opening a file type with specific program

Post by ahk7 » 26 Mar 2023, 03:12

@tatagi just to be clear, it is written in AutoHotkey ;)

User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: opening a file type with specific program

Post by mikeyww » 26 Mar 2023, 05:19

Unfortunately, you have placed a few variable definitions (app and image) immediately after a Return command, so these statements will never execute. This means two things. First, you should learn about the auto-execute section. Second, you should move those lines to the auto-execute section.

Post Reply

Return to “Ask for Help (v1)”