Skip navigation
Currently Being Moderated

[JS] Can an InDesign/InCopy script cause a web browser to open?

Sep 20, 2010 1:22 PM

I work for a newspaper and am trying to create a very rudimentary system to integrate print with the web.  Very, very rudimentary.

 

My idea is this:

 

All the articles on our website have an ID number, with a corresponding URL, so an article might be something like "http://www.ournewspaper.com/articles/83920".  From there, there's an edit button you can click on (if you're logged in to edit the website), which will take you to the back end, where you enter all the metadata for the story (headline, author, image captions, related links, tags, etc.).

 

I was thinking of having each InCopy story linked to its corresponding article on the web, so that people could click on the story in the InDesign layout (either from InCopy or InDesign) and run a script which would give them the ID number of the story in our website's CMS.  This ID number would be stored as a label of the Story object.  Once the person who is editing the story has access to the web page for that same story, they can start typing in all that metadata, and after the paper has gone to print, they can upload the story itself (processed by another script I wrote).

 

If worst comes to worst, I could just have the script provide the user with the URL for the story, and they can cut and paste it into a browser window.  But I have a feeling this will not make our editors happy.

 

So my question is:  does anybody know a way we can have the script open a browser, and point it to the appropriate URL?

 

Also, if anyone knows of any cheap and simple solutions that have already been created along these lines, I'd be happy to hear them.

 

Thanks,

 

Richard Harrington

 
Replies
  • Currently Being Moderated
    Sep 20, 2010 2:10 PM   in reply to richardh6

    Sure that my fellow-scripters will provide a simple answer to your request!

    (I really have no experience in app/url targetting from InDesign.)

     

    However this snippet seems to work on WindowsXP.

     

    function openInBrowser(/*str*/url)
    {
        var    f = new File(Folder.temp.absoluteURI +
            '/url_' + (+new Date()) + '.url' );
       
        var fStr = "[InternetShortcut]\rURL="+url;

     

        f.open('w');
        f.write(fStr);
        f.close();
        f.execute();
        f.remove();
    }

     

    // Test:
    openInBrowser("http://www.indiscripts.com/");

     

     

    The script simply creates a temporary URL Shortcut File and executes it. The default browser is launched and the web page is displayed.


    @+

    Marc

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2010 1:31 AM   in reply to Marc Autret

    Here's Gerald Singelmann's function using your method which works both on Mac and Windows:

     

    The adavntage of this approach is that it works event while InDesign is locked in a modal state...

     

        function openWebsite(ws) {
            if (File.fs == "Macintosh") {
                var tempFile = File("~/Desktop/tempurl.webloc");
                tempFile.open("w");
                tempFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\
                <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\
                <plist version=\"1.0\">\
                    <dict>\
                        <key>URL</key>\
                        <string>"+ ws +"</string>\
                    </dict>\
                </plist>");
                tempFile.close();
                tempFile.execute();
                for (var aux = 0; aux < 100; aux++) {
                    $.sleep(10);
                }
                tempFile.remove();
            } else {
                var tempFile = File("~/Desktop/cuppascript.url");
                tempFile.open("w");
                tempFile.write("[InternetShortcut]\rURL="+ws);
                tempFile.close();
                tempFile.execute();
                for (var aux = 0; aux < 100; aux++) {
                    $.sleep(10);
                }
                tempFile.remove();
            }
        }
     
    

     

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2010 4:25 AM   in reply to Harbs.

    This one, too, (due to Dave Saunders) is platform neutral. I doubt it will work if ID is locked in a modal state, but I whether that's an advantage I don't know.

     

    Peter

     

    function goToURL (theURL)
       {
         var aDoc = app.documents.add (false);
         var myDest = aDoc.hyperlinkURLDestinations.add (theURL);
         myDest.showDestination ();
         aDoc.close (SaveOptions.no);
         }

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2010 4:47 AM   in reply to Peter Kahrel

    This is perfect!

     

    Thanks Peter and Dave!

     

    --

    tomaxxi

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2010 4:59 AM   in reply to Peter Kahrel

    pkahrel wrote:

     

    This one, too, (due to Dave Saunders) is platform neutral. I doubt it will work if ID is locked in a modal state, but I whether that's an advantage I don't know. (...)

     

    Typically, Gerald's approach (modal state safe) allows to launch the browser from a ScriptUI dialog, while Dave's approach needs to invoke the InDesign DOM (create a ghost document, etc.) which fails within a modal dialog.

     

    BTW, here's a factorized code:

     

    function openInBrowser(/*str*/ url)
    {
         var isMac = (File.fs == "Macintosh"),
              fName = 'tmp' + (+new Date()) + (isMac ? '.webloc' : '.url'),
              fCode = isMac ?
                   ('<?xml version="1.0" encoding="UTF-8"?>\r'+
                   '<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" '+
                   '"http://www.apple.com/DTDs/PropertyList-1.0.dtd">\r'+
                   '<plist version="1.0">\r'+
                   '<dict>\r'+
                        '\t<key>URL</key>\r'+
                        '\t<string>%url%</string>\r'+
                   '</dict>\r'+
                   '</plist>') :
                   '[InternetShortcut]\rURL=%url%\r';

         var f = new File(Folder.temp.absoluteURI + '/' + fName);
         if(! f.open('w') ) return false;

         f.write(fCode.replace('%url%',url));
         f.close();
         f.execute();
         $.sleep(500);     // 500 ms timer
         f.remove();
         return true;
    }

    // Test:
    openInBrowser("http://www.indiscripts.com/");

     

    @+

    Marc

     

    [Harbs, why doesn't the syntax highlighter work anymore from FF?]

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2010 5:59 AM   in reply to Marc Autret

    FWIW,

     

    doScript() works in CS5 even when the app is in a modal state and I don't like writing files unnecessarily...

     

    Here's the approach I use:

            function GotoLink(url){
                url = url || "http://in-tools.com";
                if(kAppVersion>6){
                    if(File.fs=="Macintosh"){
                        var body = 'tell application "Finder"\ropen location "'+url+'"\rend tell';
                        app.doScript(body,ScriptLanguage.APPLESCRIPT_LANGUAGE);
                    } else {
                        var body =  'dim objShell\rset objShell = CreateObject("Shell.Application")\rstr = "'+url+'"\robjShell.ShellExecute str, "", "", "open", 1 '
                        app.doScript(body,ScriptLanguage.VISUAL_BASIC);
                    }
                }
                else {
                    linkJumper = File(Folder.temp.absoluteURI+"/link.html");
                    linkJumper.open("w");
                    var linkBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL='+url+'"></head><body> <p></body></html>'
                    linkJumper.write(linkBody);
                    linkJumper.close();
                    linkJumper.execute();
                }
            }
     
     
    

     

    Marc Autret wrote:

     

    [Harbs, why doesn't the syntax highlighter work anymore from FF?]

    It works for me. How are you using it?

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2010 7:40 AM   in reply to Harbs.

    > It works for me. How are you using it?

     

    Insert > Syntax Highlighting > Java

     

    It worked before. Not the good way?

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 3, 2010 7:51 AM   in reply to Marc Autret

    That's what I do from FF Mac.

     

    Weird...

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    Oct 21, 2010 3:08 PM   in reply to richardh6

    richardh6 wrote:

    1.  What's kAppVersion, in line 3?  My interpreter doesn't recognize that word.  I changed that line to

    Sorry about that. I always initialize my script with a global variable for versioning purposes. My functions just assume that it's already been defined...

     

    I intialize it like this:

     

    kAppVersion  = parseFloat(app.version);

    2.  In the case of versions below 7, on the Mac, when you say

    linkJumper.execute();

     

    it opens the file in my text editor, which is what .html files are set to open with on my computer.  Is there a way to force it to open in a web browser without using doScript, which will not open in a modal state?

     

    This is an academic matter for me, as the typical user always has .html files set to open in browsers, and also everyone at my place of work is running CS5.  But I'd be curious to know.

     

    Good point. That's another reason why Gerald's method of writing the files is better...

     

    Harbs

     
    |
    Mark as:
  • Currently Being Moderated
    May 9, 2012 3:08 AM   in reply to Harbs.

    I think this way is very easy.

    I use a redirect javascript code.

     

     

    var url = "http://forums.adobe.com/message/3180931";
    //Example goToUrl(url);
    function goToUrl(url){
    var text='';
    var file = "/temp.html";
    file = new File(Folder.temp.absoluteURI+file);
    file.encoding = "UTF-8"; file.open("w");
    file.write(text);
    file.close();
    file.execute();
    }
    
    
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points