Skip navigation

applescript

222 Views 6 Replies Latest reply: Dec 1, 2012 5:37 AM by Jeff Shenk RSS
albar64 Calculating status...
Currently Being Moderated
Nov 30, 2012 2:04 AM

Hi all,

 

I'm new to this game and if my question is not at the right place I hope you'll excuse me.

 

The question is about "Pages" and "Applescript"

 

I have about 500 "Pages" documents in a folder and I need to pass them to .pdf format in order for them to be accessible to everybody.

So I wrote a script that  does just that: Finder opens the document and Pages saves it in .pdf. The problem is that Pages doesn't manage to close the original document  and give an error message. I've tried "close" alone and "close document", both followed by the name of the document or not, with the same result.

 

Here is  a simplified version of the script.

 

set chemin_in to "Actes"

tell application "Finder"

   

   

    count folder chemin_in

    set nombre_actes to the result

    set boucle to 0

   

   

    repeat nombre_actes times

        set boucle to boucle + 1

        set nom_acte to the name of file (boucle) of folder chemin_in

        open (document file nom_acte in folder chemin_in)

       

        set chemin_out to "Actes pdf:"

        --display alert chemin_out message boucle giving up after 1

       

        tell application "Pages"

           

            save front document in chemin_out & nom_acte as "SLDocumentTypePDF"

            activate document nom_acte

           close document nom_acte    (red color my choice to pinpoint what doesn't work) 

        end tell

        --close Finder window 1

       

    end repeat

end tell

Pages word processor, Mac OS X (10.5.8)
  • Jeff Shenk Level 4 Level 4 (2,000 points)
    Currently Being Moderated
    Nov 30, 2012 4:26 AM (in response to albar64)

    I am not sure why that line doesn't work; possibly it concerns the fact that two lines previously you created a second (pdf) document with the same name. While AppleScript error messages are often pretty cryptic, it probably would have been a good idea to include the wording of that message.

     

    I think I would change your Pages tell block to something like:

     

    tell application "Pages"

                set targetDocument to front document

                save targetDocument in chemin_out & nom_acte as "SLDocumentTypePDF"

                close targetDocument 

    end tell

     

     

    A couple years ago, MacOSXHints had a hint for doing something similar to what you are trying, with two nice scripts that might be useful to you.

  • Jeff Shenk Level 4 Level 4 (2,000 points)
    Currently Being Moderated
    Nov 30, 2012 7:44 AM (in response to albar64)

    Alain,

     

    I just made a test folder with five Pages documents in it, and ran a script with what was basically your original code, and it worked correctly. Strange, indeed.

    Have you tried "close front document" instead of referring to it by name?

     

    This is what I used, if you can see any differences that might be relevent:

     

    set theFolder to "OWC Mercury Elite AL Pro:Users:jeffshenk:Desktop:new_test:"

    set sourceFolder to "OWC Mercury Elite AL Pro:Users:jeffshenk:Desktop:new_test:Pages_docs"

    set destFolder to "OWC Mercury Elite AL Pro:Users:jeffshenk:Desktop:new_test:pdf_docs:"

     

    tell application "Finder"

              set theCount to (count folder sourceFolder)

              repeat with i from 1 to theCount

                        set nom_acte to the name of file i of folder sourceFolder

      open (document file nom_acte in folder sourceFolder)

     

                        tell application "Pages"

                                  save front document in destFolder & nom_acte as "SLDocumentTypePDF"

      activate document nom_acte

      close document nom_acte

                        end tell

              end repeat

    end tell


  • Jeff Shenk Level 4 Level 4 (2,000 points)
    Currently Being Moderated
    Dec 1, 2012 5:37 AM (in response to albar64)

    Alain,

     

    I'm glad you got it to work.

     

    I'm using AppleScript Editor version 2.3 (118) with AppleScript 2.1.2, on OS X 10.6.8, so there might be a difference in something there.

     

    From the error message you have been getting I wonder if the Finder's name for a file (which you use to set nom_acte) could differ in some detail from the Pages name for the document opened from that file. My tests   with a different system and different names have not shown any difference, though.

     

    Jeff

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.