Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jodles

macrumors regular
Original poster
Dec 5, 2008
172
3
I've been at this for some time now, and can't seem to get it work. I'm very new with applescript...

All I want is to create a list in TextEdit with the numbers from 1 to for example 10, each on its own line.

This is what I've done so far, but it doesn't seem like the repeat loop wants to output each iteration?

Here's my code:
Code:
tell application "TextEdit"
	open (choose file)
	tell front document
		tell its text
			repeat with counter from 1 to 10
				set its text to text & return & counter
			end repeat
		end tell
		save
		close
	end tell
end tell

Can anybody see what's wrong? Obviously this must be the most simple thing to do? :confused:

J
 
I'll highlight something that may help. I'm not at my Mac to try anything.

Code:
	tell front document
		tell [B][COLOR="Blue"]its text[/COLOR][/B]
			repeat with counter from 1 to 10
				set [B][COLOR="Blue"]its text[/COLOR][/B] to [B][COLOR="Red"]text[/COLOR][/B] & return & counter
			end repeat
		end tell
 
Code:
tell application "TextEdit"
	open (choose file)
	tell front document
		[COLOR="Red"]tell its text[/COLOR]
			repeat with counter from 1 to 10
				set [COLOR="Blue"]its text[/COLOR] to text & return & counter
			end repeat
		end tell
		save
		close
	end tell
end tell

The red-hilited code means that the subsequent commands are being directed to the object its text. This means that references to objects within that tell block can only refer to objects or types of objects that can be contained by the its text object itself. If you look at TextEdit's scripting dictionary (which you should always do), what types of objects can the document's text object contain?

Now look at the blue-hilited its text. Does the text contain itself? In this case, the answer is "No".

There is a fundamental error here:
A container is not the same as its contents.


There are several possible approaches for fixing this.

One is to tell the text object to make new objects that it can contain, such as paragraphs, and to make them at the location 'end' with data that is the next number. You can explore that on your own, if you wish.

A simpler way is to not tell the text anything, but tell the front document to set its text to something else. The something else is its current text plus the added text. For example:
Code:
tell application "TextEdit"
	tell front document
		repeat with counter from 1 to 10
			set its text to its text & return & counter
		end repeat
	end tell
end tell
This works. I leave it to you to add back the open, save, and close.

There are still more ways to do this that may be faster, or may work better with rich documents (as opposed to plain text documents), or may have other advantages I can't think of right now.
 
Thank you very much for a very informative post, and for explaining instead of just giving the answer! That made perfect sense!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.