Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random Number from 1 to Variable in Applescript

I'm working on a quick program in Applescript to help me study for a test, and I can't seem to get the random number generation right. Here's the program:

repeat
    set page to (random number from 3 to 198) as text
    set dialog1Text to "Page: " & page & "
    How many paragraphs are on this page?"

    display dialog dialog1Text default answer ""
    set userNumParagraphs to text returned of result
    set numParagraphs to (userNumParagraphs) as integer

    set paragraph to (random number from 1 to numParagraphs) as text
    set dialog2Text to "Paragraph: " & paragraph

    display dialog paragraph
end repeat

There's one part that's not working. It's the random number generator, this part:

set paragraph to (random number from 1 to numParagraphs) as text

Applescript gives me this error:

Can’t set paragraph to (random number from 1 to numParagraphs) as text. Access not allowed.

I have done much research on the problem, but I can't find anything. I have looked for ways to generate a random number from a variable to a variable, but to no avail. I was wondering if any of you knew a way to do this. Any help that can be provided would be much appreciated. Thanks in advance.

like image 937
jmindel Avatar asked May 21 '26 22:05

jmindel


1 Answers

The word 'paragraph' is a reserved word in applescript. You can't use it as a variable name. Always a good practice to add a prefix, as you did with others. Change those two lines to:

set theParagraph to (random number from 1 to numParagraphs) as text
set dialog2Text to "Paragraph: " & theParagraph

For cleanliness sake, do the same for 'page'. Change it to 'thePage', etc.

like image 112
jweaks Avatar answered May 25 '26 12:05

jweaks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!