Writing a little script to open a PDF file?

Hey guys,

I am looking for a little script that will open up a PDF user-guide when clicked on. The purpose is simple: Rather than having the user search the support folder for my PDF manuals, I would like to include a "link" to them directly in my product folders. I saw some other PA's have done this with their products, but I do not quite understand the code for the script. 

Any help in this regard would be appreciated,

-P

Comments

  • barbultbarbult Posts: 23,126
    edited February 2018

    // DAZ Studio version 4.10.0.123 filetype DAZ Script
    // Script example provided by rbtwhiz https://www.daz3d.com/forums/discussion/30942/solved-help-with-app-showurl-for-opening-local-files
    //
    // Create a file info object, using the path of the current script
    var oFileInfo = new DzFileInfo( getScriptFileName() );
    // Prompt the operating system to do whatever it does with the given file type; same path/name as script
    App.showURL( String("file:///%1/%2.pdf").arg( oFileInfo.path() ).arg( oFileInfo.baseName() ) );

    Post edited by barbult on
  • barbultbarbult Posts: 23,126
    edited February 2018

    Sorry about the missing indentation. I just cut and pasted it and that is what happened. Follow the linked discussion to see the forum thread where Rob provided this information. The thread discusses what the script does and how it works and some path name tips. Name the script the same as your PDF file name. Provide a PNG with the same name for a thumbnail.

    Post edited by barbult on
  • jag11jag11 Posts: 885

    This script requires path to be relative to your content, like in contentToLaunch:

    // DAZ Studio version 4.10.0.107 filetype DAZ Script(function () {    var contentToLaunch = '/Shaders/iray/jag11/MDL_spec_1.3.4_13Jan2017.pdf';	var contentMgr = null;	var fullPath = (contentMgr = App.getContentMgr()).findFile(contentToLaunch);		if(!fullPath) {		var message = [];				message.push("Could not find required content file:");		message.push("\n\n");		message.push(contentToLaunch);		message.push("\n\n");		message.push("Make sure path to file is added to Content Directory Manager.");			    MessageBox.critical(message.join(""), "Fatal Error", "&OK");	    		return;    }        App.showURL("file:///" + fullPath);    })();
  • macleanmaclean Posts: 2,438
    edited February 2018

    P

    The link Richard gave is the one I use. All you need to change is the last line. Like this

    })( "My Folder/My Product/Product Help.pdf" );

    Save it as a .dsa wih the same name as the ,pdf and put them both in your content folder along with a .png. Double-clicking it will open the .pdf.

    mac

    Edit - DAZ like you to include a link to the original scipt for the benefit of other users. I added the link at the end of my .pdf

    Post edited by maclean on
  • maclean said:
    Edit - DAZ like you to include a link to the original scipt for the benefit of other users. I added the link at the end of my .pdf

    It's not that Daz likes this, rather it's the requirement of the license model they (or Rob) chose to use for these samples to ensure that they coudl be used to benefit everyone. You can download the script, including the copyright statement and URL, by clicking on its name in the tab at the top of the script box.

  • PA_ThePhilosopherPA_ThePhilosopher Posts: 1,039
    edited February 2018

    So helpful guys, as always. Thanks once again for the huge assist. Now my customers won't be fumbling around anymore looking for the PDF user guide. *pleased*

    -P

    Post edited by PA_ThePhilosopher on
  • What should I do if these scripts are not working? Neither my own, neither any what included with others' product.
    I have acrobat reader. And I know mine should work, because I asked others and they can use it.

    I have windows 10 pro 64-bit

     

  • NorthOf45NorthOf45 Posts: 5,204

    Check your WIndows Settings -> Apps -> Default Apps. At the bottom there is a link to "Set defaults by app". You will see a page called "Set your default programs". Click on Adobe Acrobat Reader DC (or whatever version you have) and select Manage. The file types listed should be associated with Adobe Acrobat Reader. If not "Choose a Default", "Adobe Acrobat Reader" should be in the list that pops up.

  • It is there and if I open a pdf from the file explorer, acrobat reader opens it. And some years ago I could use this script without any problem. And 1-2 years ago I noticed I can't use it anymore .... and really don't know what changed ... If nothing to set inside Daz Studio ... than I don't know what happened :(

  • NorthOf45NorthOf45 Posts: 5,204
    edited December 2019

    If you right-click -> "Open in Script IDE..." and hit the Execute button, is there any error message in the bottom pane?

    Post edited by NorthOf45 on
  • It says ... 
     

    Executing Script...
    Result: 
    Script executed in 0 secs 87 msecs.

     

    But nothing happen

  • NorthOf45NorthOf45 Posts: 5,204
    edited December 2019

    Hm-m. Just to be certain, are you trying to run something using the Display_Document_Dynamic script that Richard linked in? Or the shorter 2-line script sometimes used? Does either type work, or do none of them work?

    Jumping ahead a little, have you tried creating one like maclean suggested, using the Display_Document_Dynamic.dsa script? You just need to change that one line at the end to point to a file in your library, using the relative path, and place it in any folder in your library.

    The script can invoke any application that has an associated file type recognized by Windows, like image files, text files, or even videos. You could place a simple "Hello World" text file somewhere in your library and make a script to open it with whatever application would open .txt files (i.e., Notepad). If that works, then we would know that the functionality is there. If not, well...

    Post edited by NorthOf45 on
  • I used several once .... at least tried to :)

    For example in the product Map Mapker Pro by MikeD, the script's main window includes a button to open a pdf or a link to a video. The video is working, the pdf is not.

    Or I tried this one ... 

    var g_oFILE = new DzGZFile( getScriptFileName() );

    var g_sSCRIPT_NAME = String( "%1.%2" ).arg( g_oFILE.baseName() ).arg( g_oFILE.extension() );

    var g_sSCRIPT_LOCATION = g_oFILE.path();

    App.showURL( String("file:///%1/blablabla.pdf").arg( g_sSCRIPT_LOCATION ) );

     

     

    or tried this one:

     

    //Script generated by BlackFeather's CL_FileLauncher

    var sPath = MainWindow.getPaneMgr().findPane("DzContentLibraryPane").getSelectedContainer().getFullPath();

    App.showURL("file:///" + sPath + "/Celinette Clothes 0INFO.pdf");

     

    In this last one what I made, if I change the pdf to tip.png, it opens the Windows Photo Viewer which was associated with the png files. So the script is working, only the pdf has problem :(

    I tried to install the latest acrobat reader, but it say, the latest one is installed. And when I open any pdf from the windows explorer, the Acrobat reader opens the file without any problem, as it is the associated program.

     

     

    Or when I made another one for a G3 clothes, the QA changed mine for this:

    // DAZ Studio version 4.8.0.59 filetype DAZ Script
    //*********************************************************************
    var oContentMgr = App.getContentMgr();
    var relPath = "/readme's/";
    App.showURL( oContentMgr.findFile( relPath + "36035_Patty-Clothes-User-Guide.pdf" ) );

    //var relPath = "/ReadMe's/";
    //var importFolders = false;
    //var absPath = App.getContentMgr().getAbsolutePath( relPath, importFolders );
    //App.showURL( String(absPath + "Patty Clothes User Guide.pdf") );

     

    And they told me it is working, but for me it was not. However when I used Blackfeather's for a .txt file, it is still working.

    //Script generated by BlackFeather's CL_FileLauncher

    var sPath = MainWindow.getPaneMgr().findPane("DzContentLibraryPane").getSelectedContainer().getFullPath();

    App.showURL("file:///" + sPath + "/Patty Clothes 0INFO.txt");

     

  • Oh, now I uninstalled my latest Acrobat Reader and downloaded an older one (XI) and now it is working .. .damn adobe!!!!

    But when I installed the Adobe Acrobat Reader DC, it was stopped working again. So it is adobe reader issue :( At first I thought maybe because I used the Hungarian version, so uninstalled, installed the English one, but still ... not working :( 

    Any idea what should I set?

  • NorthOf45NorthOf45 Posts: 5,204

    I have never changed any settings in Acrobat, but looking at the preferences, there are a lot. Maybe something in there is interfering, but hard to say for sure. Sorry, but I really do not know where to go from here.

  • Never mind, thank you for your help, at least I figured out it is Adobe related!

  • jjoynerjjoyner Posts: 468

    NorthOf45 said:

    If you right-click -> "Open in Script IDE..." and hit the Execute button, is there any error message in the bottom pane?

     

    In exploring a video tutorial set (released in 2018) that I purchased in the store in 2020, the product installs its video files (WMV) and Daz scripts (DSA) in the Runtime/General/… folder.  None of the scripts launch its associated WMV file though.  The code in all of the script files is identical to that provided by rbtwhiz in 2013 in the Help with App.showURL for opening local files thread (https://www.daz3d.com/forums/discussion/30942/solved-help-with-app-showurl-for-opening-local-files):

        // DAZ Studio version 4.8.0.18 filetype DAZ Script

        // Create a file info object, using the path of the current script

        var oFileInfo = new DzFileInfo( getScriptFileName() );

        // Prompt the operating system to do whatever it does with the given file type; same path/name as script

        App.showURL( String("file:///%1/%2.wmv").arg( oFileInfo.path() ).arg( oFileInfo.baseName() ) );

    I tested other file types by creating a text file (TXT), Word file (DOCX), Adobe file (PDF), web file (HTML), audio file (.MP3) and a few others and changed the file extension in the script from WMV to TXT, DOCX, etc.  When I launched the scripts, the designated file was opened in its associated application.  I tested a few other file types - image (.JPG), video (MP4), Apple video (MOV)  – that failed to open when I launched the script.

    I also tested the script in the Daz Studio Script IDE.  The output was

        Executing Script...

        Result:

        Script executed in 0 secs 38 msecs.

    I can manually launch the tutorials WMV videos but I’d like to try to determine why the script isn’t launching the videos.

    Any help, thoughts, advice will be greatly appreciated.

  • NorthOf45NorthOf45 Posts: 5,204

    Are the reluctant file types associated with a valid application in the OS? I use (on Win10) the display_document_dynamic script linked by Richard. Works with .pdf, .jpg, .png, .mp4, .mov and .wmv, among others. It will open a file explorer if you supply just a folder.

    As mentioned earlier, the source must be in a mapped folder, but that usually throws an error if it isn't.

     

  • Right, if th script is working for soem file types it is working - the issue is whether there is a default handler for the file type, because the script is just passing the file name to the OS and saying open this in the default application.

  • jjoynerjjoyner Posts: 468
    edited July 2022

    NorthOf45 said:

    Are the reluctant file types associated with a valid application in the OS? I use (on Win10) the display_document_dynamic script linked by Richard. Works with .pdf, .jpg, .png, .mp4, .mov and .wmv, among others. It will open a file explorer if you supply just a folder.

    As mentioned earlier, the source must be in a mapped folder, but that usually throws an error if it isn't.

    Richard Haseltine said:

    Right, if th script is working for soem file types it is working - the issue is whether there is a default handler for the file type, because the script is just passing the file name to the OS and saying open this in the default application.

     Yes, before posting I had checked that WMV, MOV and JPG were associated with a default application.  As further evidence of that, when I double click each of those file types, they are opened in the associated applicastion.  I am running Windows 10 Pro.

    Whether the script works or not is not the issue; I know that it works for some file types but not other.  Once/If I find a solution, I plan to modify each of the scrips in the video tutorial package so that, if desired, I can launch them from my Smart Content or Content Library.  I'll take another look the display_document_dynamic script and give it a try if all else fails.

    Post edited by jjoyner on
  • jjoynerjjoyner Posts: 468
    edited July 2022

    I tried the display_document_dynamic script but still could not get it to open a WMV file.  I downloaded the script, saved a copy as Joe.dsa, and moved it to the folder containing the vendor's dsa scripts that don't open the product's WMV files.  I also created files named Joe.wmv, Joe.txt, Joe.docx, Joe.pdf, and Joe.jpg. In Joes.dsa, I then modified the name of the file to open to match the files types that i just listed.  Joe.dsa opend the text file, the Word file, and the Acrobat file but it did not open the WMV file or the JPG file.

    Again, this is not a major issue since I can open the WMV files manually.  It's more about why?

    :(

    Post edited by jjoyner on
  • What happens if you find the file in a browser and double-clic it - does it play, or are you asked what you want to do with it?

  • jjoynerjjoyner Posts: 468

    Richard Haseltine said:

    What happens if you find the file in a browser and double-clic it - does it play, or are you asked what you want to do with it?

    I checked the associations in Windows Settings a few days ago and again today.  Double clicking any WMV file opens it in Windows Media Player.  Double clicking any JPG file opens in Paint.

  • I found this thread this morning (would also like to open a pdf via script), but struggled for some time with the "relative path". For programmers or HTML developers like me, this term has a different meaning than for the Studio app. Therefore, I would like to add a little information to this thread, as other (later) viewers may also struggle with this "relative path" thing.

    In short (and simplified): The Studio app script uses the folder "My DAZ 3D Library", which contains directories such as "data", "People", "Documentation", "Runtime" etc. as source.

    2 examples:

    If you want to save your PDF in the "Documentation" folder, the "relative path" should be as follows: Documentation/your.pdf

    If you want to save your PDF in a folder you have created for your own garments for GF8, the path might look like this:

    People/Genesis 8 Female/Clothing/Your Garment/your.pdf

    This works well with the script from rbtwhiz, that Richard has linked (see above)

Sign In or Register to comment.