Need a simple applescript

Discussion in 'Mac Programming' started by apple713, Dec 8, 2014.

  1. apple713 macrumors newbie

    Joined:
    Dec 8, 2014
    #1
    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!
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    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...
     
  3. apple713 thread starter macrumors newbie

    Joined:
    Dec 8, 2014
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK

Share This Page