Applescript - Read text in Pages document into text string as plain text

Hello all,


I am new at this and I greatly appreciate your guidance.


I want to:


  1. Ask the user to choose a Pages document
  2. Then read the text in it into a text string in Applescript


I want to read it in as plain text - that is, no bold etc, but maintaining newlines and tabs and things like quotations, commas etc. There would be no pictures, no tables etc in the Pages document, and it is unlikely to be longer than 2 pages - we can set a limit if needed.


Posted on Jul 23, 2023 8:38 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 24, 2023 6:22 AM

This AppleScript solves your questions. It restricts the captured plain text up to the first two pages of document content and stores that text in a text variable. It then displays that text in a dialog from which it can be copy/pasted elsewhere if necessary. The following was tested in macOS Ventura 13.4.1 (c).


use scripting additions

-- place up to first two pages of text in this variable
set textvar to ""

tell application "Pages"
	activate
	open (choose file of type "Pages")
	tell front document
		repeat with i from 1 to (its page count)
			if i ≤ 2 then
				set textvar to textvar & body text of page i
			else
				exit repeat
			end if
		end repeat
	end tell
end tell
tell application "System Events" to display dialog textvar
return


Here is the first two pages of "The Invisible Man". There may be text wrap occurring in this limited AppleScript dialog.


6 replies
Question marked as Top-ranking reply

Jul 24, 2023 6:22 AM in response to MstrBanana

This AppleScript solves your questions. It restricts the captured plain text up to the first two pages of document content and stores that text in a text variable. It then displays that text in a dialog from which it can be copy/pasted elsewhere if necessary. The following was tested in macOS Ventura 13.4.1 (c).


use scripting additions

-- place up to first two pages of text in this variable
set textvar to ""

tell application "Pages"
	activate
	open (choose file of type "Pages")
	tell front document
		repeat with i from 1 to (its page count)
			if i ≤ 2 then
				set textvar to textvar & body text of page i
			else
				exit repeat
			end if
		end repeat
	end tell
end tell
tell application "System Events" to display dialog textvar
return


Here is the first two pages of "The Invisible Man". There may be text wrap occurring in this limited AppleScript dialog.


Jul 24, 2023 6:58 PM in response to VikingOSX

Thank you again, Viking OS X. Your code worked perfectly. But for my larger purpose, there is a "bug". Please allow me to first explain what I am trying to do:


I am a teacher at a public school. I communicate with caregivers (parents, grandparents) of my students via text messages. One of them may tell me "XYZ will be out today. Dentist appointment." Or I may say "XYZ did great to day, he ...". But once a week, I give a general update to every caregiver. I keep it personal - address them by their name, address their child by name and proper gender etc. Also, many of my parents do not speak or read English, and so I first visit a website like google translate to translate into their preferred language.


My motivation: A lot of bad behavior by students goes away, and motivating them to do their homework and study harder becomes easier when caregivers and teachers simply communicate and coordinate more. Non-English speakers are so relieved I communicate in their language - because it finally allows them to be aware of and participate in a key part of their child's development.


It is not sustainable though, because it takes way too much time to send individual messages to each caregiver of each class.


For it to be sustainable, I am automating things. Hence this effort. Please see "Additional Text" for the script. It works just fine - and you can see the part where you helped me. It addresses key hurdles - save time, overcome the language barrier, yet keep it personal.


If it were just me who were going to use it, I am done. However, several other teachers and administrators in my district and in other school districts want to use it, but I don't want them to be editing anything in the script to use it. Worst case I will give them the script as is and tell them to be careful, but I'd like to make it nicer.


Here are the challenges that perhaps you can guide me:


  1. Currently, I am composing the message to send inside the script. You will notice that I mix regular text and variables so that the message is personal. I want them to compose in an app like pages. I used your code, but then it does not recognize that it contains variables. Also, I want them to choose the pages document just once - before that repeat loop - and then just update the messageToSend with the personalized message to send to each caregiver. I tried to by-pass the choose and open with my own code, but it does not work, so I commented it out.
  2. The Caregiver's contact info, and student info is in a numbers spreadsheet. Currently, I have the filename hardwired into the code. I tried to do a choose, but then I was getting errors in referencing the document. Each class has its own set of caregivers, so hardwiring the name means the teachers/administrators have to manually manage which file gets called "Contacts.numbers". Ideally, I don't want to hard wire the name of the sheet or table either - there will be only one sheet in the spreadsheet and one table (can there even be more than one table).
  3. As an aside, how do I reference the folder that the script is located in? Path to me gives me the script's filename as well, and I want just path to the folder. Path to container of me and other variations I tried gives me errors.
  4. I want to send an iMessage to caregivers who have an iPhone, and SMS to others. Because, if I send to all via SMS, then for those with an iPhone, messages that I manually send and messages that I send via this script are treated as different senders even when the sending phone number is the same. Besides the confusion, it splits apart the communication history under different senders. Ideally, the Messages application would take care of choosing iMessage to SMS - because, how do I know whether the recipient has an iPhone or not? I came up with the code in the script, but I am told it will fail to send to non-iPhone users because the on error part never gets executed. Everyone I am close enough to test with uses iPhone, and I don't want to test this with the teachers - some of whom do use Android.


Other notes:

  • I could not upload contacts.numbers, so here is a screenshot with the mobile numbers and email addresses changed.
  • So you can execute the code, including translation, I have kept the actual key in the translate API call. I will change it when we are done.
  • Mr. Banana is what my students call me.


Thank you in advance. I opened up to you because I can tell from the detail in your code that you are fully vested in helping people, and for that, I am grateful.





See "Additional Text" for Actual Script


[Image Edited by Moderator to Remove Personal Information]


Jul 24, 2023 6:58 PM in response to MstrBanana

Never post privacy data on these public communities. I have asked the Apple hosts to remove that posted image of the numbers Contacts for that reason.


This is a pretty big ask for my available time. There are also a lot of contingencies about what devices and applications others are using to make this a generic solution across the faculty.


Items 1 & 2: Recent versions of Pages (v12.1 and later) support Mail Merge so you can tie fields in a Numbers spreadsheet to placeholder text fields in the Pages document. The Mail merge will replace designated placeholder text in a template with your desired spreadsheet data. This would require a language specific template for each parent's native language. The weak link in this approach is other faculty will need Pages v12.1 or later, and what if they only use MS Word?


See: Add, change, or delete a merge field in Pages on Mac - Apple Support


If you can't use Mail merge, then this will be better served and supported by someone in a local Mac Users group that knows how to code in AppleScript and commits to supporting you as your requirements evolve.


item 3:


use scripting additions

tell application "Finder" to set parentFolder to (name of container of (path to me)) as text


Item 4:


How to send SMS messages from Mac to Android users as otherwise, the macOS Messages app can only send text messages to other Apple product users.


The code to simulate a mail merge between Pages and Numbers using AppleScript would be formidable and time consuming to produce. It won't be me.




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.

Applescript - Read text in Pages document into text string as plain text

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