How to use Automator to randomly order selected text?

I would like to know how to order selected, ordinary text randomly. I have used Automator to create a command that sorts selected text alphabetically but would like to sort some text randomly.


Thank you,

Daniel

Posted on Dec 19, 2014 11:09 AM

Reply
4 replies

Dec 19, 2014 3:11 PM in response to MrStonefish

Here is one approach on how to make a random, no-repeat list of words from an ordered word list.


set myList to {"first", "second", "third", "fourth", "fifth", "six"}

set rList to {}

set n to 0 as integer


repeat until (count rList) is equal to (count myList)

set n to (random number from 1 to (count myList))

if item n of myList is not in rList then

copy item n of myList to end of rList

end if

end repeat

log (rList)

Output:
(*second, six, third, first, fourth, fifth*)

Dec 20, 2014 10:39 AM in response to MrStonefish

Hello


You may try an Automator service with a Run Shell Script action as follows.


0) Automator service


- Service receives selected [text] in [any application]

- [x] replace selected text



1) Run shell Script action


- Shell: /usr/bin/ruby

- Pass input: as argument

- Code: as follows



ARGV.map { |a| a.dup }.each do |a| if ( d = a[/\015\012|\015|\012/] ) e = a.chomp!(d) ? d : '' print a.split(d, -1).shuffle.join(d), e else print a.split(' ').shuffle.join(' ') end end



Resulting workflow will look something like this:


User uploaded file



The service will -


a) shuffle lines if multiple lines are selected; or


b) shuffle words (delimited by white spaces) if single line is selected.


In case of a), trailing line-ending character is preserved as the last character. And in case of b), delimiting white spaces are reduced to single space.



Regards,

H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to use Automator to randomly order selected text?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.