Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Save as Word via script

I am trying to write an AppleScript that will save a Pages document as a Word document (with the same file name), a script I can execute with a single keystroke (assigned by FastScripts). I found such a script in an archived 2010 entry in this forum (reproduced below), using the GUI interface, but it does not work. The AppleScript dictionary for Pages has no entry for "sheet", so the script fails. I am using Pages 09 Version 4.0.5.


Another thread mentioned an Automator workflow someone had written to do this, but the link to it gets a "Not found" page error. And I cannot see any Pages actions in Automator. Does anyone know how this can be done?


Here's the script that does not work (this script--if it worked--would save the Word doc with the name "Pages document" every time; I'd want to modify that to use the name of the Pages file):


--BEGINNING OF SCRIPT

set theName to "Pages document.doc"

tell application "Pages" to activate

tell application "System Events" to tell process "Pages"

click menu item "Export…" of menu 1 of menu bar item "File" of menu bar 1

repeat until sheet 1 of window 1 exists

delay 1

end repeat

click radio button "Word" of radio group 1 of sheet 1 of window 1

click button "Next…" of sheet 1 of window 1

keystroke "a" using command down -- Select All (⌘A)

keystroke theName

keystroke "d" using command down -- save to Desktop (⌘D)

keystroke return

end tell

--END OF SCRIPT

Pages 09-OTHER, Mac OS X (10.6.7)

Posted on May 28, 2011 9:46 AM

Reply
5 replies

May 28, 2011 9:55 AM in response to Allen Watson1

A related query: Pages 09 has a script command "save" which looks like this in the dictionary:


save (verb)Save an object. (from Standard Suite)

command syntax save reference ¬ as unicode text ¬ in alias parameters


where:


The first parameter is what is being saved (should this be "document" or "window"?)

The other two, optional, are:


as unicode text The file type in which to save the data.

in alias The file in which to save the object.


This looks as if it might work to save as a Word document. However, I can find no info on what the Unicode text specifying file type should be: "doc", "docx", "Word document"? And if you provide anything here, the syntax then requires specifying the file in which to save the object. If I try putting an alias there, and run the command, it tells me it can't execute because the alias is not found! But I'm trying to CREATE the file, not overwrite an existing file.


Nutshell: HOW DOES THE SAVE COMMAND WORK?

May 28, 2011 11:37 AM in response to Allen Watson1

Applying exactly what is written in the dictionary give this simple piece of code :


--{code}

(*

Grabs the path of the Pages document.

Yes, to save as .doc, the document must be already saved as .pages

*)

tell application "Pages"

set chemin_pages to path of document 1

end tell

(*

Grabs the document's name and the path to the folder where it is stored

*)

tell application "System Events" to tell disk item chemin_pages

set nom_pages to name

set le_dossier to path of container

end tell

(*

builds the name of the .doc file to create

*)

if nom_pages ends with ".pages" then

set nom_doc to text 1 thru -7 of nom_pages & ".doc"

else

set nom_doc to nom_pages & ".doc"

end if

(*

Create the .doc file

*)

tell application "System Events"

if not (exists (disk itemnom_doc of folderle_dossier)) then makenewfileat end of folderle_dossierwith properties {name:nom_doc}

end tell

(*

take care to coerce it as alias

*)

set chemin_doc to (le_dossier & nom_doc) as alias

(*

Now we may save it.

CAUTION, Pages no longer use the old ID "SLDocumentTypeMSWord"

it uses "Microsoft Word 97 - 2004 document"

*)

tell application "Pages"

save document nom_pages as "Microsoft Word 97 - 2004 document" in chemin_doc

end tell

--{code}


Yvan KOENIG (VALLAURIS, France) samedi 28 mai 2011 20:37:38

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.7

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

May 29, 2011 12:57 AM in response to Allen Watson1

I forgot to explain why the first script (GUI scripting) fails.

For us, users, the export dialog is the same when ran under different Operating Systems (10.4, 10.5, 10.6) but its components don't have the same nature.

In mac OS X 10.4.x, the button "Word" is really a button

in mac OS X 10.5.x, it's a check box (may be surprising but it's really that)

in mac OS X 10.6.x, it's a radio button

I'm wondering what it will be under Lion 😉

I wish to add that I didn't trigger Export in the menu File of Pages '09 because in the original application version there was not such item.

Only the Share menu contained this item which was re-introduced in the first revision.

Last not least, using a GUI scripting piece of code as the listed one may be failing if the running system isn't an English one.

As you saw, the items are described by tier name which change with the language in use.

It's safer to trigger them thru their index in the menu bar or in the menu itself.

My piece of code is a quick and dirty one. A test upon the running operating system would be welcome so that the save instruction use the correct format ID.

I will test a slightly modified version in which the format ID isn't explicitly given.

The error message issued when I used the old ID make me think that maybe, giving a pathname pointing to a .doc file may be sufficient.

If I guess well,

tell application "Pages"


savedocumentnom_pagesinchemin_doc

end tell

would be sufficient.


Yvan KOENIG (VALLAURIS, France) dimanche 29 mai 2011 09:57:14

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.7

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

May 29, 2011 1:44 AM in response to KOENIG Yvan

Something bothered me so I made a new test.

It was a good idea. It's not Save As which requires that the document was already saved once, it's Export.

Knowing that, we may use this shorter script :


--{code}

(*

Create an unique fileName

*)

set nom_doc to do shell script "date +_%Y%m%d_%H%M%S.doc"

(*

define the destination folder

*)

set le_dossier to path todocuments folderastext

(*

Create the .doc file

*)

tell application "System Events"


makenewfileat end of folderle_dossierwith properties {name:nom_doc}

end tell

(*

Now we may save it.

*)

tell application "Pages"


savedocument 1 inalias (le_dossier & nom_doc)

end tell

--{code}


Yvan KOENIG (VALLAURIS, France) dimanche 29 mai 2011 10:43:42

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.7

Please : Search for questions similar to your own before submitting them to the community


To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

Save as Word via script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.