Previous 1 2 Next 20 Replies Latest reply: May 21, 2015 3:13 PM by rmt1218
Flint Million Level 2 Level 2 (155 points)

Here is my script:

 

tell application "Pages"

 

          set thisDoc to open "/Users/fmillion/test.pages"

          set docFile to file of thisDoc

          log "The file is " & docFile

 

          export thisDoc to "/Users/fmillion/test2.docx" as Microsoft Word

 

end tell


The script runs, Pages opens and loads the document. However, the Export step fails. Here's the full events output:

 

tell application "Pages"

  open "/Users/fmillion/test.pages"

  --> document "test.pages"

  get file of document "test.pages"

  --> file "Macintosh HD:Users:fmillion:test.pages"

  (*The file is Macintosh HD:Users:fmillion:test.pages*)

  export document "test.pages" to "Macintosh HD:Users:fmillion:test2.docx" as Microsoft Word

  --> error "The document “test.pages” could not be exported as “test2”. You don’t have permission." number 6

Result:

error "Pages got an error: The document “test.pages” could not be exported as “test”. You don’t have permission." number 6

 

No amount of fiddling with parameters will change this. Obviously, I have write permission to my home directory, which just to be absolutely sure I verified. (I do.) I even tried creating a directory with chmod 777 to give it world-writable state, but Pages still wouldn't export there.

 

I feel like I'm missing something - could anyone tell me what it is?

 

Thanks!

 

F


OS X Mavericks (10.9.2)
  • fruhulda Level 6 Level 6 (15,120 points)

    From what I have heard does not Pages 5 have much Applescript. Pages 09 is better suited for it. Which version are you attempting to create an Applescript for?

  • Jeff Shenk Level 4 Level 4 (2,620 points)

    The export command exports to <some file>, but your line

     

     

     

              export thisDoc to "/Users/fmillion/test2.docx" as Microsoft Word

     

     

     

    is trying to export to a string that is the name of a file

     

    try

     

      export document "test.pages" to file "Macintosh HD:Users:fmillion:test2.docx" as Microsoft Word


  • VikingOSX Level 6 Level 6 (10,330 points)

    Incorporating Jeff's advice, here is an AppleScript that outputs a Word document derived from the opened Pages filename with the extension replaced by .docx. No hardcoding of file paths and filenames. This example puts the Word document on the Desktop. One annoyance is that Pages exports with the extension hidden flag enabled. This example was tested in Pages v5.2.

     

    tell application "Pages"

      activate

              set pagesFile to document 1

     

              tell application "Finder"

     

                        set fname to name of pagesFile

                        set delims to AppleScript's text item delimiters

                        set AppleScript's text item delimiters to "."

                        --strip .pages extension from the filename

                        if fname contains "." then set fname to (text items 1 thru -2 of fname) as text

                        set AppleScript's text item delimiters to delims

                        set docxName to fname & "." & "docx"

                        set docxPath to POSIX path of (path to desktop from user domain) & docxName

     

              end tell

      export pagesFile to POSIX file docxPath as Microsoft Word

    end tell

  • jnew9342 Level 1 Level 1 (0 points)

    Hi VikingOSX, I found your post incredibly useful, thank you. I have just one small question.

     

    I was wondering if there is some way to modify your script so that the docx file saves in the same location as the original pages file, rather than on the deskotp?

     

    I have had a bit of a look at the AppleScript documentation but I'm very new to this and couldn't figure out how it would be done.

     

    Thanks in advance for any help.

  • VikingOSX Level 6 Level 6 (10,330 points)

    The following script, unlike the previous post, exports the Word document back to the original location of the Pages document. It also makes the Word document extension visible.

     

    tell application "Pages"

      activate

     

              set pagesFile to document 1

              set pagesPath to file of document 1

              set ppath to POSIX path of pagesPath

              set fpath to do shell script "/usr/bin/dirname " & ppath

     

              tell application "Finder"

     

                        set fname to name of pagesFile

     

                        set delims to AppleScript's text item delimiters

                        set AppleScript's text item delimiters to "."

      --strip .pages extension from the filename

                        if fname contains "." then set fname to (text items 1 thru -2 of fname) as text

                        set AppleScript's text item delimiters to delims

                        set docxName to fname & "." & "docx"

      --set docxPath to POSIX path of (path to desktop from user domain) & docxName

      -- put exported file back in original document location

                        set docxPath to fpath & "/" & docxName

      --display dialog docxPath

     

              end tell

      export pagesFile to POSIX file docxPath as Microsoft Word

    end tell

    tell application "Finder"

              set extension hidden of ((POSIX file docxPath) as alias) to false

    end tell

  • kenhotto Level 1 Level 1 (0 points)

    -- This small Applescript will take a "pages" file and export it to a "pdf" file I struggled a little bit with the "export" function, but got the answer from here - thank you Jeff Shenk and VikingOSX for pointing out that I need the 'file' qualifier before the file variable. I really struggled with that. I hope somebody will find this useful - as I have found many things useful in this forum - and elsewhere. I am using the 'native' file description.

     

    -- I am using Pages 5.5.1, MacOSX 10.10.1 and ScriptEditor 2.7 - Applescript 2.4

     

     

    --Select from where you will pick up the pages files

    set theFolder to choose folder with prompt "Select folder with original pages files :"

    --Do it

    tell application "Finder"

      set theNames to name of files of theFolder ¬

      whose name extension is "pages"

    end tell

     

    --Select where the PDF files will go

    set pdf_Folder to choose folder with prompt "Select folder where PDF files will go :"

     

    -- How many files to export

    set item_count to (get count of items in theNames)

     

    --Get files and export them

    repeat with i from 1 to item_count

     

      set current_file to item i of theNames -- get a file

      set lean_file to text 1 thru -7 of current_file & ".pdf" -- change the originalfile (.pages) to a .pdf name

      set out_file to (pdf_Folder as Unicode text) & (lean_file) -- get the fully qualified output name

      set in_file to (theFolder as Unicode text) & (current_file) -- get the fully qualified input file name

     

      tell application "Pages"

      set mydoc to open file in_file -- open input file in Pages

      export mydoc to file out_file as PDF --do the exporting

      close mydoc saving no -- close the original file without saving

      end tell

     

    end repeat

     

     

    display dialog "done" -- Job done

  • VikingOSX Level 6 Level 6 (10,330 points)

    We all learn from "borrow-ware."

     

    AppleScript tutorials and learning will augment your knowledge too.

  • rmt1218 Level 1 Level 1 (0 points)

    This isn't working for me. I get the error "Can't get document 1." I tinkered around with the script a little, but I'm new to AppleScript so I was unable to resolve the issue. I'm using Pages 5.2 on OS Mavericks.

  • VikingOSX Level 6 Level 6 (10,330 points)

    I am using Pages v5.2.2 on Mavericks 10.9.5. I just opened a Pages v5 document, copied my previously posted script back into the AppleScript Editor, compiled it, and when run without modification, it instantly exported a .docx version of the opened Pages document. No issues with "Can't get document 1."

  • VikingOSX Level 6 Level 6 (10,330 points)

    I decided to update my previously posted AppleScript with a file prompt, error handling, and less clutter. It will ensure that the file extension is always visible, and optionally, you can put a color tag on the exported document. This AppleScript has been tested without modification on both Mavericks 10.9.5, and Yosemite 10.10.3 with Pages v5.2.2 and v5.5.3 respectively.

     

    property pages_type : {"com.apple.iwork.pages.pages", "com.apple.iwork.pages.sffpages"}

    property labelColor : {none:0, orange:1, red:2, yellow:3, blue:4, purple:5, green:6, gray:7}

     

    set afile to ¬

      (choose file with prompt "Choose Pages documents" of type pages_type ¬

      default location (path to desktop folder) ¬

      without invisibles)

     

    set exportDocumentFile to text 1 thru (offset of ".pages" in (afile as text)) of (afile as text) & "docx"

    -- log exportDocumentFile

    try

         tell application "Pages"

              open afile

              delay 2

              with timeout of 1200 seconds

                     export front document to file exportDocumentFile as Microsoft Word

              end timeout

              close front document saving no

         end tell

    on error msg number enbr

         display alert "[ " & enbr & " ] " & msg giving up after 8

         tell application "Pages" to if it is running then quit

    end try

     

    tell application "Finder"

         set extension hidden of file exportDocumentFile to false

         -- set label index of file exportDocumentFile to blue of labelColor

    end tell

  • rmt1218 Level 1 Level 1 (0 points)

    I tried your new script and that didn't work for me either. I get the following Syntax Error: Expected “end” but found “property”. I'm running this in Automator (Version 2.4). I don't know how relevant that is or if that is contributing to my problems in some way.

  • VikingOSX Level 6 Level 6 (10,330 points)

    Syntactically correct AppleScript code that is error/warning free in the AppleScript Editor, or saved from it as an Application — will run and export a Word document from Pages as I have tested and stated.

     

    When you replace the boilerplate text in the Automator Run AppleScript action, with the identical latest AppleScript that I posted above, it gets a bogus error about that second colon in my labelColor property declaration. That is the only error that I receive with Automator on Mavericks. There is nothing to fix.  Just comment out that line, and the script will compile and run as an Automator application should — unless you introduced something additional. It produces the expected Microsoft Word document exported from Pages.

  • rmt1218 Level 1 Level 1 (0 points)

    Here are a couple of screen shots. This is the workflow before adding the script:

    workflow.png

    After taking this screen shot, I inserted your script from June 5 where it says (* Your script goes here *). First I removed the whole line. Then I tried keeping the parentheses, but it didn't like that. I tried curly brackets and it didn't like that either. At the top, I've tried "no input" and "text", and "Pages" and "any application", and all combinations thereof. I've both checked the "Output replaces selected text" box and unchecked it. I've added nothing else other than the script. I have not introduced anything additional.

     

    Here is the log when I run it:

    log.png

    There may be nothing to fix when you run it on your machine, but there clearly is on mine.

  • rmt1218 Level 1 Level 1 (0 points)

    Ok... so deleting the entire boilerplate text worked with the script from two days ago, but it did NOT work with the script from June 5. But I don't think it worked quite as it's supposed to. When running the service, it asked me which document I'd like to open, even though I already had an open document. So that's what seems to be the problem - for some reason it isn't recognizing that the document is open. This only happens with previously created documents though. If I create a new document and export it, that works.

Previous 1 2 Next