Lazarus: Open File or Document platform-independent
Tip by Stefan Trost | Last update on 2023-01-24 | Created on 2013-11-03
If you want to open an arbitrary file or document with the default application for that file set in the operating system, you can simply use the practical function OpenDocument() in Lazarus.
Here is a small code example for the use of this function:
uses ..., LCLIntf; procedure ... begin OpenDocument('test.docx'); end;
In order to be able to use OpenDocument() in our program, we only have to add the Unit LCLIntf to our uses section and we have to pass the name and the path of the file to be openend to the function.
In the example code, we are using OpenDocument() to open the file "test.docx" (we can leave out the complete path to the file if the file is stored in the program directory). On Windows, the result could be that Microsoft Word or WordPad is opened with the document, in Linux that one of the programs LibreOffice Writer or OpenOffice Writer is started and the document opened in it and under macOS or Mac OS X the program installed there for Word Documents.
Which Program is used to open the Files?
It is always the default application used, that is linked with the appropriate file respectively file format. Which file or file format is opened does not matter, unless the respective file extension is not linked to any program on the operating system or individual system used.
Internally, on Windows systems OpenDocument() uses the Registry to determine the default application for the file extension of the file to be opened. Under macOS, the "open" command line command is used.
Open Folders
Also in the case we want to open a folder in the responsible default application, we can use the OpenDocument() function. Instead of a file path, we can simply hand over the path to a folder for that. On Windows, as a result, the folder would typically be opened and shown in the Windows Explorer.
Open Websites
Like the function OpenDocument() is opening any file, document or folder with its default application, you can use the function OpenURL() in Lazarus to open an arbitrary web page in the default browser. Incidentally, the same happens if we hand over an URL to OpenDocument().
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile
Related Topics
MIME Types of Microsoft Office File Formats
Info | 0 Comments
VirtualBox: Change Date and Time
Tutorial | 10 Comments
Windows Batch Script: Computer Shutdown
Tutorial | 2 Comments
Lazarus: Detect Operating System (Compiler Switch)
Tutorial | 0 Comments
Delphi/Lazarus: How to set up a Link to a Website in an Application
Tutorial | 0 Comments
How to resize Image before Upload in Browser
Tutorial | 13 Comments
Lazarus: Determine Default-Font or System-Font of Form, Label, Edit and Button
Question | 3 Answers
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.