7 Replies Latest reply: Nov 23, 2014 10:14 AM by VikingOSX
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,610 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 (9,775 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 (9,775 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 (9,775 points)

    We all learn from "borrow-ware."

     

    AppleScript tutorials and learning will augment your knowledge too.