iWork / Pages with AppleScript

Discussion in 'Mac Programming' started by blueskyredkite, Jul 5, 2007.

  1. macrumors member

    blueskyredkite

    Joined:
    Nov 19, 2004
    Location:
    By the sea.
    #1
    I'm a bit p****d off that Pages doesn't work with Automator. I've no experience with AppleScript but I believe it's the only way to achieve what I want in Pages. Having migrated from that MS platform and Word I'm trying to recreate what I had there.

    In Word, you can create a template with fields that, when the document is opened, request data for those fields. This way you can have standard documents and drop in the unique info without having to go through the whole document.

    Ideally I'd like AppleScript to pop up a dialogue box requesting "our ref", "account number", "credit limit", "terms", and "discount", and then drop these into the correct place in the document.

    I've no idea how to go about this, looking at what can be done in Pages I thought it might be possible to automate a search and replace. To this end I've replaced the Windows fields with strings that won't appear in the document ([ourRef], [accNo], [cLimit], etc). I thought I may be able to get AppleScript to popup the search and replace dialogue, with, for example [ourRef] as the search string and leave the user to input the correct string, once that is done pop up the search / replace dialogue again with the next string ([accNo]).

    Any suggestions or ideas?

    Anyone know what the chance is that iWork '07 (if we ever see it) will have an Automator friendly Pages? It's a real PITA that Keynote is Automator frienly but Pages isn't, seems to me a real foot shooting move by Apple.

    Anyway, thanks for your replies.
     
  2. Retired

    psychofreak

    Joined:
    May 16, 2006
    Location:
    London
    #2
    Apple is touting Leopard's Automator, so there is a good chance :)
     
  3. Administrator emeritus

    xUKHCx

    Joined:
    Jan 15, 2006
    Location:
    The Kop
    #3
    I'm sure you could do this

    you just need to get some text parameters from your dialog boxes

    this can be done by a series of

    Code:
    set ourrefreplacemnt to the text returned of (display dialog "Enter text" default answer "[ourRef] ")

    Then you can get the text of the document via

    Code:
    tell application "Pages"
    	tell document 1
    		set john to body text
    	end tell
    end tell
    Then you could setup the script so it replaces your strings, this would be done inside the applescript program. Then once you have compiled the text inside applescript you can simply replace the body text again or create a new document.

    I would help more but don't have the time right now to.
     
  4. thread starter macrumors member

    blueskyredkite

    Joined:
    Nov 19, 2004
    Location:
    By the sea.
    #4
    [snip]

    Thanks for that, it's something to get me started. I'll see where I can get to from there.
     
  5. Administrator emeritus

    xUKHCx

    Joined:
    Jan 15, 2006
    Location:
    The Kop
    #5
    How is the script going, need any more help/advice.
     
  6. thread starter macrumors member

    blueskyredkite

    Joined:
    Nov 19, 2004
    Location:
    By the sea.
    #6
    Work keeps getting in the way. I expect to have time to look at it tomorrow. Thanks for your interest.

    If I'm lucky enough to get it done I'll post... if not, I'll be posting for more help! :)
     
  7. thread starter macrumors member

    blueskyredkite

    Joined:
    Nov 19, 2004
    Location:
    By the sea.
    #7
    OK, what I've got is:

    Code:
    set [somevarname] to the text returned of (display dialogue "Some text")
    several times to get the vars into the script. Then I get the body of the Pages document with:

    Code:
    tell application "Pages"
        tell document 1
            set BodyText to body text
        end tell
    end tell
    
    Now I attempt the search and replace. I'm using perl because I have some experience with it (I realise I could use sed, but I don't know it - overhead is hardly critical for this). What I have is:

    Code:
    do shell script "perl -e 's/zz1zz/" & OurRef & /g' " & BodyText
    zz1zz is a place-holder, OurRef is a var from one of the 'set' lines.

    This appears to run with no errors. The results pane in ScriptEditor just shows, however, "" (two double quotes), and there is no change to the text in Pages. I guesss I'm going to have to write-out BodyText at some point, but I'm really not sure that even my search/replace code is doing what it should.

    Any suggestions?
     
  8. Administrator emeritus

    xUKHCx

    Joined:
    Jan 15, 2006
    Location:
    The Kop
    #8
    Well i don't know perl so can't tell you what is wrong with the above however i think i have a working solution.

    Can't really take credit for the search and replace parts as i went and found them over here, Rainyday and Nigel Carvey.

    Obviously adapt the references section to your needs and add repeats or whatever is necessary. Hopefully you can adapt this into a working solution.

    Code:
    global editedText
    set tp to the text returned of (display dialog "Enter text" default answer "[ourRef] ")
    if tp is "test" then
    	set ourref to "Dogs"
    end if
    tell application "Pages"
    	tell document 1
    		set BodyText to body text
    	end tell
    end tell
    
    my findAndReplace2(tp, ourref, BodyText)
    
    tell application "Pages"
    	tell document 1
    		set body text to editedText
    	end tell
    end tell
    
    on findAndReplace2(toFind, toReplace, theText)
    	set astid to AppleScript's text item delimiters
    	set AppleScript's text item delimiters to toFind
    	set textItems to theText's text items
    	set AppleScript's text item delimiters to toReplace
    	tell textItems to set editedText to beginning & toReplace & rest
    	set AppleScript's text item delimiters to astid
    	return editedText
    end findAndReplace2
    or if you want to use perl then there is this method

    Code:
    global editedText
    set tp to the text returned of (display dialog "Enter text" default answer "[ourRef] ")
    if tp is "test" then
    	set ourref to "Dogs"
    end if
    tell application "Pages"
    	tell document 1
    		set BodyText to body text
    	end tell
    end tell
    
    my findAndReplace(tp, ourref, BodyText)
    
    on findAndReplace(toFind, toReplace, theString)
    	set editedText to do shell script "function sq { echo ${1} | perl -pe \"s~/~\"'\\\\\\/'\"~g;\"; }; echo " & quoted form of theString & " | perl -pe \"s/`sq " & quoted form of toFind & "`/`sq " & quoted form of toReplace & "`/g;\""
    end findAndReplace
    
    
    tell application "Pages"
    	tell document 1
    		set body text to editedText
    	end tell
    end tell
    
    Both of these methods are slightly ugly as they require the pages document to be open, perhaps with a little more work you could adjust it so it does it all without the document having to be open, i.e read the contents using a do shell script or something like that but really depends on how you would prefer to do it.
     
  9. thread starter macrumors member

    blueskyredkite

    Joined:
    Nov 19, 2004
    Location:
    By the sea.
    #9
    [snip]

    Thank you for that. I'll check out the 'original' a little later.

    The script now works. But another problem has come to light.
    Code:
    set var to the text returned of (display dialog "Address")
    can be only a single line. There must be a way to get multi-line dialogue / text-input boxes.

    From what I've found Googling this I keep finding references to AppleScript Studio, but Spotlight shows a folder full of projects on my local machine, and I've no idea where to go from there.

    The reason I need a multi-line dialogue, is that I need to put in an address. Maybe this is a problem for another thread?

    Thank you very much for the help given.
     
  10. macrumors newbie

    Joined:
    Jul 21, 2007
    #10
    Multiline dialog text

    Code:
    set var to the text returned of (display dialog "Address" default answer return & return)
    will give you a 3-line input field. Looks a little odd, though.
    Code:
    set var to the text returned of (display dialog "Address" default answer "name" & return & "street" & return & "city, state zip")
    might be a little better.
     
  11. thread starter macrumors member

    blueskyredkite

    Joined:
    Nov 19, 2004
    Location:
    By the sea.
    #11
    [snip]
    Not too odd that I'm bothered by it :)

    Thank you for your help with that.

    For anyone else trying to script pages, I've done the following:

    Code:
    tell application "Pages"
    	make new document with properties {template name:"[insert template name here [1]]"}
    	tell body text of document 1
    		set paragraph 7 to Var1 & return
    		set paragraph 11 to "Dear " & Contact & return
    		set paragraph 1 to OurRef & return
    		set paragraph 4 to Contact & return
    		set word 24 of paragraph 15 to Var2
    		set word 38 of paragraph 15 to "Some text" & Var3
    		set paragraph 5 to CusAddr & return & return
    		set word -1 to Contact
    	end tell
    end tell
    The last "set..." line:
    "set word -1 to Contact", that -1 is the last word in the document, IIUC, -2 would be second to last word, I've not checked this.

    The variables were grabbed with:
    Code:
    set var1 to the text returned of (display dialog "[some hint text]" default answer "Boris")
    ... just as has been suggested above.

    You can also use syntax like:
    Code:
    set paragraphs 7 through 11 to ...
    which I thought might be useful, but wasn't in the end.

    I found the examples on this page very useful:
    http://lists.apple.com/archives/applescript-users/2006/Jan/msg00363.html

    Thank you to all who helped.

    [1] path not needed if it's a default one or if it's been saved to the default template location
     

Share This Page