Textedit query

G’day. I have a whole heap of numbers in a textedit document, is there any way to use Applescript to do it for me? Basically, I want any number under 150 to be rounded to the nearest ten, then halved. Otherwise I’ll have to do all 36-odd pages myself =(

You can adaptate this if needed:

--> assume there is a single number in every paragraph
tell application "TextEdit"
	set x to paragraphs of document 1
end tell

repeat with i from 1 to count x
	set theNum to x's item i as number
	if theNum < 150 then --> process it
		set theNum to (round (theNum / 10)) * 10 --> round to nearest ten
		set theNum to theNum / 2 as integer --> half?
	end if
	set x's item i to theNum
end repeat

--> coerce list of numbers to text again
set prevDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set x to x as text
set AppleScript's text item delimiters to prevDelim

tell application "TextEdit"
	set text of document 1 to x
end tell

I’m changing level maxes for a MUD I play on (yay, nerdiness!). The set-up is like

Jawa Statistics

Combatant | 150 | 92 | 110 | 1 | 42 + | 98 | 36 | 0 | 76 |
Pilot | 120 | 150 | 150 | 1 | 82 + | 118 | 76 | 0 | 40 |
Engineer | 120 | 117 | 150 | 1 | 92 + | 98 | 86 | 0 | 86 |
Bounty Hunter | 130 | 117 | 110 | 120 | 42 + | 98 | 36 | 0 | 36 |
Smuggler | 120 | 142 | 110 | 1 | 150+ | 98 | 96 | 0 | 56 |
Politician | 120 | 92 | 110 | 1 | 42 + | 150 | 36 | 0 | 36 |
Slicer | 120 | 92 | 150 | 40 | 92 + | 98 | 136 | 0 | 36 |
Physician | 120 | 96 | 140 | 1 | 62 + | 118 | 36 | 0 | 146 |

4 to a page, for about 36 pages. Should I just count the number of numbers per paragraph, and then set that to x?

This is not very fast, but most probably faster than doing it by hand:
UNTESTED CODE

tell application "TextEdit"
	repeat with i from 1 to 99999999 --> process only first 99999999 words
		try
			set theNum to (word i of document 1) as number
			if theNum < 150 then --> process it 
				set theNum to (round (theNum / 10)) * 10 --> round to nearest ten 
				set theNum to theNum / 2 as integer --> half? 
				set word i of document 1 to (theNum as text)
			end if
		on error number n
			-- if n is -1700 ("can't make whatever into a number"), keep processing
			-- otherwise, there are no more words in this doc, so exit loop
			if n is not -1700 then exit repeat
		end try
	end repeat
end tell

This maybe a bit faster:

property illegalCoercions : {"", return, return & return, (ASCII character 10), (ASCII character 10) & (ASCII character 10), "+", "-"}

tell application "TextEdit" to ¬
	set theText to text of document 1

set prevTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "| "
set theText to theText's text items
set AppleScript's text item delimiters to prevTID

repeat with i from 1 to count theText
	set textChunk to theText's item i
	set AppleScript's text item delimiters to space
	set textChunkItems to textChunk's text items
	set AppleScript's text item delimiters to prevTID
	
	repeat with x from 1 to count textChunkItems
		try
			set textChunkItems's item x to my processNum(textChunkItems's item x) --> on error, isNaN 
		end try
	end repeat
	
	set AppleScript's text item delimiters to space
	set textChunk to textChunkItems as text
	set AppleScript's text item delimiters to prevTID
	
	set theText's item i to textChunk
end repeat

set AppleScript's text item delimiters to "| "
set theText to theText as text
set AppleScript's text item delimiters to prevTID

tell application "TextEdit" to ¬
	set text of document 1 to theText

to processNum(x)
	if x is in illegalCoercions then error --> illegalCoercions will coerce to number as "0", undesired behaviour 
	set x to x as number --> if error, it is not a number, man! 
	if x < 150 then --> process it 
		set x to (round (x / 10)) * 10 --> round to nearest ten 
		set x to x / 2 as integer --> half? 
	end if
	return x
end processNum

Eg:

… To:

NOTE that this code won’t handle some possible numbers, such as “150+” (line “Smuggler”), since they are not strictly numbers.

Worked an absolute treat guys, thanks. Just one more thing, can I get a script that’ll add a random number between one and ten to all numbers under 140?

This is how you get a random number from 1 to 10:

random number from 1 to 10

And this is the sum sign, which you can use to sum numbers: +

I’ve almost got it with

but I can’t get it to ignore zeroes. Any ideas?

Never mind, just worked it out =) Applescript rocks.