Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Command line audio players for Mac OS X

For information on how to install MacPorts see here and here.
# qtplay & mpg123

export PATH="/opt/local/bin:/opt/local/lib:/opt/local/include:/opt/local/man:${PATH}"
alias port=/opt/local/bin/port

port list | grep -i audio

port info qtplay 
port info mpg123

/usr/bin/sudo port -c install qtplay
/usr/bin/sudo port -c install mpg123

mpg123 file.MP3

qtplay file.MP3
qtplay 'http://file.mp3'
qtplay 'http://file.mov'     # http://www.apple.com/trailers/


#-----------------------------------------


# play, http://www.hieper.nl/html/play.html

export PATH="/usr/local/bin:${PATH}"

# create /usr/local/bin
/usr/bin/sudo /bin/mkdir -p /usr/local/bin
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local /usr/local/bin
/usr/bin/sudo /bin/chmod 0755 /usr/local /usr/local/bin

cd ~/Desktop
curl -L -O http://www.hieper.nl/downloads/play.dmg
hdiutil mount ~/Desktop/play.dmg
open -a Safari '/Volumes/play 1.3/readme.html'
/usr/bin/sudo /bin/cp -i '/Volumes/play 1.3/play' /usr/local/bin
ls -l /usr/local/bin/play
hdiutil unmount '/Volumes/play 1.3'

play ~/Music/Winterreise/Gute\ Nacht.mp3

# listen to the first 10 seconds of each Schubert song on your computer
mdfind "kMDItemComposer == Schubert" | play -vt 10


#-------------------------------------------------------------


# afplay
# compile & install afplay and related command line audio tools (Mac OS X 10.4.11)
# requires Xcode, http://developer.apple.com/tools/xcode/index.html

ls -1 /usr/bin/af*     # Mac OS X 10.5
ls -1 /usr/bin/au* 

# Mac OS X 10.4
open /Developer/Examples/CoreAudio/Services/AudioFileTools/AudioFileTools.xcodeproj
# ... then just press "Build"

ls -l /Developer/Examples/CoreAudio/Services/AudioFileTools/build/Development-Panther/* | nl
ls -1 /Developer/Examples/CoreAudio/Services/AudioFileTools/build/Development-Panther/* | nl


#/usr/bin/find /Developer/Examples/CoreAudio/Services/AudioFileTools/build/Development-Panther -mindepth 1 | \
#     /usr/bin/xargs -I '{}' /usr/bin/sudo /bin/cp -i '{}' /usr/local/bin

/bin/ls -1 /Developer/Examples/CoreAudio/Services/AudioFileTools/build/Development-Panther/* | \
     /usr/bin/xargs -I '{}' /usr/bin/sudo /bin/cp -i '{}' /usr/local/bin

ls -l /usr/local/bin/af*
ls -l /usr/local/bin/au*

afconvert
afinfo
afinterleave
afplay
afrecord
auprocess
auprofile


# background music
# cf. http://codesnippets.joyent.com/posts/show/1508
/usr/bin/screen -d -m afplay "/path/to/file.MP3"

kill -HUP $$
kill -HUP $PPID
killall -HUP Terminal


apropos audio
apropos sound

# set global Mac OS X sound output volume
# see http://osxutils.sourceforge.net
man setvolume   
setvolume 50

Clear the terminal screen & scrollback buffer

# make sure access for assistive devices is enabled in the "Universal Access" System Preference
open /System/Library/PreferencePanes/UniversalAccessPref.prefPane

# cf. Terminal -> Scrollback -> Clear Scrollback [cmd-k]
function clear_screen() {
   /usr/bin/open -a Terminal
   /usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'
   return 0
}

clear_screen


# you may use clear instead if scrollback is disabled in Terminal.app
defaults read com.apple.Terminal Scrollback
defaults write com.apple.Terminal Scrollback -string NO
#defaults write com.apple.Terminal Scrollback -string YES
kill -HUP $$
[cmd-n]
ls -l
clear

# in addition it may be useful to disable adding commands to the history list
#open -e ~/.bash_history
#set +o history   

Vertical & horizontal split for man screen

See: Compiling screen from CVS on OSX for vertical split and Terminal Productivity Tips

export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/lib:/usr/local/include:/usr/bin:/bin:/usr/sbin:/sbin
export IFS=$' \t\n'

# compile & install gawk on Mac OS X
# http://www.gnu.org/software/gawk/

cd ~/Desktop
/usr/bin/curl -L -O http://ftp.gnu.org/pub/gnu/gawk/gawk-3.1.6.tar.gz
/usr/bin/tar -xzf gawk-3.1.6.tar.gz
cd gawk-3.1.6
./configure --help
./configure --disable-nls --prefix=/usr/local
/usr/bin/make
/usr/bin/sudo /usr/bin/make install

/usr/local/bin/gawk --version
stat -x /usr/local/bin/gawk


# enable download from cvs.savannah.gnu.org through ipfw firewall
# cf. Example ipfw ruleset, http://codesnippets.joyent.com/posts/show/1267

# get the IP address
host cvs.savannah.gnu.org
dig -x 199.232.41.69 +short

grep cvs /etc/services
zgrep ':2401' /private/var/log/ipfw*


# get a free ipfw rule number
/usr/sbin/sysctl -n net.inet.ip.fw.autoinc_step
/usr/bin/sudo /sbin/ipfw list

# choose appropriate numbers for num1 & num2 according to your ipfw ruleset
function free_ipfw_num() {
   declare -i num1=6701 num2=6799 lastipfwnum
   if [[ $(/usr/sbin/sysctl -n net.inet.ip.fw.autoinc_step) -ne 100 ]]; then return 1; fi
   lastipfwnum=$(/usr/bin/sudo /sbin/ipfw list | /usr/bin/tail -n 2 | /usr/bin/head -n 1 | /usr/bin/awk '{print $1}')
   if [[ $lastipfwnum -lt $num2 ]]; then return 1; fi
   #while [[ -n $(/usr/bin/sudo /sbin/ipfw list | /usr/bin/awk -v n1=$num1 -v n2=$num2 '$1 >= n1 && $1 < n2 {print $1;}') ]]; do
   while $(/usr/bin/sudo /sbin/ipfw show ${num1} &>/dev/null) ; do
      let "num1 += 1"
      if [[ $num1 -gt $num2 ]]; then num1=; break; return 1; fi
   done
   echo $num1
   return 0
}

free_ipfw_num   # 6701


# allow internet traffic to & from cvs.savannah.gnu.org (port 2401) through ipfw
/usr/bin/sudo /sbin/ipfw -q add 6701 allow log { src-ip 199.232.41.69 or dst-ip 199.232.41.69 } dst-port 2401 keep-state

# alternatives
#/usr/bin/sudo /sbin/ipfw -q add 6701 allow log { tcp or udp } from me to 199.232.41.69 dst-port 2401 keep-state
#/usr/bin/sudo /sbin/ipfw -q add 6701 allow log { tcp or udp } from me to any dst-port 2401 keep-state


/usr/bin/sudo /sbin/ipfw show 6701

# delete rule again after download (see below)
#/usr/bin/sudo /sbin/ipfw -q delete 6701



# cf. Compiling screen from CVS on OSX for vertical split,
# http://writequit.org/blog/?p=183

cd ~/Desktop

/usr/bin/cvs -z3 -d:pserver:anonymous@cvs.savannah.gnu.org:/sources/screen co screen

/usr/bin/sudo /sbin/ipfw -q delete 6701
/usr/bin/sudo /sbin/ipfw show 6701

cd screen/src

/usr/bin/patch < /opt/local/var/macports/sources/rsync.macports.org/release/ports/sysutils/screen/files/patch-maxargs
/usr/bin/patch < /opt/local/var/macports/sources/rsync.macports.org/release/ports/sysutils/screen/files/patch-windowsize
/usr/bin/patch < /opt/local/var/macports/sources/rsync.macports.org/release/ports/sysutils/screen/files/patch-pty.c

./configure --help


#./configure --enable-locale --enable-telnet --enable-colors256 --enable-rxct_osc

./configure --prefix=/usr/local --enable-locale --enable-telnet --enable-colors256 --enable-rxct_osc

/usr/bin/make

/usr/bin/sudo /usr/bin/make install


#--------------------------------------


ls -l /usr/local/bin/screen

find -x /usr/local -name "screen*"
man /usr/local/man/man1/screen.1
info /usr/local/info/screen.info

alias screen=/usr/local/bin/screen

screen -v   # Screen version 4.00.03jw4 (FAU) 2-May-06
screen --help

# horizontal split
# cf. http://www.afp548.com/article.php?story=20070525141734763
screen -q
[ctrl-a][shift-s]
[ctrl-a][tab]
[ctrl-a][c]
top -du
[ctrl-a][shift-s]
[ctrl-a][tab]
[ctrl-a][c]
exit
[ctrl-a][tab]
exit
[ctrl-a][tab]
q
exit


# vertical split
# cf. http://writequit.org/blog/?p=183

screen -q
[ctrl-a][alt-|]
[ctrl-a]:resize 25
[ctrl-a]:resize 30%
[ctrl-a][tab]
[ctrl-a][c]
top -du
[ctrl-a][alt-|]
[ctrl-a][tab]
[ctrl-a][c]
exit
[ctrl-a][tab]
exit
[ctrl-a][tab]
q
exit

Switch to the login window from the command line

# cf. http://www.mactipper.com/2008/07/securely-lock-your-mac-system.html,
# http://www.macosxhints.com/article.php?story=20040724203315798 and
# http://www.apple.com/downloads/macosx/automator/lockdesktop.html

/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend

/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID username


alias lo='/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'

# <ctrl> + l
help bind
bind -P | grep -i c-l
bind -x '"\C-l"':'/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'

# <esc> + l
#bind -x '"\M-l"':'/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'
bind -x '"\el"':'/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend'


#---------------------------------------------


open -a 'Keychain Access'

# go to  Keychain Access > Preferences ... > General > activate "Show Status in Menu Bar" 
# now you can use "Lock Screen" via the Keychain Access menu bar item

Attach a screen before chrooting into your linux from scratch minimal system

This snippet is not so much a snippet as a tip. It's goal is to make your Linux from Scratch experience easier by allowing you the freedom of being able to attach and detach to a screen session from your host computer. Of course you would go through the book and change the chroot command to suit your needs, but just bear in mind that the chroot command you will use may be different from the one I use.

By using screen, you can start a lengthy compile and then detach, go about your business and re-attach another time. I use it primarily when I have to start a compilation from one computer, then later from another machine located somewhere else. Combined with SSH, this can be real handy.

desktop ~ # screen -S clfs
desktop ~ # chroot "${CLFS}" /tools/bin/env -i \
>     HOME=/root TERM="${TERM}" PS1='\u:\w\$ ' \
>     PATH=/bin:/usr/bin:/sbin:/usr/sbin:/tools/bin \
>     /tools/bin/bash --login +h
root:/#


At any time, you can hit ctrl+a+d to detach. Then if you want to re-attach just type screen -r clfs and whamo, your right back where you left off.

Thanks again to ChrisS67 for helping me with my GCC problem (or me forgetting to install findutils) ;)