Pages Automation with AppleScript or Scripting Language

In pages 10 and mac OS 10, it appears that Apple has abandoned MacAutomation and scripting of Pages. In learning AppleScript by Sanderson and Rosenthal, many of the scripts fail with esoteric errors with no way to resolve the errors. And there seems to be little or no documentation for using AppleScript for pages version 10 to automate simple tasks.


Bottom line is -- For our agency, don't develop in or buy Apple Products. I hate to see this. I really enjoy my MAC.


I'm asking the community to:

    • Help me mute this decision by suggesting a scriptable word processor, that runs on a Mac, used to develop ebooks and manuals for our users.
    • Point me to documentation, books, etc on the subject -- MacAutomation for pages or other word processors.


Regards


Daryl

iMac 27″, macOS 10.15

Posted on Jun 7, 2020 6:14 AM

Reply

Similar questions

10 replies

Jun 12, 2020 9:19 AM in response to VikingOSX

@ddturk: “In learning AppleScript by Sanderson and Rosenthal, many of the scripts fail with esoteric errors with no way to resolve the errors.”


Sorry to hear that. (I was the lead author on Learn AppleScript, 3rd edition.) Unfortunately, Apple dropped Mac iWork’s original AppleScript support back in 2013 when they merged it with iOS iWork. They later reinstated it, but there’s probably differences between the two implementations which may explain why scripts written for iWork 2010 don’t work on newer versions. Unfortunately, there was insufficient market demand to justify a 4th edition of the book, so we never got the chance to update that chapter (the Entourage material is also obsolete).


If there’s a specific script that you need to get working and Page’s current dictionary isn’t helping, post it here and perhaps other users can help you update it.


@VikingOSX: “The link to the AppleScript Language Guide reflects exactly what its revision history shows — that it was last updated in Jan 2016.”


That’s correct. Apple fired Soghoian and disbanded the Mac Automation team back in 2016, not long after their (half-baked) JavaScript for Automation crashed and sank. Since then the whole AppleScript stack has been in maintenance mode (i.e. essential security fixes only). That said, AppleScript itself hasn’t changed much since 2010: the only significant changes were the addition of native library support (though oddly no libraries), tighter ObjC/Cocoa integration, and the elimination of 3rd-party scripting additions, so the book’s explanation of the language itself still holds (E&OE).


Anyway, we’re only a couple weeks away from WWDC 2020 now, so it may be worth waiting to see what that brings. I expect Siri Shortcuts will be announced for macOS 10.16, at which point we should know if AppleScript will play any part in its future, or if it’s now part of the past.

Jun 10, 2020 5:24 PM in response to ddturk

You may want to consider the following up-to-date guides.


Apple documentation of AppleScript as of June 2020 with all the changes relating to the stages of macOS development up to Catalina.

https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html


The Mac automation site by Sal Soghoian containing more streamlined and easy-to-follow explanations of scripting Pages and other iWork applications in their current form.


https://iworkautomation.com/pages/index.html

Jun 12, 2020 11:48 AM in response to scrutinizer82

“Are you really Hamish Sanderson”


Yes. More often on the developer forums, but occasionally look in here too. Great to hear you still find our book helpful.


I agree Shortcuts isn’t really a replacement for AppleScript, but I can understand Apple wanting to try something new after the old Mac Automation team’s product failures. Obviously the key to mass success lies with Siri: if it can find and run actions upon voice request and automatically assemble them into reusable workflows for you, that can reach a hundred million Apple users. No coding, no point-n-click, no taps-n-swipes; just say what you want and your device does it for you. It’s still got a long way to go, but at least it’s a direction—something frustratingly lacking from Mac Automation over the last decade.


In an ideal world Apple would provide a modern cross-platform successor to AppleScript as well. There’s a huge existing wealth of scriptable apps already available, and Apple event IPC (which is what AppleScript uses) provides far more capable, fine-grained control over applications than the App Extensions used by Shortcuts. A “SiriScript” language would enable amateur scripters [1] to create their own actions, filling the middle space between what end-users can do via Siri Shortcuts and what professional programmers can build in Swift and Xcode.


But whether Apple think that middle space is important or not… well, like I say, we’ll hopefully have a better idea after WWDC. Till then, fingers crossed and keep rocking the old-school Automation. :)


--


[1] Anyone interested in promoting End-User Programming should read Papert’s Mindstorms and Nardi’s A Small Matter of Programming. 70 years of modern computing and the grand challenge of making it truly accessible to all is still barely scratched.

Jun 23, 2020 9:12 AM in response to jtqr

tell application "Pages"


tell (document 1's body text) -- which is a "rich text" class that contains paragraphs according to a Pages 8.x hierarchy.


tell paragraph 1


set font to "TimesNewRomanPS-BoldMT" -- instead of "font name", that's legacy and deprecated. As the dictionary entry for the "font" property says you should consult the FontBook utility for the exact name - either PostScript or displayed one - of the font. In this example I used the PostScript name.


set size to 45 -- instead of "font size". That's legacy and deprecated.


(*


set bold to true -- that's legacy and deprecated.




          set shadow to true -- that's legacy and absent from the current Pages dictionary. Only if you use the "text item" class which is described as "a text container" (e.g., a text box) and contained by the class "document" then you can set such properties as "background fill type" (no fill/‌color fill/‌gradient fill/‌advanced gradient fill/‌image fill/‌advanced image fill, r/o), "object text", "opacity", "reflection showing", "reflection value", "rotation". Try to put your text inside the text box to use said properties.


          set alignment to center -- the "alignment" property is only for cells in a table.


*)


end tell


save document 1 in POSIX file "/Users/Shared/Examples.pages"


end tell


end tell



Jun 11, 2020 5:41 AM in response to scrutinizer82

The link to the AppleScript Language Guide reflects exactly what its revision history shows — that it was last updated in Jan 2016. That is a far cry from showing changes relating to stages of macOS development through Catalina.


Apple is not bound to update the preceding guide, should they have made changes to AppleScript since 2016.


Most corporations have an MS Office suite document standard requirement, and Microsoft continues to provide AppleScript support for their applications.

Jun 23, 2020 8:55 AM in response to jtqr

You simply cannot force code that was written for Pages '08/'09 over a decade ago to supercede the currently supported Scripting Dictionary properties and syntax now required for Pages 10. If you want bold text, then use the bold font name. There is no shadow property for paragraph text, and paragraph does not accept alignment directives now. Even the save syntax is now different.

Jun 23, 2020 10:24 AM in response to jtqr

jtqr wrote:

I suspected that this is the case. However, I got uncertain, because I could not believe that 10 years ago AppleScript for Pages was more advanced than now.

I am trying to format some large texts and scripting the process would save me a lot of time. If it were possible to assign styles to paragraphs through scripting, that would be sufficient for me. However, that seems to be not possible either.

That's why people keep several versions of macOS. I still use iWork'09 in older ones just to get the work done the way I want it to. Some AppleScript changes are more limiting than they used to, especially with regard to formatting - more streamlined and easy. Things started getting different with the release of Mavericks and the new iWork where the class "iWork object" was introduced, and not before Apple re-added AppleScript to the apps again after the stripping them of it initially resulted in a loud outcry from the users.

Jun 12, 2020 9:48 AM in response to hhas

Are you really Hamish Sanderson (forgive me if I'm confusing you with your co-auther Hanaan Rosenthal: I forgot who the nickname "hhas" of you two belongs to)? That's a great surprise to meet you on this forum. I still use your 2010 edition and find the benefits from it worth its content. I even wrote you a letter 4 years ago and you found time to reply clearing up some details for me. Actually, when you follow what that book says closely, try it out, experiment, then you develop a kind of awareness allowing you intuitively achieve things regardless of what new things were added to AppleScript language. The mechanics remains the same save a few commands and classes updated here and there. And even if there were more significant changes the logic stays the same. I think that even if they make Shortcuts available for macOS it will take a long way to come close to the capacity of AppleScript. iOS Shortcuts is still in its infancy and very limiting which is so annoying I stopped using it. With that being said, wouldn't even 2016 revision suffice for most of users? I'm inclined to think it would.


The OP could post his script so others could look it at and make their suggestions, though.

Jun 23, 2020 9:51 AM in response to scrutinizer82

VikingOSX, scrutinizer82, thank You very much for Your quick reply!


@VikingOSX: "You simply cannot force code that was written for Pages '08/'09 over a decade ago to supercede the currently supported Scripting Dictionary ..."


I suspected that this is the case. However, I got uncertain, because I could not believe that 10 years ago AppleScript for Pages was more advanced than now.


I am trying to format some large texts and scripting the process would save me a lot of time. If it were possible to assign styles to paragraphs through scripting, that would be sufficient for me. However, that seems to be not possible either.


Thanks again!





Jun 23, 2020 8:39 AM in response to hhas

@hhas: "If there’s a specific script that you need to get working and Page’s current dictionary isn’t helping, post it here and perhaps other users can help you update it."


Taking up on Your offer, here is an example from Your book: Script 24–2. 


tell application "Pages"

  tell paragraph 1 of document 1

          set font size to 45

          set font name to "Times New Roman"

          set bold to true

          set shadow to true

          set alignment to center

    end tell 

    save document 1 in POSIX file “/Users/Shared/Examples.pages"

end tell


According to Pages' current AppleScript library, a paragraph can have only 3 properties: color, font and size. I am sure, there are more properties, but do not know how to access them.


Thanks for Your help!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Pages Automation with AppleScript or Scripting Language

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