Script works, but not the CMS reference to the script. (Solved)
lukon100
Posts: 845
This script works to open the “Instructions” text file:
var sPath = MainWindow.getPaneMgr().findPane("DzContentLibraryPane").getSelectedContainer().getFullPath();
App.showURL("file:///" + sPath + "/Instructions.txt");
But it fails when I copy it in CMS to a custom categrory, wherein the copy is supposed to be a mere reference.
I want to know how to fix this, such that even the reference in the custom CMS category will open the text file when clicked.
I suspect I need a different function for building the path. But I have no idea what that function might be.
Post edited by lukon100 on

Comments
First, that really needs some error checking (and it would fail if installed through Connect).
A category is not a folder, it is an organised list of links to files in folders.
Thanks, Richard. I will now investigate how to perform this "error checking". I preume it is pretty easy, that there's some debugging module within Daz Studio that does it or something.
If I never offer my product (of which that script is a part) through the Daz store, is there any danger of someone installing my product via Daz Connect?
Daz Connect is daz store only, so no.
By error checking I meant have the script check itself - the first line has five things chained together with no checking that each step has returned a valid response before going on to the next step. A more robust approach can be found in http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/samples/general_ui/display_document_dynamic/start - but remember to obey the CC 3.0 By Attribution licenses terms if you do adapt it.
Thanks for directing me to that script, Richard. I'd like to use that script.
Unfortunately, I don't know enough about scripting to understand how to use it. I need to know:
1. Whether the script requires me to put the text file it opens in a specific folder.
2. What strings in the script to replace with the name of my text file that I want it to open, and how much of the path to that file I need to include with that name.
3. The name of the script's author for the purpose of my attributing them.
**********************************************************************/
// Source: http://docs.daz3d.com/doku.php/public/software/dazstudio/4/referenceguide/scripting/api_reference/samples/general_ui/display_document_dynamic/start
// 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" );
The attribution should point back to the source script.
You can put the file it is calling anywhere in your content directory, and the last line gets the relative path to it - e.g.
})( "Readme's/MyProject/instructions.txt" );
if you put the file in a folder named for your product in the readme's folder in the content directory.
---------------------------
I didn't explain why the original script was failing when used from a category - it gets the current path from the Content Library pane, but if you are in the category it won't have a file path, and if you are using Smart Content the Content Library may not be showing anything at all related to the product. If there had been stepwise checks on the original script that would have helped you to find the problem's cause.
Yes. Thanks for the explanation for the problem.
And thanks for your help using this script. It does work, even clicking on a reference to it among the CMS categories.