This shows you the differences between two versions of the page.
opening_20a_20file_20in_20a_20specified_20application [2018/03/31 13:19] 127.0.0.1 external edit |
opening_20a_20file_20in_20a_20specified_20application [2018/04/17 18:03] tbest3112 Added syntax highlighting |
||
---|---|---|---|
Line 2: | Line 2: | ||
//by J.G.Harston, February 2008//\\ \\ You can open a file in an application, equivalent to double-clicking on it in a filer window, by using ShellExecute, as follows:\\ | //by J.G.Harston, February 2008//\\ \\ You can open a file in an application, equivalent to double-clicking on it in a filer window, by using ShellExecute, as follows:\\ | ||
+ | <code bb4w> | ||
SYS "ShellExecute", @hwnd%, 0, file$, 0, 0, 1 | SYS "ShellExecute", @hwnd%, 0, file$, 0, 0, 1 | ||
+ | </code> | ||
This loads the file into the application that is associated with the file by reference to its file extension. For instance, a file with a ".doc" extension will be loaded into whatever application is associated with the ".doc" extension.\\ \\ There may be times when you want to load a file into a specific application regardless of extension. For instance, you may want to load a batch file into a text editor. The simplest solution is to do:\\ | This loads the file into the application that is associated with the file by reference to its file extension. For instance, a file with a ".doc" extension will be loaded into whatever application is associated with the ".doc" extension.\\ \\ There may be times when you want to load a file into a specific application regardless of extension. For instance, you may want to load a batch file into a text editor. The simplest solution is to do:\\ | ||
+ | <code bb4w> | ||
SYS "ShellExecute", @hwnd%, 0, "notepad", file$, 0, 1 | SYS "ShellExecute", @hwnd%, 0, "notepad", file$, 0, 1 | ||
+ | </code> | ||
or\\ | or\\ | ||
+ | <code bb4w> | ||
OSCLI "notepad "+file$ | OSCLI "notepad "+file$ | ||
+ | </code> | ||
There are several problems with this.\\ \\ Firstly, a minor one, you are depending on an application called 'notepad' existing on the computer the program is run on.\\ \\ Secondly, and much more importantly, you must never force a user to use **your** choice of application, you must use the **user's** choice. Many people regard notepad as a very bad text editor and use something else.\\ \\ The user's configured applications are named in the registry and referenced by the file extension they respond to. You can load a file to the user's text editor using the following code:\\ | There are several problems with this.\\ \\ Firstly, a minor one, you are depending on an application called 'notepad' existing on the computer the program is run on.\\ \\ Secondly, and much more importantly, you must never force a user to use **your** choice of application, you must use the **user's** choice. Many people regard notepad as a very bad text editor and use something else.\\ \\ The user's configured applications are named in the registry and referenced by the file extension they respond to. You can load a file to the user's text editor using the following code:\\ | ||
+ | <code bb4w> | ||
INPUT "File: "file$ | INPUT "File: "file$ | ||
root$=FNRegistry_RdStr(&80000000,".txt",""):REM HKEY_CLASSES_ROOT | root$=FNRegistry_RdStr(&80000000,".txt",""):REM HKEY_CLASSES_ROOT | ||
Line 18: | Line 25: | ||
SYS "ShellExecute", @hwnd%, 0, command$, file$, 0, 1 | SYS "ShellExecute", @hwnd%, 0, command$, file$, 0, 1 | ||
END | END | ||
+ | </code> | ||
Here we read the default application which opens text files (.txt) from the registry and use "ShellExecute" to open the requested file in the chosen application. Effectively, this bypasses the automatic file association and executes the file as though it had a ".txt" extension.\\ \\ The ".txt" extension in the code above can be changed to any other extension. For instance to open a HTML document or URL in the user's choice of web browser, and not force them to use Internet Explorer you would change the relevant line above to:\\ | Here we read the default application which opens text files (.txt) from the registry and use "ShellExecute" to open the requested file in the chosen application. Effectively, this bypasses the automatic file association and executes the file as though it had a ".txt" extension.\\ \\ The ".txt" extension in the code above can be changed to any other extension. For instance to open a HTML document or URL in the user's choice of web browser, and not force them to use Internet Explorer you would change the relevant line above to:\\ | ||
+ | <code bb4w> | ||
root$=FNRegistry_RdStr(&80000000,".htm",""):REM HKEY_CLASSES_ROOT | root$=FNRegistry_RdStr(&80000000,".htm",""):REM HKEY_CLASSES_ROOT | ||
+ | </code> | ||
The above code uses the following code from the [[http://mdfs.net/blib|Registry]] library and a routine called **FNExpandEnvironmentStrings**:\\ | The above code uses the following code from the [[http://mdfs.net/blib|Registry]] library and a routine called **FNExpandEnvironmentStrings**:\\ | ||
+ | <code bb4w> | ||
DEF FNRegistry_RdInt(hk%,Key$,Item$):LOCAL Want%:Want%=4 | DEF FNRegistry_RdInt(hk%,Key$,Item$):LOCAL Want%:Want%=4 | ||
DEF FNRegistry_RdBig(hk%,Key$,Item$):LOCAL Want%:Want%=4 | DEF FNRegistry_RdBig(hk%,Key$,Item$):LOCAL Want%:Want%=4 | ||
Line 43: | Line 54: | ||
SYS "ExpandEnvironmentStrings", S$, B%, 256 | SYS "ExpandEnvironmentStrings", S$, B%, 256 | ||
=$$B% | =$$B% | ||
+ | </code> |