How do I add leading zeroes to a number?

For example, you are renaming files, and you would like better “0001” instead of “1”, so the visual look is fine when you browse the folder. Then, you can use this old trick:

set theNumber to 1

text -4 thru -1 of ("0000" & theNumber) --> "0001"

Basically, you add some leading zeroes to theNumber (which could be 1 or 7895), you are converting the “thing to manipulate” to string, then extracting the last four characters (this would return, of course, “7895” if you input 7895, and “0127” if you input 127).