The Original Prankster

Version: v0.1

Summary: A script a threw together to prank Mac users.

Code:

#!/bin/bash
 
#Customize what's in this area...
 
MinSleepTime=3
MaxSleepTime=10
 
WisperVol=13
 
Line[1]="Looking good today."
Line[2]="Hey Sexy!"
Line[3]="What are you doing?"
Line[4]="Are you ok?"
Line[5]="Help me!"
Line[6]="I can't get out!"
Line[7]="You look sad."
Line[8]="Do you ever get the feeling you are being watched?  You should."
Line[9]="I'm watching you."
Line[10]="Why are you not helping me?"
Line[11]="Would you like to play?"
 
Lines=11
 
#Don't customize anything below this area.
function setVolume() {
	TargetVol=${1}
 
	CurrentVol=`osascript -e "get output volume of (get Volume settings)"`
 
	if [ ${CurrentVol} -gt ${TargetVol} ]; then
		while [ ${CurrentVol} -gt ${TargetVol} ]; do
			NewVol=${CurrentVol}
			(( NewVol -= 2 ))
			echo "Turning volume down: ${NewVol}"
			osascript -e "set Volume output volume ${NewVol}"
			CurrentVol=`osascript -e "get output volume of (get Volume settings)"`
			echo "Volume is now: ${CurrentVol}"
			sleep .001
		done
	elif [ ${CurrentVol} -lt ${TargetVol} ]; then
		while [ ${CurrentVol} -lt ${TargetVol} ]; do
			NewVol=${CurrentVol}
			(( NewVol += 2 ))
			echo "Turning volume up: ${NewVol}"
			osascript -e "set Volume output volume ${NewVol}"
			CurrentVol=`osascript -e "get output volume of (get Volume settings)"`
			echo "Volume is now: ${CurrentVol}"
			sleep .001
		done
	fi	
}
 
function say() {
	Line=$1
 
	#Get Original Volume
	OriginalVol=`osascript -e "get output volume of (get Volume settings)"`
 
	#Adjust to target wisper volume.
	setVolume "${WisperVol}"
 
	#Speak
	echo "Speaking line: ${Line}"
	osascript -e "say \"${Line}\" using \"Whisper\""
 
	#Return to original volume
	setVolume "${OriginalVol}"
}
 
while [ true ]; do
	echo
	#Set sleep time.
	RandomSleepTime=`echo $((RANDOM%${MaxSleepTime}+${MinSleepTime}))`
	RandomSleepTime=$((RandomSleepTime*60))
	echo "Sleeping for ${RandomSleepTime} seconds."
	sleep ${RandomSleepTime}
 
	#Set Random Line Number
	RandomLine=`echo $((RANDOM%${Lines}+1))`
	say "${Line[${RandomLine}]}"
done

Directions:

  1. Give the script a “.command” extension.
  2. Drag into the users’ login items and check the “Hide” box.
  3. Sit back and enjoy yourself.

Changes:
v0.1: Initial Release

HTC Android Splasher

Version: v0.1

Summary: A quick script a threw together to allow Mac or Linux users to create and flash splash screens for their HTC Android phones.

Requirements:

Code:

#!/bin/bash
 
##Written by Blake Johnson
##Website: http://www.blakeanthonyjohnson.com/
##Script requires ImageMagick and nbimg to be installed and located in $PATH.
 
#Convert image to 24-bit bitmap.
echo -n "Converting image to 24-bit bitmap...   "
convert "$1" "/tmp/$1.bmp"
convert "/tmp/$1.bmp" +matte "/tmp/$1.bmp"
echo "[Done]"
echo
 
#Check image dimensions.
echo -n "Verifying image dimensions...   "
ImageInfo=`identify "/tmp/$1.bmp"`
if [[ "$ImageInfo" =~ "480x800" ]]
then
	echo "[Done]"
	echo "Dimensions (480x800) verified for the following phones: HTC Evo, HTC Desire"
elif [[ "$ImageInfo" =~ "320x480" ]]
then
	echo "[Done]"
	echo "Dimensions (320x480) verified for the following phones: HTC Hero"
else
	echo "Unable to verifiy proper dimensions.  Please make sure the image dimensions are correct."
	exit
fi
echo
 
#Convert to splash image.
echo -n "Converting verified image to splash format...   "
nbimg -F "/tmp/$1.bmp" > /dev/null 2>&1
echo "[Done]"
echo
 
#Check if they want it flashed via Fastboot, or just copied.
echo "Please select what should be done with the resulting image."
echo "1. Copy image to Desktop."
echo "2. Write image to phone."
echo
read -p "Please select an option. (1, 2)?"
if [[ $REPLY == "1" ]]
then
	echo "You have selected Fastboot mode."
	MODE='write'
elif [[ $REPLY == "2" ]]
then
	echo "You have selected copy-to-Desktop."
	MODE='copy'
else
	echo "Unknown selection.  Exiting."
	exit
fi
echo
 
##Perform the selected action.
if [[ "${MODE}" == "write" ]]
then
	echo "Launching the Fastboot command...  Please put phone into Fastboot mode."
	fastboot flash splash1 "/tmp/$1.bmp.nb"
	echo "Spash screen has been written successfully."
elif [[ "${MODE}" =~ "copy" ]]
then
	#Copy converted image to Desktop.
	echo -n "Copying converted splash image to Desktop...   "
	cp "/tmp/$1.bmp.nb" "$HOME/Desktop/$1.nb"
	echo "[Done]"
else
	echo "Unknown variable passed to last step."
	exit
fi

Changes:
v0.1: Initial Release

FilenameUnifier

Version: v0.2

Summary: Ever wanted to mass rename a bunch of files into a specific pattern based on their original filenames? Yeah, there are other utilities out there, but this is a simple solution based on my FilenameSanitizer script which locates filenames in a given directory with a specific pattern, and then renames them based on that pattern. As of the current version, it’s set to unify numeric .jpg images. For example, “5466.Jpeg” would become “005466.JPG”.

Requirements:

  • find
  • sed
  • wc
  • whoami

Code:

#!/bin/bash
 
LOG="${HOME}/.FilenameUnifier.log"
TMPLOG="/tmp/FilenameUnifier-`whoami`"
 
##Write Date & Time to Log File
echo "-------------------------------------" > "${LOG}"
echo "`date +\%B\ \%d,\ \%Y`" >> "${LOG}"
echo "-------------------------------------" >> "${LOG}"
 
USAGE="Usage: ${0} -s <SOURCE_DIRECTORY>"
 
echo "FilenameUnifier"
echo "Prototype by Kevin Warns"
echo "Modified by Blake Johnson"
echo "http://www.simplescripts.net/"
echo
 
while getopts 's:h' OPTION; do
	case ${OPTION} in
		s) SOURCE="${OPTARG}";;
		h) echo "${USAGE}" >&2
			exit 2;;
		\?) echo "Unknown option \"-${OPTARG}\"." >&2
			echo ${USAGE};;
		:) echo "Option \"-${OPTARG}\" needs an argument." >&2
  			echo ${USAGE}
  			exit 2;;
  		*) echo ${USAGE}
  			exit 2;;
  	esac
done
 
if [ -z "$SOURCE" ]
then
	echo "One or more parameters are missing."
	echo "${USAGE}"
	exit 2
fi
 
 
find "${SOURCE}" -type f -regextype "posix-extended" -iregex ".*[/][0-9]{1,5}\.jpe?g$" > "${TMPLOG}" 2>"${LOG}" ##Let's check for .jpg files with only numbers in their names.
 
UpperBound=$(cat "${TMPLOG}" | wc -l)  ##Count the number of results found.
 
if [ "${UpperBound}" = "0" ]  ##Check to make sure that 'find' found some files that need to be renamed.
then
	echo "There is nothing to rename."
	exit 1
else
	echo "${UpperBound} file(s) found."
	i=0
	e=0
	s=0
	while [ "${i}" -lt "${UpperBound}" ]
	do
		(( i += 1 ))
		TMPLINE=$(sed -n "${i}p" "${TMPLOG}")
		InputLocation="${TMPLINE%/*}"
		InputName="${TMPLINE##*/}"
 
		NewName="${InputName}"
		NewName=$(echo -n "${NewName}" | sed 's/jpe\?g/JPG/i')  ##Strip leading spaces and trailing spaces / periods.
 
		FilenameLength=${#NewName} ##grab the # of characters in the filename
 
		#number of digits = length - 4 (accounting for .jpg)
		let COUNTER=$FilenameLength-4
		while [ $COUNTER -lt 6 ]; do
	   	 	NewName="0"${NewName}
			(( COUNTER += 1 ))
		done
 
		OutputPath="${InputLocation}/${NewName}"
 
		if [ "${OutputPath}" == "${TMPLINE}" ] ##Check to make sure that the name was actually changed.
		then
			echo -e "\"${TMPLINE}\" [ \033[1;31mNot Renamed\033[0m ]"
			echo "Reason: Name Unchanged. This may be an error in the script."
		fi
 
		if [ -f "${OutputPath}" ] ##Check to make sure that the new name won't overwrite an existing file.
		then
			OutputPath="${OutputPath}.RENAME_${i}"
		fi
 
		echo -n "Renaming \"${TMPLINE}\" to \"${OutputPath}\"... "
		mv "${TMPLINE}" "${OutputPath}" >> "${LOG}" 2>&1
		echo "mv \"${TMPLINE}\" \"${OutputPath}\"" >> "${LOG}" 2>&1
 
		if [ "$?" == 0 ] ##Check to see if the 'mv' command failed or not.
		then
			echo -e "[ \033[1;32mRenamed\033[0m ]"
			(( s += 1 ))
		else
			echo -e "[ \033[1;31mNot Renamed\033[0m ]"
			(( e += 1 ))
		fi
		echo
		unset NewName OutputPath TMPLINE
	done
 
	echo "${s} / ${i} file(s) renamed successfully."
	echo "${e} file(s) could not be renamed."
fi
 
rm "${TMPLOG}" >> "${LOG}"
exit 0

Changes:
v0.2: Initial Release
v0.1: Prototype

Credits: Special thanks to Kevin Warns for creating a prototype for me to start off with.

Filename Sanitizer (v0.6)

Summary: This script was designed to help ease the migration of data from a Mac or Linux PC to a FAT32 flash drive, which in turn would be compatible with Windows. It does this by attempting to find any questionable characters in any of the file names, and replacing them with hyphens ‘-’. It also removes leading and trailing spaces, along with trailing periods.

Requirements:

  • find
  • sed
  • tac (coreutils)

Code:

#!/bin/bash
 
LOG="${HOME}/.FilenameSanitizer.log"
TMPLOG="/tmp/FilenameSanitizer-`whoami`"
 
##Write Date & Time to Log File
echo "-------------------------------------" > "${LOG}"
echo "`date +\%B\ \%d,\ \%Y`" >> "${LOG}"
echo "-------------------------------------" >> "${LOG}"
 
USAGE="Usage: ${0} -s <SOURCE_DIRECTORY>"
 
echo "FilenameSanitizer"
echo "Scripted by Blake Johnson"
echo "http://www.simplescripts.net/"
echo
 
while getopts 's:h' OPTION; do
	case ${OPTION} in
		s) SOURCE="${OPTARG}";;
		h) echo "${USAGE}" >&2
			exit 2;;
		\?) echo "Unknown option \"-${OPTARG}\"." >&2
			echo ${USAGE};;
		:) echo "Option \"-${OPTARG}\" needs an argument." >&2
  			echo ${USAGE}
  			exit 2;;
  		*) echo ${USAGE}
  			exit 2;;
  	esac
done
 
if [ -z "$SOURCE" ]
then
	echo "One or more parameters are missing."
	echo "${USAGE}"
	exit 2
fi
 
 
 
##This is what the script is looking for...
##1. Illegal Characters
##2. Leading Spaces
##3. Trailing Spaces and periods.
find "${SOURCE}" \
-regex ".*[][%\+\\\"\*;:\?\|<>,=]+[^\/]*$" \
-o -regex ".*\/[ ]+[^\/]*$" \
-o -regex ".*[ .]+$" \
| tac > "${TMPLOG}" 2>"${LOG}" ##In most cases, reversing the results requires only one run of this script.
 
UpperBound=$(cat "${TMPLOG}" | wc -l)  ##Count the number of results found.
 
if [ "${UpperBound}" = "0" ]  ##Check to make sure that 'find' found some files that need to be renamed.
then
	echo "There is nothing to rename."
	exit 1
else
	echo "${UpperBound} file(s) found."
	i=0
	e=0
	s=0
	while [ "${i}" -lt "${UpperBound}" ]
	do
		(( i += 1 ))
		TMPLINE=$(sed -n "${i}p" "${TMPLOG}")
		InputLocation="${TMPLINE%/*}"
		InputName="${TMPLINE##*/}"
 
		NewName="${InputName}"
		NewName=$(echo -n "${NewName}" | sed 's/^ *//' | sed 's/[ .]*$//')  ##Strip leading spaces and trailing spaces / periods.
		NewName=$(echo -n "${NewName}" | sed "s/[][%\+\\\"\*;:\?\|<>,=]/-/g") ##Replace illegal characters.
 
		if [ "${NewName}" == "" ]  ##Check to make sure the new file name is not blank.
		then
			NewName="Untitled - ${i}"
		fi
 
		OutputPath="${InputLocation}/${NewName}"
 
		if [ "${OutputPath}" == "${TMPLINE}" ] ##Check to make sure that the name was actually changed.
		then
			echo -e "\"${TMPLINE}\" [ \033[1;31mNot Renamed\033[0m ]"
			exit
		fi
 
		if [ -f "${OutputPath}" ] ##Check to make sure that the new name won't overwrite an existing file.
		then
			OutputPath="${OutputPath}.RENAME_${i}"
		fi
 
		echo -n "Renaming \"${TMPLINE}\" to \"${OutputPath}\"... "
		mv "${TMPLINE}" "${OutputPath}" >> "${LOG}" 2>&1
 
		if [ "$?" == 0 ] ##Check to see if the 'mv' command failed or not.
		then
			echo -e "[ \033[1;32mRenamed\033[0m ]"
			(( s += 1 ))
		else
			echo -e "[ \033[1;31mNot Renamed\033[0m ]"
			(( e += 1 ))
		fi
		echo
		unset NewName OutputPath TMPLINE
	done
 
	echo "${s} / ${i} file(s) renamed successfully."
	echo "${e} file(s) could not be renamed."
fi
 
rm "${TMPLOG}" >> "${LOG}"
exit 0

Changes:
v0.6: Simplified the search command. Added some more questionable characters. Added ability to set parameters at the command line. Displays usage line upon error.
v0.5: Name changed to “Filename Sanitizer”. Simplified the replacement of illegal characters. Parses the results in reverse order. Added check for duplicate names. Commented code a bit.
v0.4: Search results are now written to a file instead of variable, fixes bug with trailing spaces, removes trailing periods, removes leading and trailing spaces, counts and shows success rate.
v0.3: Name changed to Filename Cleanser. Added more questionable characters to search for.
v0.2: Errors are logged, exit status of each rename command, and some other revisions.
v0.1: Initial release.

Bugs / Unimplemented Features:

  • File names with repeating periods _may_ end up disappearing off the face of the planet. (I don’t know why, or how.)