AutoHotkey Community

It is currently May 27th, 2012, 10:48 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2
Author Message
 Post subject:
PostPosted: June 15th, 2011, 2:53 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
jballi, you still have not a topic on scripts and functions forum for this?! :shock: But hey, I made a link on top of this topic, so people should find yours more easily. :) Its definitiv the better solution.

zzzooo10: Maybe jballi could add this into his function too? I am willing to test a newer version. But later... not now...

_________________
{1:"ahkstdlib", 2:"my libs", 3:"my apps", 4:"my license"}
--> Don't feed the troll! <--


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2011, 4:04 am 
Offline

Joined: October 1st, 2005, 9:55 pm
Posts: 775
Location: Texas, USA
Tuncay wrote:
jballi, you still have not a topic on scripts and functions forum for this?! :shock: But hey, I made a link on top of this topic, so people should find yours more easily. :) Its definitiv the better solution.

Sorry to bump this old post but I just knew that there had to be a better solution out there than my makeshift function and somehow I stumbled on to it. I just wish that someone had pointed this out to me sooner.

TheGood wrote a function to find the associated program to any file extension using the built-in AssocQueryStringW function. The original solution can be found here:

My version of the function (I named it AssociatedProgram) should work on AHK and AHK_L.

Code:
;-- Function: AssociatedProgram
;-- Description: Returns the full path of the program (if any) associated to
;   a file extension (p_FileExt).
;-- Original author: TheGood
;       http://www.autohotkey.com/forum/viewtopic.php?p=363558#363558
;-- Programming note: AssocQueryStringA is never called because it returns
;   invalid results on Windows XP.
AssociatedProgram(p_FileExt)
    {
    Static ASSOCSTR_EXECUTABLE:=2

    ;-- Workaround for AutoHotkey Basic
    PtrType:=A_PtrSize ? "Ptr":"UInt"

    ;-- File extension
    if SubStr(p_FileExt,1,1)<>"."
        p_FileExt:="." . p_FileExt  ;-- Prepend dot

    ;-- If needed, convert file extension to Unicode
    l_FileExtW:=p_FileExt
    if not A_IsUnicode
        {
        nSize:=StrLen(p_FileExt)+1          ;-- Size in chars including terminating null
        VarSetCapacity(l_FileExtW,nSize*2)  ;-- Size in bytes
        DllCall("MultiByteToWideChar","UInt",0,"UInt",0,PtrType,&p_FileExt,"Int",-1,PtrType,&l_FileExtW,"Int",nSize)
        }

    ;-- Get the full path to the program
    VarSetCapacity(l_EXENameW,65536,0)      ;-- Size allows for 32K characters
    DllCall("shlwapi.dll\AssocQueryStringW"
        ,"UInt",0                           ;-- ASSOCF flags
        ,"UInt",ASSOCSTR_EXECUTABLE         ;-- ASSOCSTR flags
        ,PtrType,&l_FileExtW                ;-- pszAssoc (file extension used)
        ,PtrType,0                          ;-- pszExtra (not used)
        ,PtrType,&l_EXENameW                ;-- pszOut (output string)
        ,PtrType . "*",65536)               ;-- pcchOut (len of the output string)

    ;-- If needed, convert result back to ANSI
    if A_IsUnicode
        Return l_EXENameW
     else
        {
        nSize:=DllCall("WideCharToMultiByte","UInt",0,"UInt",0,PtrType,&l_EXENameW,"Int",-1,PtrType,0,"Int",0,PtrType,0,PtrType,0)
            ;-- Returns the number of bytes including the terminating null

        VarSetCapacity(l_EXEName,nSize)
        DllCall("WideCharToMultiByte","UInt",0,"UInt",0,PtrType,&l_EXENameW,"Int",-1,PtrType,&l_EXEName,"Int",nSize,PtrType,0,PtrType,0)
        Return l_EXEName
        }
    }


Example of use:
Code:
#NoEnv
#SingleInstance Force

ExtList=
   (ltrim join|
    7z
    ahk
    avi
    bmp
    cab
    chm
    css
    csv
    doc
    fon
    gif
    htm
    ini
    jar
    jpg
    js
    log
    m3u
    m4a
    mid
    mov
    mp3
    mp4
    mpa
    mpg
    msg
    pdf
    pls
    png
    pps
    ppt
    ra
    rar
    rtf
    tif
    torrent
    txt
    url
    vbs
    wav
    wma
    wmv
    wsf
    xls
    xml
    zip
   )


Loop Parse,ExtList,|
    RPList.=A_LoopField . "`t" . AssociatedProgram(A_LoopField) . "`n"

MsgBox %RPList%
return

#include AssociatedProgram.ahk

To find the default web browser, call the function using the "htm", "html", or whatever file extension you think is best.

All credit and thanks to TheGood and the others that contributed to this solution. Read the entire discussion thread here for additional information on this topic.

Edit 20111216_2243: This function is not 64-bit ready.
Edit 20111217_0057: OK, the function should be 64-bit ready but it has not been tested on a 64-bit build. Let me know if you have a problem.


Last edited by jballi on December 17th, 2011, 8:05 am, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2011, 5:00 am 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
You should use Uptr instead of UInt for pointers. If AHK Basic support is your goal, try something like
Code:
If !A_PtrSize
   UPtr := "UInt"
and then use unquoted UPtr.

_________________
Autofire, AutoClick, Toggle, SpamWindow Control Tools
Recommended: AutoHotkey_L


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2011, 5:58 am 
Offline

Joined: October 1st, 2005, 9:55 pm
Posts: 775
Location: Texas, USA
nimda wrote:
You should use Uptr instead of UInt for pointers.

Good catch! As you have figured out, I do not write for the 64-bit version of AHK_L. The reason: I'm still using AHK Basic and (most importantly), I don't have a 64-bit machine to test it on. I've updated my post to indicate the discrepancy. When I get a chance, I'll rewrite the function to use the Ptr types.

Edit: OK, I updated the post with a version that should be 64-bit ready but it has not been tested on a 64-bit build.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2011, 11:04 am 
I click a web page link. My currently assigned 'default browser' renders the page. 8) :D


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 17th, 2011, 11:59 pm 
Offline

Joined: December 9th, 2011, 2:03 pm
Posts: 14
yes but I believe the point of this is to give this information to a script without the human needing to get involved


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 36 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group