Link file extension ".my_ext" to "ext_handler.ahk %filepath%" Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
stepanVL
Posts: 11
Joined: 27 Jun 2017, 08:56

Link file extension ".my_ext" to "ext_handler.ahk %filepath%"

27 Jun 2017, 09:32

Goal: add this Windows behavior (pseudo code):

Code: Select all

if( (doubleclick on %file%) and  (%file%.ftype = ".my_ext"))
	run  "C:\MyScripts\my_extension_handler.ahk %file%"
 
I have a file "C:\1\myfile.my_ext" containing text "1234test"
I wrote a script "C:\MyScripts\my_extension_handler.ahk" that displays content of the file given as first parameter
Script works as expected when I run it in a terminal using

Code: Select all

C:\MyScripts\my_extension_handler.ahk C:\1\myfile.my_ext"


I associated this script with ".my_ext" : properties -> Open with -> C:\extension_handler.ahk
When I doubleclick "myfile.my_ext" I get "This app can't run on your PC error message".
How to associate double click with Windows launching this script and passing full file path as a parameter?

---------------------------------
Update:
---------------------------------
I've ran this code in cmd as an admin:

Code: Select all

assoc .my_ext=myownext
ftype myownext="C:\MyScripts\my_extension_handler.ahk" "%1"
regsvr32 /i shell32.dll
Association registered successfully, but I still get the same error..

Code: Select all

Loop, %0%  ; For each parameter:
{
	
	if (! param){
		global param := []	
		global index := 0 	
		}	 
	index := index + 1
	param[index] :=  %A_Index%		
}
strElements := ""
for index, element in param ; Recommended approach in most cases.
{
    strElements := strElements . "Element number " . index . " is " . element . "`r`n"
}
MsgBox % strElements

str := ""
source := param[1] 
MsgBox % "Source is " . source
Loop, read,   %source% ; should read selectedfile
    str  := str  . "`r`n" .  A_LoopReadLine

MsgBox % str

Last edited by stepanVL on 27 Jun 2017, 11:27, edited 4 times in total.
Guest

Re: Associate an AHK script with my file extension

27 Jun 2017, 09:38

Replace your sourcefile line with sourcefile = %1%
Make sure you're association is

C:\MyScripts\my_extension_handler.ahk "%1"

%1 is the file you double click, press enter on

%1% is a command line parameter passed on to the script, see "Script Parameter" here https://autohotkey.com/docs/Scripts.htm#cmd
stepanVL
Posts: 11
Joined: 27 Jun 2017, 08:56

Re: Associate an AHK script with my file extension

27 Jun 2017, 10:25

evilC wrote:AHK does not use \ for escape characters, it uses `, so you do not need any escaping at all for backslashes
I changed my post. Double slash was a blunder, but my main problem is windows file association.
robertcollier4
Posts: 33
Joined: 09 Apr 2016, 22:14

Re: Associate an AHK script with my file extension  Topic is solved

27 Jun 2017, 10:48

Use the following reg script (save it to a filename with extension .reg and then run it):

Code: Select all

Windows Registry Editor Version 5.00

; Remove previously wrongly made associations
[-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.my_ext]
[-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\my_ext]
[-HKEY_CURRENT_USER\Software\Classes\.my_ext]
[-HKEY_CURRENT_USER\Software\Classes\my_ext]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.my_ext]
@="my_ext"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\my_ext\shell\open\command]
@="\"C:\\Program Files\\AutoHotkey\\AutoHotkeyU32.exe\" \"C:\\MyScripts\\my_extension_handler.ahk\" \"%1\" %*"
stepanVL
Posts: 11
Joined: 27 Jun 2017, 08:56

Re: Link file extension ".my_ext" to "ext_handler.ahk %filepath%"

27 Jun 2017, 12:50

This reg file did the trick. Thank you for commenting it.

I will post working code in case someone else wants to link extension to ahk. I use this one to initialize ahk with settings storred in a ".1tg" file.

update.reg

Code: Select all

; Remove previously wrongly made associations 
[-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.1tg]
[-HKEY_LOCAL_MACHINE\SOFTWARE\Classes\1tg]
[-HKEY_CURRENT_USER\Software\Classes\.1tg]
[-HKEY_CURRENT_USER\Software\Classes\1tg]
; comment out above lines if you haven't mess up registry yet

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.1tg]
@="1tg"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\1tg\shell\open\command]
@="\"C:\\Program Files\\AutoHotkey\\AutoHotkeyU32.exe\" \"C:\\AHK\\tags\\ThisScriptOpensTG.ahk\" \"%1\" %*"
C:\AHK\tags\ThisScriptOpensTG.ahk

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

MsgBox % "main script"

Loop, %0%  ; For each parameter:
{
	
	if (! param){
		global param := []	
		global index := 0 	
		}	 
	index := index + 1
	param[index] :=  %A_Index%		
}
strElements := ""
for index, element in param ; Recommended approach in most cases.
{
    strElements := strElements . "Element number " . index . " is " . element . "`r`n"
}
MsgBox % strElements

str := ""
source := param[1]
MsgBox % "Source is " . source
Loop, read,   %source% ; should read selectedfile
    str  := str  . "`r`n" .  A_LoopReadLine
MsgBox % str

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee and 388 guests