Skip to content

Instantly share code, notes, and snippets.

@LukeChannings
Created July 15, 2011 17:58
Show Gist options
  • Save LukeChannings/1085175 to your computer and use it in GitHub Desktop.
Save LukeChannings/1085175 to your computer and use it in GitHub Desktop.
A script to start a random Application in OS X
#!/bin/bash
##
# RunRandom
# - A script to run n random applications.
#
# Copyright 2011 Luke Channings
#
# Usage:
# To run a single app: ./runrandom.sh
# To run 10 random apps: ./runrandom.sh 10
##
# Declare app index.
i=0
echo "Building a list of applications."
# Loop through applications.
while read -r location; do
# Add the application to the array.
app[$i]=$location
# Increment the loop.
(( i += 1 ))
done < <(find /Applications -name *.app 2> /dev/null)
echo "$i Applications found."
# Declare random index.
x=0
# Establish how many applications to run.
if ([ ! -z "$1" ] && [ $1 -eq $1 ]) 2> /dev/null; then
n=$1
elif [ -z "$1" ]; then
n=1
else
echo "NaN."
exit
fi
echo "Running $n random application(s)."
# App Execution loop.
while [ $x -lt $n ]; do
# Fetch a random number.
R=$RANDOM
N=`expr $(echo $R | wc -c) - 1`
d=`awk "BEGIN{print 10^$N}"` # Divisor.
R=`awk "BEGIN{print ($R/$d)*$i}"`
R=${R/\.*}
# Make sure the random number isn't too large.
while [ $R -gt $i ]; do
# Half the number until it is small enough.
R=`expr $R / 2`
done
# Run a random application.
open "${app[$R]}"
# Increment the loop.
(( x += 1 ))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment