Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

apple713

macrumors newbie
Original poster
Dec 8, 2014
2
0
I am in desperate need of a script for law school. hopefully I am not asking a lot.

The applescript needs to search a 'pages' document and find all instances of a word or phrase (ideally prompted at start of script), and return all of the page numbers that word was found on. Ideally the page numbers would be added to the end of the file like an index (i.e., pizza....1, 4, 6, 8). if thats not easy enough, i'd be happy if they were made into a new text file on my desktop and i could add them to the document myself.

Any help is appreciated!
 
Hmmm.

If you're *just* interested in the main body text then its relatively straight forward. Something like the script below should set you off in the right direction (NB. It's a starting point and is untested). It should set the contents of your clipboard to the required text so you can just paste it into you Pages document.


Code:
set thePageNo to 1
set theTextToSearchFor to text returned of (display dialog "Please enter the search text" default answer "")
set theFoundPageNumbers to {}

--search the pages document
tell application "Pages"
	tell front document
		set thePages to every page
		repeat with thePage in thePages
			
			tell thePage
				set theText to body text of item 1
				set theOffset to offset of theTextToSearchFor in theText
				if theOffset > 0 then
					set theFoundPageNumbers to theFoundPageNumbers & thePageNo
				end if
			end tell
			
			set thePageNo to thePageNo + 1
			
		end repeat
	end tell
end tell

--change the list into text
set AppleScript's text item delimiters to ", "
set theResult to theTextToSearchFor & ": " & theFoundPageNumbers as text
set AppleScript's text item delimiters to ""

--set the contents of the clipboard to the text
set the clipboard to theResult

If, however, you need to include the contents of tables, charts etc then it starts to get a bit more complex...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.