AppleScript Library

iWork Pages Library

   This library contains AppleScript handlers for working with iWork's Pages.

All handlers displayed on this page were freely available on the internet or written by myself. Please refer to the copyright section at the end of this page. Feedback appreciated.


AppleScript Editor open download

Properties
myName
Find & Replace
bodyReplace

Properties

property myName

Name that should be used when loading this library.

show source AppleScript-Editor open insert append toc
property myName : "_pages"

Find & Replace

bodyReplace(oldText, Newtext)

Case-sensitive find and replace in body text of Pages' front document.

oldTextstringthe string to search
Newtextstringthe delimiter to use
nothingerror if problems

Written by:ljr

Example:

bodyReplace("Mr.", "Mrs.") 
--> nothing
show source AppleScript Editor open insert append toc
on bodyReplace(oldText, Newtext)
	-- 120 secs replacing 741 items in a 60 page document
	-- loop alone: < 1 sec; select: 93 sec; change: 26 sec
	local oldText, Newtext, ASTID, str, tmp
	local len, len2, len3, pos, i, thisPos, appName
	set ASTID to AppleScript's text item delimiters
	try
		tell application "Pages"
			set str to body text of document 1
			if str does not contain oldText then return
			set AppleScript's text item delimiters to oldText
			considering case
				script k
					property tmp : str's text items
				end script
			end considering
			set AppleScript's text item delimiters to ASTID
			set len to count k's tmp
			set len2 to count oldText
			set len3 to count Newtext
			set dif to len3 - len2
			set pos to 0
			repeat with i from 1 to len - 1
				set thisPos to (count k's tmp's item i)
				set thisPos to thisPos + pos
				set pos to thisPos + len2 + dif
				tell body text of document 1 to ¬
					select (text from character (thisPos + 1) to ¬
						character (thisPos + len2))
				set selection to Newtext
			end repeat
		end tell
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't pagesReplace: " & eMsg number eNum
	end try
end bodyReplace

Copyright

© 2008 ljr (http://applescript.bratis-lover.net)
bodyReplace

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.