How to Randomize/Shuffle Lines in a Variable or Clipboard?

Any tips on how to randomize lines? Thank you.

from:

Apple
Bob
Car
Dog

To:

Car
Dog
Bob
Apple

Hey Leonardo,

This is probably not purely random, but it might be good enough for your task.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/10/31 01:18
# dMod: 2021/10/31 01:18 
# Appl: Miscellaneous AppleScript
# Task: Randomly Shuffle a List.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, , @Randomly, @Shuffle, @List
--------------------------------------------------------

set dataList to paragraphs 2 thru -2 of "
Apple
Bob
Car
Dog
"

set collatorList to {}
set maxNum to length of dataList
set randomNumber to random number from 1 to maxNum

repeat with i from 1 to length of dataList
   set listItemReference to (a reference to item randomNumber of dataList)
   set end of collatorList to listItemReference's contents
   set listItemReference's contents to 0
   set dataList to text of dataList
   set maxNum to maxNum - 1
   set randomNumber to random number from 1 to maxNum
end repeat

set AppleScript's text item delimiters to linefeed
return collatorList as text

--------------------------------------------------------

To get a variable from Keyboard Maestro into the Execute an AppleScript action:

set kmInstance to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
   set asVarName to getvariable "MyVariableName" instance kmInstance
end tell

-Chris

Thank you so much!

1 Like