Pages 4.0.3 scripting help.

Arrrrrgh. As a programmer I’m finding Applescripting very frustrating. Here is what I’m trying to do: I want to create a droplet to process text files and save them as Pages documents. As part of the process I want to change the text font and text size and then change the left and right margins. So far I can get Pages to open up the document I drop on the droplet but I can’t manipulate anything. HELP. Below is what I have done so far.

 on open these_items
repeat with i from 1 to item i of these_items
set this_item to item i of these_items
tell application "Pages" to open this_item
end repeat
end open

From what I have read here and other places. I need to create a reference and when I try something like

set these_text to  this_item

the result is that “These_text” is an alias (Which is what I expected since this_item is an alias. ) I can’t do anything with the alias. What am I doing wrong?

Any help is greatly appreciated.

Paul

Hi,

AppleScript is not more or less logical than other programming languages.
An alias is a file reference. Opening a file reference in Pages.app creates a document reference of Pages


on open these_items
	repeat with i from 1 to count these_items
		set this_item to item i of these_items
		tell application "Pages"
			set frontDocument to open this_item
			tell frontDocument
				-- do something
			end tell
		end tell
	end repeat
end open

I recommend to comment out the open handler lines and define these_items as a list of aliases.
Then you get error messages if something goes wrong.

Ok, I changed my script per your suggestion but I’m still having problems. my first thought was this.


set frontdocument to open this_item
tell frontdocument 
set left margin of frontdocument to .5
set right margin of frontdocument to .5
end tell

This is what I was planning on replacing your ‘Do something’ comment in the code’. Obviously I’m missing something fundamental. Now that I have a document reference “frontdocument”? How do I access it? I feel like a complete noobie.

Are there any good debugging tools out there?

as you have already referenced frontDocument with the tell block either use


.
set frontdocument to open this_item
tell frontdocument
	set left margin to 0.5
	set right margin to 0.5
end tell
.

or without the tell block


.
set frontdocument to open this_item
	set left margin of frontdocument to 0.5
	set right margin of frontdocument to 0.5

The best debugging tool is Script Debugger by LateNight Software, it’s not free, but it’s really worth its price

Script Debugger is what I’m using in Demo mode. Ok when I run the script I get this error from Script Debugger.

“Can’t set <> of missing value to 0.5” What’s that mean?

is the code within an tell application “Pages” block?

Yes.

I replaced your – do something with those two lines.

this works on my machine (10.5.8)


on open these_items
	repeat with i from 1 to count these_items
		set this_item to item i of these_items
		tell application "Pages"
			set frontDocument to open this_item
			tell frontDocument
				set left margin to 0.5
				set right margin to 0.5
			end tell
		end tell
	end repeat
end open

I’m running 10.6.1

I checked your code against mine and the only thing different is the “frontDocument” is like this on my machine “frontdocument” and like this on your posts. “frontDocument”.

Could that be the problem?

This is working correctly OMM. 10.6.1 and Pages '09 v 4.0.2.

no, the spelling doesn’t matter.
I’m still on PPC so I can’t test anything on 10.6

I’m on Pages '09 4.0.3

A side question. Do I need to select the text of the document before I change the margins?

I just updated to 4.0.3 and it still works. You do not need to select the text before hand.

Ok, then my next question I have this saved as an Application, should I save it as a “script”, “script bundle”?

Application (Bundle)

Under SL I don’t have Application (Bundle) just application. Is there a difference in SL?

When I do a save as in ScriptDebugger on Snow Leopard, this is what I have as options.

I was flipping back int the Applescript editor not using script debugger.

the difference between Application and Application Bundle in Leopard and earlier is
Application = Carbon
Application Bundle = Cocoa.

I guess Apple has dropped the carbonized version in Snow Leopard