PDA

View Full Version : iWork / Pages with AppleScript




blueskyredkite
Jul 5, 2007, 06:31 AM
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.



psychofreak
Jul 5, 2007, 06:46 AM
Apple is touting Leopard's Automator, so there is a good chance :)

xUKHCx
Jul 5, 2007, 06:58 AM
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

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


Then you can get the text of the document via

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.

blueskyredkite
Jul 6, 2007, 07:39 AM
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



[snip]

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

xUKHCx
Jul 9, 2007, 01:30 PM
[snip]

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

How is the script going, need any more help/advice.

blueskyredkite
Jul 10, 2007, 03:26 PM
How is the script going, need any more help/advice.

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! :)

blueskyredkite
Jul 11, 2007, 07:08 AM
How is the script going, need any more help/advice.

OK, what I've got is:

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:

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:

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?

xUKHCx
Jul 11, 2007, 07:42 AM
OK, what I've got is:

Any suggestions?

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 (http://bbs.applescript.net/viewtopic.php?pid=56766), 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.

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

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.

blueskyredkite
Jul 13, 2007, 05:05 AM
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 (http://bbs.applescript.net/viewtopic.php?pid=56766), Rainyday and Nigel Carvey.


[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.
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.

mapinguari
Jul 21, 2007, 04:03 AM
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.
set var to the text returned of (display dialog "Address" default answer "name" & return & "street" & return & "city, state zip")
might be a little better.

blueskyredkite
Jul 25, 2007, 07:33 AM
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.
[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:

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:
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:
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