Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Adding Hyperlinks via CSV File

Return to Member Forum

  • Author
    Posts
    • #57730
      lowjackson
      Member

      Hi

      I have built a 52 page product document in indesign cs5, and now have been asked to produce a PDF version with Hyperlinks on all the 300+ product names. The client is able to supply a spreadsheet of all the product names and URL in columns (XLS).

      Muppet Mark has gotten me this far but I'm not a javascript guy, The following script works with a csv file with 2 columns of “TEXT TO FIND” and “URL”.

      My problem is it only works on a single text frame, I need some repeat loop adding to do a whole doc, here the script…

      #target indesign

      function main() {
      if (app.documents.length == 0) {
      return;
      }else{
      var doc = app.activeDocument;
      var csv = selectCSV();
      if (csv != null) {
      var csvArray = readInDelimitedText(csv, ',')
      for (var i = 0; i < csvArray.length; i++) {
      // Your Hyperlink name from excel column 1
      var hn = csvArray[i][0];
      // Your Hyperlink URL from excel column 2
      var url = csvArray[i][1];
      // Not too sure about this method…
      var find = RegExp(hn,'gi');
      if (!inArray(doc.hyperlinks.everyItem().name, find)) {
      // This needs replacing with some find function?
      // I only have CS2 better let someone else do this part?
      // Set a text source
      var ts = doc.textFrames[0].words[i];
      addHyperlink(doc, ts, url, hn);
      }else{
      alert('Hyperlink name is in use…')
      }
      }
      }
      }
      }

      main();


      function selectCSV() {
      if (isMac()) {
      var csv = File.openDialog('Select an CSV file', function(f) {return (f instanceof Folder)||f.name.match(/.csv$/i);});
      }else{
      var csv = File.openDialog('Open Comma-delimited File', 'comma-delimited(*.csv):*.csv;');
      }
      return csv;
      }


      function addHyperlink(doc, ts, url, hn) {
      // Create the Hyperlink
      var hts = doc.hyperlinkTextSources.add(ts);
      var urld = doc.hyperlinkURLDestinations.add(url);
      var hl = doc.hyperlinks.add(hts, urld);
      // Edit the look of it here…
      hl.borderColor = [255,0,255];
      hl.borderStyle = HyperlinkAppearanceStyle.dashed;
      hl.highlight = HyperlinkAppearanceHighlight.outline;
      hl.name = hn;
      hl.width = HyperlinkAppearanceWidth.medium;
      hl.visible = true;
      }


      function readInDelimitedText(f, d) {
      var c = Array();
      f.open('r');
      while(!f.eof) {
      c.push(f.readln().split(d));
      }
      f.close();
      return c;
      }


      function inArray(arr, exp) {
      return exp.test(arr.join(''));
      }


      function isMac() {
      return /(Macintosh)/i.test($.os);
      }






      thanks




      LJ

Viewing 0 reply threads
  • You must be logged in to reply to this topic.
Forum Ads