Difference between revisions of "OpenDocument"

From Free Pascal wiki
Jump to navigationJump to search
m (Alextp moved page opendocument to OpenDocument: casing must be Pascal-casing)
 
(9 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{LanguageBar}}
 +
 
==Definition==
 
==Definition==
 +
 
Unit: Lazarus '''lclintf'''
 
Unit: Lazarus '''lclintf'''
  
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
function OpenDocument(APath: String): Boolean;
 
function OpenDocument(APath: String): Boolean;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Official documentation: [http://lazarus-ccr.sourceforge.net/docs/lcl/lclintf/opendocument.html]
+
Official documentation: [http://lazarus-ccr.sourceforge.net/docs/lcl/lclintf/opendocument.html OpenDocument]
  
 
==Description==
 
==Description==
'''opendocument''' opens a document/file with the default viewer/editor registered with the operating system for that file/file extension. E.g. on Windows, the code will use the registry to look up the file association for the extension.
 
  
{{Warning|On Windows at least in stable version (currently 1.0.6), trying to open documents with spaces in it will not work. This has been fixed in trunk (r36934) and it will probably be merged to 1.0.8. (See {{MantisLink|21659}} in the bugtracker)}}
+
<syntaxhighlight lang=pascal inline>OpenDocument</syntaxhighlight> opens a document/file with the default viewer/editor registered with the operating system for that file/file extension.
 +
 
 +
* On macOS, the code calls the [[macOS Open Sesame|open]] command line utility for files '''and''' directories. This will open the default application for the file or directory as determined by LaunchServices.
 +
* On Windows, the code will use the registry to look up the file association for the extension. As with macOS, it will also open directories.
 +
 
 +
On both macOS and Windows, <syntaxhighlight lang=pascal inline>OpenDocument</syntaxhighlight> will also open a URL using the default web browser.
  
 
==Example==
 
==Example==
<syntaxhighlight>
+
<syntaxhighlight lang="pascal">
 
uses  
 
uses  
...
+
  LCLIntf;
lclintf
+
begin
...
+
  OpenDocument('readme.pdf');
OpenDocument('readme.pdf');
 
 
</syntaxhighlight>
 
</syntaxhighlight>
[[category:Lazarus]]
 
[[category:lclintf]]
 
== Win NT "File names with spaces" workaround==
 
The following is a simple function you can experiment with to do a similar job for file names with spaces.  It is derived directly from OpenURL in <your lazarus install>\lcl\include\sysenvapis_win.inc, and has variations for wince and win9x removed - if you need those, see the original file.  '''Experiment with''' is emphasised - it has only been tested for my particular use-case on windows 7.  It does not seem to need the filename quoted.
 
  
<syntaxhighlight>
+
== See also ==
uses windows;
 
  
function OpenFileDoc(Doc: String): Boolean;
+
* [[macOS_Open_Sesame|macOS Open command]]
var
+
* [[OpenURL]]
  ws: WideString;
 
begin
 
  Result := False;
 
  if Doc = '' then Exit;
 
  ws := UTF8Decode(doc);
 
  Result := ShellExecuteW(0, 'open', PWideChar(ws), nil, nil, SW_SHOWNORMAL) > 32;
 
end; 
 
</syntaxhighlight>
 
  
 +
[[category:Lazarus]]
 +
[[category:lclintf]]
 
[[Category:Inter-process communication]]
 
[[Category:Inter-process communication]]

Latest revision as of 20:37, 28 November 2021

English (en) 中文(中国大陆)‎ (zh_CN)

Definition

Unit: Lazarus lclintf

function OpenDocument(APath: String): Boolean;

Official documentation: OpenDocument

Description

OpenDocument opens a document/file with the default viewer/editor registered with the operating system for that file/file extension.

  • On macOS, the code calls the open command line utility for files and directories. This will open the default application for the file or directory as determined by LaunchServices.
  • On Windows, the code will use the registry to look up the file association for the extension. As with macOS, it will also open directories.

On both macOS and Windows, OpenDocument will also open a URL using the default web browser.

Example

uses 
  LCLIntf;
begin
  OpenDocument('readme.pdf');

See also