Counting up to 999 from 1

I am trying to figure out how to get a count from 1 to a variable number between 999.
Something like:

property Apples : ""
property hundreds_Apples : "0"
property tens_Apples : "0"
display dialog "How many apples are in the basket?" default answer (Apples as string) buttons {"quit", "OK"} default button 2
copy the result as list to {num_Apples, button_pressed}
set num_Apples to num_Apples as integer
set num to num_Apples
repeat with num_Apples from 1 to num_Apples
set ones_Apples to num_Apples
set num_apple to hundreds_Apples & tens_Apples & ones_Apples
set ones_Apples to num_Apples + 1
end repeat

The ending results could vary like this:

021
008
562

I know I neet a repeat loop to build the number for Hundreds and Tens of Apples before the script continues. It’s all just not coming clear enough.

I’m not really sure what you are trying to do in the script. But I do know how to get a number from 1 to 999.

set i to random number from 1 to 999

I’m also not entirely sure of what you’re trying to do, but I’m pretty sure part of your problem is the line:

repeat with num_Apples from 1 to num_Apples

In a repeat loop like this, AppleScript will automatically assign the loop value to the named variable, so if num_Apples contains the value 10 before the repeat loop, you’re conceptually saying:

repeat with num_Apples from 1 to 10

On the first iteration, num_Apples gets set to 1 (the first value in the loop)

HOWEVER, by the time the second iteration comes around, you’ve set the value of num_Apples to 1, so your repeat loop is now essentially:

repeat with num_Apples from 1 to 1

and so the loop exits

Instead, consider something more along the lines of:

set num_Apples to num_Apples as integer
repeat with this_Apple from 1 to num_Apples
… – do something with this_Apple
end repeat

In this case, ‘this_Apple’ will hold the incrementing value from 1 to num_Apples

property Apples : ""

display dialog "How many apples are in the basket?
" & "Use a number between 1 and 999" default answer (Apples as string) buttons {"quit", "OK"} default button 2
copy the result as list to {num_Apples, button_pressed}
set total_Apples to 0
set num_Apples to num_Apples as integer

repeat until total_Apples is equal to num_Apples
	set total_Apples to total_Apples + 1
	if total_Apples = num_Apples then
		exit repeat
	end if
end repeat

if total_Apples < 10 then
	set total_Apples to "0" & "0" & total_Apples
else if total_Apples >= 100 then
	set total_Apples to total_Apples
else if total_Apples >= 10 then
	set total_Apples to "0" & total_Apples
end if

display dialog "There are " & total_Apples & " in your basket."

To explain why this exercise.
I am needing a sequence number algorithm to build a file name based upon a user input. Eventually the file name will look something like this:
12345678911.01.01.001.ext
12345678911.01.01.002.ext
12345678911.01.01.003.ext
12345678911.01.01.004.ext
12345678911.01.01.005.ext
12345678911.01.01.006.ext
12345678911.01.01.007.ext
12345678911.01.01.008.ext
12345678911.01.01.009.ext
12345678911.01.01.010.ext
12345678911.01.01.011.ext

further down the road, the name will become more complex when the two inner sequence numbers will also change, like so:

12345678911.01.02.001.ext
12345678911.01.02.002.ext
12345678911.01.02.003.ext
12345678911.01.02.004.ext
12345678911.01.02.005.ext
12345678911.01.02.006.ext
12345678911.01.02.007.ext
12345678911.01.02.008.ext
12345678911.01.02.009.ext
12345678911.01.02.010.ext
12345678911.01.02.011.ext
12345678911.01.02.012.ext
etc until
12345678911.05.06.007.ext
12345678911.05.06.008.ext
12345678911.05.06.009.ext
12345678911.05.06.010.ext
12345678911.05.06.011.ext
12345678911.05.06.012.ext
12345678911.05.06.013.ext
with the highest sequence id being
12345678911.99.99.999.ext

Eventually the user will input the kind of apple, represented by the 11 digit number.
How many trucks will be carrying the Apples, represented by the first .01
How many bushels the trucks will be carrying, represented by the second .01
Finally how many Apples will be in each basket in the bushel in the truck, represented by the three digit number at the end.

if there is a better way of determining the sequence id based on beginning variable number I would like to know about it.

Hi :slight_smile:
I am not sure to understood what you seek to do, but here a script allowing to generate series of numbers with the structure which you wish. This script changes the file name of every files in the folder selected with the generated number :

--[Script Serial Numbers]
--©15/03/2003 Fredo d;o)
property theName : "12345678911"
property theExt : ".ext"
property Num1 : "01"
property Num2 : "01"
property Num3 : "000"

on run
	tell application "Finder"
		set theFolder to (choose folder)
		set theFiles to every file of theFolder whose file type is "TEXT"
		repeat with Itm in theFiles
			set Num3 to my SbJob(0, Num3, 3)
			set Num2 to my SbJob(Num3, Num2, 2)
			set Num1 to my SbJob(Num2, Num1, 2)
			set theNameOk to "" & theName & "." & Num1 & "." & Num2 & "." & Num3 & theExt
			set name of Itm to theNameOk
		end repeat
	end tell
end run

on SbJob(NumTest, NumOk, Zero)
	if (NumTest as number) = 0 then ¬
		set NumOk to (NumOk as number) + 1
	return text -Zero thru -1 of ("00" & NumOk)
end SbJob
--[/Script]

And here the code for test the algorythm :

property theName : "12345678911"
property theExt : ".ext"
property Num1 : "01"
property Num2 : "01"
property Num3 : "000"

on run
	set Lst to {}
	repeat with Itm from 1 to 1500 --for exemple
		set Num3 to my SbJob(0, Num3, 3)
		set Num2 to my SbJob(Num3, Num2, 2)
		set Num1 to my SbJob(Num2, Num1, 2)
		set theNameOk to "" & theName & "." & Num1 & "." & Num2 & "." & Num3 & theExt
		set end of Lst to theNameOk
	end repeat
	set text item delimiters of AppleScript to return
	set the clipboard to (Lst as text)
	set text item delimiters of AppleScript to ""
end run

on SbJob(NumTest, NumOk, Zero)
	if (NumTest as number) = 0 then ¬
		set NumOk to (NumOk as number) + 1
	return text -Zero thru -1 of ("00" & NumOk)
end SbJob

For look the result, paste it in the AppleWorks text document.

:rolleyes: