Sequential Numbering in TextEdit

We work at a print shop and have to print 80,000 tickets starting with 179,001. Our Design Merge program needs a data file to merge into the variable place holder. I created this AS program that asks for the starting, quantity and step numbers. It then builds a comma delimited Text file for use in Design Merge.

On my G5, it creates 711 numbers/minute. I wonder if it would be faster to create the list in memory, then write the file out. Or create a 1000 numbered list, then multiply each value by the step to create blocks of values, then write the file. Also, I could create 10,000 set, paste it into excel or OpenOffice, put in a formula and extract it out.

tell application "TextEdit"
	activate
	if document 1 exists then beep
	display dialog "What is the starting number? " default answer "" buttons {"OK", "Cancel"} default button 1
	copy the result as list to {StartNum, button_pressed}
	
	display dialog "How many numbers needed? " default answer "" buttons {"OK", "Cancel"} default button 1
	copy the result as list to {TotalNum, button_pressed}
	display dialog "Step by? " default answer "" buttons {"OK", "Cancel"} default button 1
	copy the result as list to {StepNum, button_pressed}
	set StartNum to StartNum as real
	set TotalNum to TotalNum as real
	set StepNum to StepNum as real
	set SequentialList to {}
	-- Build CSV file
	
	repeat with i from StartNum to (StartNum + TotalNum - 1) by StepNum
		
		set SequentialList to SequentialList & (i as text) & "," -- as list
		tell document 1 to set it's text to SequentialList as text
		--set StartNum to StartNum + 
		
	end repeat
end tell

Hi tlotzer,
Is this quicker?

tell application "TextEdit"
	activate
	if document 1 exists then beep
	display dialog "What is the starting number? " default answer "" buttons {"OK", "Cancel"} default button 1
	copy the result as list to {StartNum, button_pressed}
display dialog "How many numbers needed? " default answer "" buttons {"OK", "Cancel"} default button 1
	copy the result as list to {TotalNum, button_pressed}
	display dialog "Step by? " default answer "" buttons {"OK", "Cancel"} default button 1
	copy the result as list to {StepNum, button_pressed}
	set SequentialList to (do shell script "#!/bin/sh
number=" & StartNum & " # start number
while [ $number -lt " & TotalNum & " ] ; do # Total Num needed
k=$k$number,
    let number=$number+" & StepNum & " #steps
done
 echo $k")
	tell document 1 to set it's text to SequentialList as text
end tell

Also you could have it easily write out to a text file, without needing one open.

Much faster. I figured there was another way. Thanks.

BTW, for CSV files, the commas actually need to be replaced with Paragraph Returns. The ASC code 013 I believe.

http://www.jimprice.com/ascii-0-127.gif
http://www.jimprice.com/jim-asc.shtml

do you mean the result should be:

1
2
3
4
5

rather than :
1,2,3,4,5

edit
By taking the main script out of textedits control, These are FASTER, 1 to 80000 numbers in steps of 1 and writing the file in about 36 seconds on G4.

For numbers in sequence:
1
2
3
4
5

display dialog "How many numbers needed? " default answer "" buttons {"OK", "Cancel"} default button 1
copy the result as list to {TotalNum, button_pressed}
display dialog "Step by? " default answer "" buttons {"OK", "Cancel"} default button 1
copy the result as list to {StepNum, button_pressed}

set SequentialList to (do shell script "#!/bin/sh
number=" & StartNum & " # start number
while [ $number -lt " & TotalNum & " ] ; do # Total Num needed
echo $number
    let number=$number+" & StepNum & " #steps
done
")

tell application "TextEdit"
	set d to make new document at beginning of documents
	tell document 1 to set it's text to SequentialList as text
end tell

For numbers in sequence:
1,2,3,4,5


display dialog "What is the starting number? " default answer "" buttons {"OK", "Cancel"} default button 1
copy the result as list to {StartNum, button_pressed}

display dialog "How many numbers needed? " default answer "" buttons {"OK", "Cancel"} default button 1
copy the result as list to {TotalNum, button_pressed}
display dialog "Step by? " default answer "" buttons {"OK", "Cancel"} default button 1
copy the result as list to {StepNum, button_pressed}

set SequentialList to (do shell script "#!/bin/sh
number=" & StartNum & " # start number
while [ $number -lt " & TotalNum & " ] ; do # Total Num needed
echo $number
    let number=$number+" & StepNum & " #steps
done
")

set SequentialList to words of SequentialList
set AppleScript's text item delimiters to {", "}
set SequentialList to SequentialList as string
set AppleScript's text item delimiters to ""
tell application "TextEdit"
	set d to make new document at beginning of documents
	tell document 1 to set it's text to SequentialList as text
end tell

Hi, tlotzer.

Don’t coerce records (whose properties are labelled, not ordered) to lists (whose items are ordered) if you want to be sure of setting your variables correctly to the corresponding values. Either use a record to set the variables:

display dialog "What is the starting number? " default answer "" buttons {"OK", "Cancel"} default button 1
set {text returned:StartNum, button returned:button_pressed} to the result

“ or, since you don’t use the button_pressed value anywhere in the script, just extract the ‘text returned’ value:

display dialog "What is the starting number? " default answer "" buttons {"OK", "Cancel"} default button 1
set StartNum to text returned of the result

Your script would work more quickly if you moved the ‘tell document 1 to set its text to SequentialList as text’ line to after the repeat. That way, the text of the document is only set once “ to the completed number list “ which of course saves time.

In fact, though, you could write the text directly to file without involving TextEdit, which would be even faster still:

display dialog "What is the starting number? " default answer "" buttons {"OK", "Cancel"} default button 1
set StartNum to text returned of the result

display dialog "How many numbers needed? " default answer "" buttons {"OK", "Cancel"} default button 1
set TotalNum to text returned of result

display dialog "Step by? " default answer "" buttons {"OK", "Cancel"} default button 1
set StepNum to text returned of result

script
	property SequentialList : {}
end script
set o to result

-- Build CSV file

repeat with i from StartNum to (StartNum + TotalNum - 1) by StepNum
	set end of o's SequentialList to i
end repeat

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
set o's SequentialList to o's SequentialList as string
set AppleScript's text item delimiters to astid

-- Save the text in a file in the required location.
set destFile to (choose file name with prompt "Save the text file as.?" default name "Number sequence.txt")
set fref to (open for access destFile with write permission)
try
	set eof fref to 0
	write o's SequentialList to fref
end try
close access fref

[i]Edit: Script object added to the script for even more speed (needed if creating 80,000 numbers!) and the delimiter changed to a return in view of tlotzer’s and Mark’s exchanges above.

Apologies for the earlier assertion that ‘choose file name’ is now called something else. I must have been thinking of when it was called something else back in the days of OS 8.6.[/i] :rolleyes:

Yes, that is correct. Weird for a COMMA delimited file, huh.

Did you see my last edit???

Alternatively:

display dialog "What is the starting number?" default answer ""
set startNum to text returned of result

display dialog "How many numbers needed?" default answer ""
set reps to text returned of result

display dialog "Step by?" default answer ""
set stepBy to text returned of result

do shell script "/usr/bin/jot " & ¬
	quoted form of reps & " " & ¬
	quoted form of startNum & " - " & ¬
	quoted form of stepBy
set sequentialList to result

Thanks to mark hunte, Nigel Garvey, and Bruce Phillips for your expert help. This site is absolutely the greatest thing since sliced bread. Your contribution are GOLD.

Mark, I haven’t studied Unix shell scripting, just DOS, Visual Basic, AS, and a little C++. Would you be so kind as to explain all the “do shell script”? I looked up the meaning of #!/bin/sh from http://steve-parker.org/sh/loops.shtml but the rest is French to me. I can see a while loop in there.

Also Mark,
I’m suprised to see below, that to replace the comma with a CR, the null string is swapped out rather than ASC013. Is there a way to build the list within the shell command rather than swapping after the fact. Since I don’t grasp the shell part quoted above, I don’t see where the commas were appending the list anyway.

Whooshhhh!!! :slight_smile:

Bruce

What character is between the quotes?

:lol:

A hyphen.

That hyphen is being used to omit the upper bound. (The upper bound can be used if you don’t have a step size for the data you want, or if you don’t want the values to exceed a certain value.)

"set SequentialList to (do shell script "#!/bin/sh

number=" & StartNum & " # start number
This line Assigns what ever comes after the = to the variable number. This can be a result of a command after = or as in this case a string

while [ $number -lt " & TotalNum & " ] ; do # Total Num needed
This line checks the value of the variable number, to call the veriable you use a $ and the variable name, $number
-lt is an option for the while loop or statement, It stands for is less than.
so what the line is saying is: while the value of the veriable number is less than the TotalNum then do the following.

echo $number
echo will echo any string or value that come after it to the standard output.( It like print this value)
let number=$number+" & StepNum & " #steps
This line puts each result together as a string
done
“)”

For better explanations, do a google on unix while statement.

The script with the delimiters for a horizontal list.
I just remembered that you can use a -n option to not print the new line that is the default.

display dialog "What is the starting number? " default answer "" buttons {"OK", "Cancel"} default button 1
copy the result as list to {StartNum, button_pressed}

display dialog "How many numbers needed? " default answer "" buttons {"OK", "Cancel"} default button 1
copy the result as list to {TotalNum, button_pressed}
display dialog "Step by? " default answer "" buttons {"OK", "Cancel"} default button 1
copy the result as list to {StepNum, button_pressed}

set SequentialList to (do shell script "#!/bin/sh
number=" & StartNum & " # start number
while [ $number -lt " & TotalNum & " ] ; do # Total Num needed
echo -n $number,
    let number=$number+" & StepNum & " #steps
done
")
tell application "TextEdit"
   set d to make new document at beginning of documents
   tell document 1 to set it's text to SequentialList as text
end tell

I did originally do a script that used jot, but I did not get the sequences you seemed to be after with it.
partly I think for lack of understanding it fully.

But you can use the -s option which will add the following string to the end of the number instead of the new line
So in Bruce’s script (again not sure if you wanted this type of steps?)

display dialog "What is the starting number?" default answer ""
set StartNum to text returned of result

display dialog "How many numbers needed?" default answer ""
set reps to text returned of result

display dialog "Step by?" default answer ""
set stepBy to text returned of result

do shell script "/usr/bin/jot -s, " & ¬
	quoted form of reps & " " & ¬
	quoted form of StartNum & " - " & ¬
	quoted form of stepBy
set SequentialList to result

And of course, I have not taken out any of you redundant bits of script that Nigel Garvey mentions, as I was lazy the first time and did not want to waste that non-effort :D, Bad scripting I know… sorry