Adding text and images to a Pages document

I’m trying to automate adding text and images to the end of a Pages document. I need to add some text, an image via a POSIX path, more text, and then another image.

What I have so far over writes the text and overlays the images:

tell application "Pages"
	if the front document exists then
		set aDoc to the front document
	else
		set aDoc to make new document
	end if
	activate
	tell aDoc
		tell front page
			set titleOne to "Title One"
			set body text to titleOne
			set image1Path to POSIX file "/path/to/images/image_0.png"
			make new image with properties {file:image1Path}
			
			set titleTwo to "Title Two"
			set body text to titleTwo
			set image2Path to POSIX file "/path/to/images/image_1.png"
			make new image with properties {file:image2Path}
		end tell
	end tell
end tell

Pages’s AppleScript dictionary’s “image” object has “file” property.
“file” require alias path data.

If Pages accepts POSIX path, this should be “text” or “any”.

スクリーンショット 2025-03-27 8.31.18

So, you should convert POSIX path to alias.

set aFile to POSIX path of (choose file) --POSIX path
set bFile to (POSIX file aFile) as alias

tell application "Pages"
	if the front document exists then
		set aDoc to the front document
	else
		set aDoc to make new document
	end if
	
	tell aDoc
		tell front page
			set titleOne to "Title One"
			set body text to titleOne
			
			make new image with properties {file:bFile}
		end tell
	end tell
end tell

I wrote these ebooks about iWork (Keynote, Pages, Numbers) scripting.
It include a lot of basic & high level AppleScript.

https://piyomarusoft.booth.pm/items/4049167

https://piyomarusoft.booth.pm/items/6154207

Piyomaru, thanks so much for your reply.

I have gotten an image file to insert into Pages along with one body text, but my problem is that I need to write two or more of each without over writing or overlaying the previous ones. I have tried using paragraphs but those always return errors.

Any other ideas?

Also, I’d be very interested in your books, but I don’t read Japanese :frowning:

Have a look at this:

tell application "Pages"
   if the front document exists then
      set aDoc to the front document
   else
      set aDoc to make new document
   end if
   activate
   tell aDoc
      tell front page
         set titleOne to "Title One"
         set body text to titleOne
         set image1Path to POSIX file "/Users/edstockly/Desktop/Tally-0.jpg"
         set image1 to make new image with properties {file:image1Path}
         set {image1x, image1y} to position of image1
         set image1Height to height of image1
         set titleTwo to "Title Two"
         set body text to titleTwo
         set image2Path to POSIX file "/Users/edstockly/Desktop/Tally-1.jpg"
         
         set image2 to make new image with properties {file:image2Path}
         set position of image 2 to {image1x, (image1y + image1Height)}
      end tell
   end tell
end tell

When you place an image Pages follows it’s own idea of where to place the image.

The above script get’s the position and height of the first image, and places the second image right below the first. The script below does the same thing, but more efficiently, and adds a space between the images.

tell application "Pages"
   set spacer to 12
    
   if the front document exists then
      set aDoc to the front document
   else
      set aDoc to make new document
   end if
   activate
   tell aDoc
      tell front page
         set titleOne to "Title One"
         set body text to titleOne
         set image1Path to POSIX file "/Users/edstockly/Desktop/Tally-0.jpg"
         set image1 to make new image with properties {file:image1Path}
         set {image1x, image1y} to position of image1
         set image1Height to height of image1
         set titleTwo to "Title Two"
         set body text to titleTwo
         
         set image2Path to POSIX file "/Users/edstockly/Desktop/Tally-1.jpg"
         set image2Position to {image1x, (image1y + image1Height + spacer)}
         
         set image2 to make new image with properties {file:image2Path, position:image2Position}
         
      end tell
   end tell
end tell

Pages has two document mode.

Body text mode: Default mode. Body text exists.

Page layout mode: Like InDesign or Quark Xpress, place box and flow text

Writing and cat multiple text is @estockly way.
Placing multiple text box (page layout mode) can place text boxes to every where you like within page layout.

You can translate Japanese language to English by using Google Chrome’s translation function.