Subroutine newbie help

I’m trying to create a subroutine to create text styles in InDesign. I’ve got a script that creates a document, styles the text, creates pages with content etc.

However, trying to make it more compact and ‘correct’, I thought I’d start using subroutines to do the repetitive stuff, passing the details to the sub using parameters. Here’s a simplified version of my simple subroutine with just a name parameter:

on createParagraphStyle(styleName)
	try
		set myParagraphStyle to paragraph style styleName
	on error
		set myParagraphStyle to make paragraph style with properties {name:styleName}
	end try
end createParagraphStyle

I’ve tried to call this with ‘on createParagraphStyle(“style name here”)’, but cannot place it anywhere in the script without an error. I’ve tried id within tell statements (using ‘of me’), I’ve tried it at the top of the script, and even after the final ‘end’, but I always get an error and it fails to compile. It’s usually an ‘Invalid key form’ error, but also an ‘expected end or end tell but found…’ error pops up.

The sub compiles fine in a separate script, but I can’t place the sub in another script anywhere (I can put the call in without a problem); how can I make it ‘fit’ in a larger script? I’m sure I’m missing something stupidly obvious, but any help would be appreciated.

I’m using Lion, by the way… if that’s relevant.

Call the subroutine without using “on” in the calling statement:

createParagraphStyle(styleName)

When in a ‘tell app’ block you say:

my createParagraphStyle(styleName)

This is to tell the script not to send that message to the app it was talking to, but to itself.

Thanks Alastor.

I’m doing that in the script itself, thus …

createParagraphStyle("Chapter Title") of me

… using the ‘of me,’ because the sub is in a ‘tell’ statement.

That bit compiles fine, it’s just the actual subroutine (shown in my first post) that won’t “ no matter where I put it.

Hi,

paragraph style is a part of Indesign terminology,
so you have to put it in an application tell block.

Hi Stefan.

I added ‘tell application…’ etc. inside the sub, along with an ‘active document’ tell, called the subroutine using a ‘my’ method, and it seems to work fine.

Many thanks Stefan: would never have figured that out by myself. Now I just need to work on passing the multitude of variables for my text styles to my subroutine.