Disclaimer: I am OS Agnostic. Please don’t send me hate mail about operating systems.
So for the last 4 years I’ve been using this MacBook Pro as my main laptop (OSX Mountain Lion through macOS Sierra). I’ve also run Parallels Desktop for various flavors of Windows, Android, and Fedora. I can honestly say I’ve thoroughly enjoyed using this machine and software setup. It has served me well. For my next personal laptop, I may or may not stay with the Mac world, but regardless, I want to share some scripts and tweaks I’ve used over the last 4 years.
First off directory listings with Terminal (bash) (using the UNIX
ls command). Obviously I don’t need to harp on the power of this command, but I do want to note the differences on a Mac. Because OSX/Darwin was originally built with a BSD Unix (read “beastie”),
ls and many other common UNIX commands don’t work exactly the same as they do on, say, just about all flavors of Linux, and thus just about all distributions of UNIX.
For example, the
-G hides group names on Linux, but for some reason toggles colors on BSD Unix. That’s not a huge deal, but what is a huge deal is the case-sensitive sorting. On a Mac,
ls sorts files and folders case-sensitive (using ASCII value), whereas Linux
ls sorts case-insensitive for a directory listing.
So I found that flipping the -f flag on, which actually disables sorting, sorts the files/folders the way I want. Why would turning sorting off enable case-insensitive sorting? Here’s a good StackExchange thread on that. Ultimately I ended up using the following alias (which of course I can’t live without now):
alias la="ls -lfG"
Note that flipping on
-f also forces
-a (all), which shows everything, including hidden files and folders. So you don’t have to manually turn that on if you want it (and I want it). This alias is still not perfect, as I’d like folders and files to be separated, and I’d prefer
-a, but it’s pretty good, and, well, you can’t have everything 🙂
Actually if you really want to escape BSD UNIX, feel free to install Homebrew or MacPorts, both of which are awesome environments for giving you Linux UNIX on your Mac. There are just some tools you can’t live without, and those environments really solve the problem!
Toggling Hidden Files
In a shell, I always want to see hidden files (
ls), but in a windowed environment, not so much. Especially on a Mac, where there are
.DS_Store files in every directory and various hidden files on the desktop. So, when you do need to see hidden files in a windowed environment… there’s a script for that!
# read current option into variable isVisible=$(defaults read com.apple.finder AppleShowAllFiles) # toggle visibility if [ $isVisible == FALSE ] then defaults write com.apple.finder AppleShowAllFiles TRUE else defaults write com.apple.finder AppleShowAllFiles FALSE fi # force changes by restarting Finder killall Finder
toggleHidden from the shell and voilà, all your hidden files are belong to us, until you toggle again.
So there was a fairly well-known bug with OSX and WiFi after the machine would go into sleep or auto-lock (see here and here). One of the fixes at the time (and perhaps still useful that’s why I’m providing it here), was to toggle the WiFi device off and then back on. And because it’s always faster to do everything from a shell… Simply use the UNIX
ifconfig command to get a list of your network devices and then use the same command to turn the WiFi device off/on (down/up). Below,
en1 is the name of my WiFi device, one of the devices in the
ifconfig list. If you don’t know which device is your WiFi device, use System Preferences->Network to find its MAC address.
sudo ifconfig en1 down
sudo ifconfig en1 up
Converting Between Windows and OSX Network Paths
This is a pet peev of mine. Whenever someone sends/emails/documents a network path, it is almost always OS specific. And when you need to visit the path from a foreign operating system, it sucks having to massage it into place. Now there are probably a gajillion solutions to this problem, but I’ve been using a Python script to go back and forth between network paths (namely OSX and Windows) for years. It was written in Python 2, but may still work for 3 (untested). There are probably some special cases; it’s not unit-tested; and it assumes SMB over CIFS network access on OSX. But, in general, it has served me well these past few years.
#!/usr/bin/python # -*- coding: utf-8 -*- ''' @author: Joe Del Rocco @since: 08/04/2015 @summary: This program converts network paths between Windows and OSX. ''' import sys import argparse def main(): # handle command line arguments parser = argparse.ArgumentParser(description='A program that converts network paths between OSX and Windows.', formatter_class=argparse.RawTextHelpFormatter) parser.add_help = True parser.add_argument('filepath', help='path to convert') parser.add_argument('-r', '--reverse', dest='reverse', action='store_true', help='convert the opposite direction') args = parser.parse_args() # path required as parameter if not args.filepath: print "Error: no filepath specified" sys.exit(2) # strip drive if present drive = (args.filepath.find(':') > -1) if (drive): splitPath = args.filepath.split(':') args.filepath = splitPath # convert and reassemble if not args.reverse: args.filepath = '\\' + args.filepath # add another slash args.filepath = args.filepath.replace('\\', '/') # convert args.filepath = 'smb:' + args.filepath # add Samba drive prefix else: if not drive: args.filepath = '/' + args.filepath # add another slash args.filepath = args.filepath.replace('/', '\\') # convert # output final path print args.filepath if __name__ == "__main__": main()
WAV to CAF Files
This one is for those that need a CAF audio file. Say, for example, if you’re building an iOS app or something. I got tired of converting .wav files one at a time, and so I wrote a small script to convert a whole directory of them to
.caf files. It’s really just a wrapper for
afconvert. You can optionally pass the format of the .caf file. It could easily be extended to support more options. I wrote it in less than 5 minutes and have used it many times.
#!/usr/bin/python # -*- coding: utf-8 -*- ''' @author: Joe Del Rocco @since: 11/04/2014 @summary: Script to convert a folder of .wav to .caf ''' import sys import os import argparse import glob def main(): # handle command line args parser = argparse.ArgumentParser(description='A program that converts .wav to .caf files.', formatter_class=argparse.RawTextHelpFormatter) parser.add_help = True parser.add_argument('-f', '--folder', dest='folder', type=str, default='.', help='folder of files to convert') parser.add_argument('-d', '--data', dest='data', type=str, default='ima4', help='data format of .caf') args = parser.parse_args() # process files in folder os.chdir(args.folder) for filename in glob.glob("*.wav"): name, ext = os.path.splitext(filename) command = 'afconvert -f caff -d ' + args.data + ' ' + filename + ' ' + name + '.caf' os.system(command) if __name__ == "__main__": main()