15 Replies Latest reply on Mar 28, 2010 11:42 PM by John Hawkinson

    export and email pdf script

    andrewbish

      I am using a script that creates an email version pdf.

       

      Does anyone know a way to then put that PDF into an email straightaway. or at the very least open the var myfolder

       

       

       

       

       

      //Exports current document to email version PDF

      if(app.documents.length != 0){
      if(!app.activeDocument.saved || app.activeDocument.modified) {
        alert('Please save your document before exporting.');
      } else {
        if(exportPDF()) alert('Documents exported successfully!');
      }
      } else {
      alert("Please open a document and try again.");
      }

       


      function exportPDF(){
      var myFilePath = "";
      var myDocument = app.activeDocument;
      var myFolder = myDocument.filePath;
      var myDocumentName = myDocument.name.replace(".indd", "");
      // define export preset
      var myPDFExportPreset = app.pdfExportPresets.item("[Smallest File Size]");
      try {
        // create file path
        myFilePath = myFolder + "/" + myDocumentName + ".pdf";
        // prepare new file
        myFile = new File(myFilePath);
        // export file
        myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
      } catch (e) {
        alert("There was an error creating the pdf." + e);
        return false;
      }
      return true;
      }

        • 1. Re: export and email pdf script
          [Jongware] Most Valuable Participant

          Platform? (Because on a Mac you can have it nest an AppleScript that might be able to do just that. There is nothing like it for Windows -- it's so unfair ...)

           

          The second option, opening the folder, is easy -- put this at the end of your script:

           

          myFolder.execute();

          • 2. Re: export and email pdf script
            Muppet Mark-QAl63s Level 4

            This is doable on the mac using AppleScript both Address Book & Mail are fully scriptable. The nice thing is if you've got an organized Address Book then sending to groups/multi-groups is fairly easily done.

            • 3. Re: export and email pdf script
              Kasyan Servetsky Level 5

              I am writing a script that does this. On windows, I did this via Acrobat — I wrote about it in this post.
              For Mac, I wrote a function in AS that creates a message in Mail application and attaches a pdf file.

               

              function SendEmail(myFile, mySubject, myMessage) {
                   if (File.fs == "Macintosh") {
                        var myPath = myFile.fsName;
                        if (myPath.match("/Volumes/") != null) {
                             var myMacPath = myPath;
                             var myAppleScript = 'tell application "Mail"\r';
                             myAppleScript += 'set newMessage to make new outgoing message\r';
                             myAppleScript += 'tell newMessage\r';
                             myAppleScript += 'set visible to true\r';
                             myAppleScript += 'set subject to "' + mySubject + '"\r';
                             myAppleScript += 'set content to "' + myMessage + '"\r';
                             myAppleScript += 'make new attachment with properties {file name:"' + myMacPath + '"} at after the last paragraph\r';
                             myAppleScript += 'end tell\r';
                             myAppleScript += 'end tell\r';
                             app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage);
                        }
                        else {
                             var myMacPath = myPath.replace(/\//g, ":");
                             var myAppleScript = 'tell application "Mail"\r';
                             myAppleScript += 'set newMessage to make new outgoing message\r';
                             myAppleScript += 'tell newMessage\r';
                             myAppleScript += 'set visible to true\r';
                             myAppleScript += 'set subject to "' + mySubject + '"\r';
                             myAppleScript += 'set content to "' + myMessage + '"\r';
                             myAppleScript += 'tell application "Finder"\r';
                             myAppleScript += 'set MyDiskName to name of startup disk\r';
                             myAppleScript += 'end tell\r';
                             myAppleScript += 'make new attachment with properties {file name:(MyDiskName &"' + myMacPath + '") as alias} at after the last paragraph\r';
                             myAppleScript += 'end tell\r';
                             myAppleScript += 'end tell\r';
                             app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage);
                        }
                   }
              }
              
              • 4. Re: export and email pdf script
                Muppet Mark-QAl63s Level 4

                Kasyan, I was pleased to see this is very close to what i have too. I didn't post because I was not sure if it was going to be of any use to people who don't know AppleScript. On a side note was your PC & Acrobat route totally invisible to the user? Here are some of the Address Book options that could probably do with cleaning up (my JS skills are only at the noob level). They should pass back Arrays that a you can parse thru with JavaScript then use in Mails 'to', 'cc' & 'bcc'.

                 

                #target indesign

                 

                var allGroups = getAllGroups();

                for (var i = 0; i < allGroups.length; i++) {

                $.writeln(allGroups[i]);

                }

                 

                var allNames = getAllNames();

                for (var i = 0; i < allNames.length; i++) {

                $.writeln(allNames[i]);

                }

                 

                var namesInGroup = getNamesInGroup('IT');

                for (var i = 0; i < namesInGroup.length; i++) {

                $.writeln(namesInGroup[i]);

                }

                 

                var nameAddresses = getNameAddresses('Mark Larsen');

                for (var i = 0; i < nameAddresses.length; i++) {

                $.writeln(nameAddresses[i]);

                }

                 

                function getAllGroups() {

                var allGroups = ''

                + 'Get_Groups()' + '\n'

                + 'on Get_Groups()' + '\n'

                + 'tell application "Address Book"' + '\n'

                + 'set The_Groups to name of every group' + '\n'

                + 'end tell' + '\n'

                + 'return The_Groups' + '\n'

                + 'end Get_Groups' + '\n'

                var groupList = app.doScript(allGroups, 1095978087);

                groupList = groupList.toString().split(',');

                return groupList;

                }

                 

                function getAllNames() {

                var allNames = ''

                + 'Get_Names()' + '\n'

                + 'on Get_Names()' + '\n'

                + 'tell application "Address Book"' + '\n'

                + 'set Name_List to name of every person' + '\n'

                + 'end tell' + '\n'

                + 'return Name_List' + '\n'

                + 'end Get_Names' + '\n'

                var nameList = app.doScript(allNames, 1095978087);

                nameList = nameList.toString().split(',');

                return nameList;

                }

                 

                function getNamesInGroup(thisGroup) {

                var namesInGroup = ''

                + 'Get_Group_Names()' + '\n'

                + 'on Get_Group_Names()' + '\n'

                + 'tell application "Address Book"' + '\n'

                + 'set Name_List to name of every person of group "' + thisGroup + '"\n'

                + 'end tell' + '\n'

                + 'return Name_List' + '\n'

                + 'end Get_Group_Names' + '\n'

                var nameList = app.doScript(namesInGroup, 1095978087);

                nameList = nameList.toString().split(',');

                return nameList;

                }

                 

                function getNameAddresses(thisName) {

                var nameAddresses = ''

                + 'Get_Addresses()' + '\n'

                + 'on Get_Addresses()' + '\n'

                + 'tell application "Address Book"' + '\n'

                + 'set Address_List to value of every email of person "' + thisName + '"\n'

                + 'end tell' + '\n'

                + 'return Address_List' + '\n'

                + 'end Get_Addresses' + '\n'

                var addressList = app.doScript(nameAddresses, 1095978087);

                addressList = addressList.toString().split(',');

                return addressList;

                }

                • 5. Re: export and email pdf script
                  Kasyan Servetsky Level 5

                  On a side note was your PC & Acrobat route totally invisible to the user?

                  It depends on bUI parameter in acrobat script:

                  bUI: false // send e-mail silently

                  bUI: true// show dialog

                   

                  I didn't post because I was not sure if it was going to be of any use to people who don't know AppleScript.

                  I am personally interested in such exchange of know-how, and gathering a collection of other people's tips and tricks. So, next time make sure to post your code.

                   

                  Kasyan

                  • 6. Re: export and email pdf script
                    Muppet Mark-QAl63s Level 4

                    Kasyan as your interested in this stuff my approach was slightly different. I was trying to see if I could return information from AppleScript. Parse/Manipulate in JavaScript pass back to Applescript. Not finished but works, here it where my experiments got me so far. Items from the returned arrays of my previous post can be used or concatenated to strings and passed back to another AppleScript call. I have used delays as my mail@home is a tad dozzy… I still need to check that if a persons name is used then they only have the 1 address.

                     

                    #target indesign

                     

                    var subject = 'Testing if this works?';

                    var to = '"someone@somewhere.com"';

                    var cc = '"Some Group"';

                    var bcc = '"someoneelse@somewhereelse.co.uk"';

                    var someText = 'New Test Attachment…'

                     

                    var thisFile = new File('~/Desktop/TestImage.png');

                     

                    if (thisFile.exists) {

                    thisFile = thisFile.fsName;

                    launchMail();

                    sendMail(subject, to, cc, bcc, someText, thisFile);

                    quitMail();

                    }

                     

                    function sendMail(subject, to, cc, bcc, content, attFile) {

                    var mailScript = ''

                    + 'set To_List to {'+ to + '}\n'

                    + 'set CC_List to {'+ cc + '}\n'

                    + 'set BCC_List to {'+ bcc + '}\n'

                    + 'set This_File to (POSIX file "' + attFile + '") as alias' + '\n'

                    + 'tell application \"Mail\"' + '\n'

                    + 'set This_Mail to make new outgoing message with properties ¬' + '\n'

                    + '{subject:"' + subject + '", visible:false}' + '\n'

                    + 'tell This_Mail' + '\n'

                    + 'repeat with i from 1 to count of To_List' + '\n'

                    + 'if item i of To_List contains "@" then' + '\n'

                    + 'make new to recipient with properties {address:item i of To_List}' + '\n'

                    + 'else' + '\n'

                    + 'make new to recipient with properties {name:item i of To_List}' + '\n'

                    + 'end if' + '\n'

                    + 'end repeat' + '\n'

                    + 'repeat with i from 1 to count of CC_List' + '\n'

                    + 'if item i of To_List contains "@" then' + '\n'

                    + 'make new cc recipient with properties {address:item i of CC_List}' + '\n'

                    + 'else' + '\n'

                    + 'make new cc recipient with properties {name:item i of CC_List}' + '\n'

                    + 'end if' + '\n'

                    + 'end repeat' + '\n'

                    + 'repeat with i from 1 to count of BCC_List' + '\n'

                    + 'if item i of To_List contains "@" then' + '\n'

                    + 'make new bcc recipient with properties {address:item i of BCC_List}' + '\n'

                    + 'else' + '\n'

                    + 'make new bcc recipient with properties {name:item i of BCC_List}' + '\n'

                    + 'end if' + '\n'

                    + 'end repeat' + '\n'

                    + 'set the content to "' + content + '" & return & return' + '\n'

                    + 'tell content' + '\n'

                    + 'make new attachment with properties  ¬' + '\n'

                    + '{file name:This_File} at after the last paragraph' + '\n'

                    + 'end tell' + '\n'

                    + 'end tell' + '\n'

                    + 'delay 1' + '\n'

                    + 'send This_Mail' + '\n'

                    + 'delay 10' + '\n'

                    + 'end tell' + '\n'

                    app.doScript(mailScript, 1095978087);

                    }

                     

                    function launchMail() {

                    var launchMail = ''

                    + 'tell application \"Mail\"' + '\n'

                    + 'activate' + '\n'

                    + 'delay 0.5' + '\n'

                    + 'set visible of every window to false' + '\n'

                    + 'delay 2' + '\n'

                    + 'end tell' + '\n'

                    app.doScript(launchMail, 1095978087);

                    }

                     

                    function quitMail() {

                    var quitMail = ''

                    + 'tell application \"Mail\"' + '\n'

                    + 'quit' + '\n'

                    + 'end tell' + '\n'

                    app.doScript(quitMail, 1095978087);

                    }

                    • 7. Re: export and email pdf script
                      Kasyan Servetsky Level 5

                      It's very interesting, Mark. Thanks.

                      I can't test your script until Wednesday — don't have Mac.

                       

                      I was trying to see if I could return information from AppleScript. Parse/Manipulate in JavaScript pass back to Applescript.

                       

                      Have you tried to use script arguments for this purpose?

                       

                      Some of the ScriptArg's methods baffle me:

                      What is the difference between get and getValue (and accordingly set getValue) methods?

                      What restore and save methods are used for?

                       

                      Kasyan

                      • 8. Re: export and email pdf script
                        Muppet Mark-QAl63s Level 4

                        Kasyan, I have looked at the ID app's script arguments and was a confused by them. Im a fairly recent convert from the Mac/AppleScript side and to be honest the differences between scripting these apps in ExtendScript has me stumped at times. I've left scripting ID until last as it was the one I started to use last plus its big one. Coming in from AppleScript I have been used to a lot more information being freely available to me than what can be got 'directly' from ExtendScript. What I do like and think ID got spot on is this doScript() allowing you to reach out further on either platform although personally its only the mac one that I use.

                         

                        In the Address Book functions I have used 'value of' for the case of 'email' the reason being that a UUID? would otherwise be returned.

                         

                        My experiments were to try make this email invisible in the GUI but it does NOT work as I would have liked. There is a shell option as well as mail but I have NOT looked at that yet.

                         

                        Let me know how you get on when at a mac… I'd be interested to know if it works for others…

                         

                        Adobe please put system() out of Bridge into all apps with stdout back. I'll have another argument for my upgrade list… {}

                        • 9. Re: export and email pdf script
                          Harbs. Level 6

                          I've never actually used them myself, but I think environment 

                          variables are an easier way of passing data between different processes.

                           

                          Check out $.setenv() and $.getenv()...

                           

                          Harbs

                          • 10. Re: export and email pdf script
                            Muppet Mark-QAl63s Level 4

                            Harbs, I only have $.getenv() a CS2 thing no $.setenv() not much in the way of an example as to what this does?

                            • 11. Re: export and email pdf script
                              Harbs. Level 6

                              Hmm.

                               

                              CS2 was a long time ago. I didn't remember that it was missing there 

                              (or I never knew...).

                               

                              I'm not so clear on them myself, but basically it sets variables for 

                              the user environment accessible by all processes run by the user...

                               

                              http://en.wikipedia.org/wiki/Environment_variable

                               

                              Harbs

                              • 12. Re: export and email pdf script
                                Kasyan Servetsky Level 5

                                I just remembered about another option: send an e-mail using socked object. Long ago I played with BasicEmailer.jsx example from Bridge CS3 SDK — and it worked well for me: sent e-mail silently without user interaction. There's also another example — BackgroundEmailer.jsx. As far as I understand, the sockedobject is available to InDesign too, so you should be able to send e-mails from InDesign as well.

                                However, the authors of the script warn that it may not run as expected with all mail servers, and is not intended to be a robust mail client.

                                 

                                Kasyan

                                • 13. Re: export and email pdf script
                                  John Hawkinson Level 5

                                  Changes to environment variables are only accessible to children of the current process. If that works for you, great. But they're not accessible to all processes of the current user.

                                   

                                  Writing your own MUA (Mail User Agent) is probably not a good idea. You're much better handing off to a dedicated piece of software that knows how to do it and will reliably work in all environments. The standard Unix answer is to call the sendmail program, which might work for you on the Mac, but certainly won't work under Windows. I dont' think there's a good portable solution... (and you'd still be stuck producing your own MIME-compliant message, which would mean base64-encoding your PDF file, etc., etc. Piles of stuff that is probably better not done by you and is really out of scope for this forum...).

                                  • 14. Re: export and email pdf script
                                    Harbs. Level 6

                                    John Hawkinson wrote:

                                    ...But they're not accessible to all processes of the current user...

                                     

                                    Thanks for that! Like I said; I've never used them myself...

                                     

                                    Do you know what the parent process would be in this case? InDesign? When a doScript runs an AppleScript which runs another app?

                                     

                                    Harbs

                                    • 15. Re: export and email pdf script
                                      John Hawkinson Level 5

                                      Yup, that's fine. It's possible to imagine cases where it isn't true, but if InDesign runs a javascript which changes some environment variables and then runs an AppleScript via doScript, then the AppleScript and it's children see the changes.

                                       

                                      For instance, here we display the USER environment variable, then change it, and display it again:

                                      app.doScript('do shell script "echo $USER" ',ScriptLanguage.APPLESCRIPT_LANGUAGE);
                                      Result: writer
                                      $.setenv("USER","harbs")
                                      Result: undefined
                                      $.getenv("USER")
                                      Result: harbs
                                      app.doScript('do shell script "echo $USER" ',ScriptLanguage.APPLESCRIPT_LANGUAGE);
                                      Result: harbs