User Tools

Site Tools


Display Document

Summary

Below is an example demonstrating how you can dynamically locate a document in a mapped content directory and then prompt the Operating System to perform its default handling of the file, via script.

API Areas of Interest

Example

Display_Document_Dynamic.dsa
// Define an anonymous function;
// serves as our main loop,
// limits the scope of variables
(function( sRelativePath ){
 
	/*********************************************************************/
	// String : A function for retrieving a translation if one exists
	function text( sText )
	{
		// If the version of the application supports qsTr()
		if( typeof( qsTr ) != "undefined" ){
			// Return the translated (if any) text
			return qsTr( sText );
		}
 
		// Return the original text
		return sText;
	};
 
	/*********************************************************************/
	// Get the content manager
	var oContentMgr = App.getContentMgr();
	// If we do not have a content manager
	if( !oContentMgr ){
		// We are done...
		return;
	}
 
	// Declareworking variables
	var sPreferredBasePath, sAbsolutePath;
 
	// Get the path of this script
	var sScriptPath = getScriptFileName();
 
	// If the version is 4.8.1.51 or newer
	if( App.version64 >= 0x0004000800010033 ){
		// Define the directory type to look in
		var nDirType = DzContentMgr.AllDirs;
 
		// If the version is 4.9.0.51 or newer
		if( App.version64 >= 0x0004000900000033 ){
			// Also look in cloud directories
			nDirType = DzContentMgr.AllDirsAndCloud;
		}
 
		//Define the preferred [mapped] base path; use the path of the current script
		sPreferredBasePath = oContentMgr.getMappedPath( nDirType, sScriptPath, false );
 
		// Get the absolute path of the file to be shown/opened
		sAbsolutePath = oContentMgr.getAbsolutePath( nDirType, sRelativePath, sPreferredBasePath );
	// If the version is older than 4.8.1.51
	} else {
		// Define the preferred [mapped] base path; use the path of the current script
		sPreferredBasePath = oContentMgr.getMappedPath( sScriptPath, true, false );
 
		// Get the absolute path of the file to be shown/opened
		sAbsolutePath = oContentMgr.getAbsolutePath( sRelativePath, true, sPreferredBasePath );
	}
 
	// If the file was found
	if( !sAbsolutePath.isEmpty() ){
		// Prompt the operating system to perform its default handling of the given file type
		App.showURL( String("file:///%1").arg( sAbsolutePath ) );
	// If the file was not found
	} else {
		// Inform the user
		MessageBox.information(
			text( "'%1' could not be found in a mapped content directory. " +
				"Check the installation and try again.").arg( sRelativePath ),
			text( "File Not Found" ), text( "&OK" ) );
	}
 
// Finalize the function and invoke
})( "Relative/path/to/a/file.txt" );