Benches macOS

6

AppleScript 101 - How do I make random va…

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

How do I create random values in AppleScript? For instance, if I wanted to create a delay of 40-50 seconds which random selects a number in between there?

Also, how would I do for it text? Like if I wanted my script to type out either "blue" or "red"

example: Set textToType to "The bard is wearing a {blue or red} tunic"

6 replies

Sign in or join Beachball Help to reply.

  1. 0

    Niel

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    For a number:

    random number from 40 to 50

    For text:

    some item of {"blue", "red"}

    (51594)

  2. 0

    eGeekee

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Whoa, extremely fast response, thank you very much.

  3. 0

    eGeekee

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Having difficulties implementing the code

  4. 0

    eGeekee

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    okay, so I tried to test out the code and it doesn't seem to be working. This is my code:

    tell application "TextEdit"

    launch

    repeat

    delay (random number from 2 - 8)

    some item of {"hello", "goodbye"}

    end repeat

    end tell

    This doesn't seem to be doing anything at all, I also can't seem to put the "some item of {"hello", "goodbye"}" into a set textToType/keystroke command.

    What am I doing wrong here? Is there a command to make either "hello" or "goodbye" typed that I'm not doing?

  5. 0

    Niel

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Use the following:

    tell application "TextEdit"

    launch

    repeat

    delay (random number from 2 to 8)

    set textToType to "text" & some item of {"hello", "goodbye"} & text

    end repeat

    end tell

    (51595)

  6. 0

    eGeekee

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Perfect, got it working. Thank you so much.