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!)

seticon - set icon of Mac OS X files

# See:
# http://osxutils.sourceforge.net
# http://www.sveinbjorn.org/osxutils_docs

cd ~/Desktop
curl -L -O http://surfnet.dl.sourceforge.net/sourceforge/osxutils/osxutils1.7.pkg.zip
unzip -qq osxutils1.7.pkg.zip
open -a Installer osxutils1.7.pkg

/bin/mkdir -p ~/Desktop/IconDir
/bin/cp /Library/Desktop\ Pictures/Nature{/Ladybug.jpg,'/Evening Reflections.jpg'} ~/Desktop/IconDir
/usr/bin/sips -i ~/Desktop/IconDir/*

/usr/bin/touch ~/Desktop/IconDir/testfile.txt
/bin/chmod 0777 ~/Desktop/IconDir/*

ls -l ~/Desktop/IconDir/*

open ~/Desktop/IconDir

# set icon
/usr/local/bin/seticon ~/Desktop/IconDir/Ladybug.jpg ~/Desktop/IconDir/testfile.txt

# update file system changes of open Finder window
/usr/local/bin/wsupdate ~/Desktop/IconDir/testfile.txt
#/bin/mv ~/Desktop/IconDir{/testfile.txt,/tmp.txt} && /bin/mv ~/Desktop/IconDir{/tmp.txt,/testfile.txt} 


# set icon
/usr/local/bin/seticon ~/Desktop/IconDir/'Evening Reflections'.jpg ~/Desktop/IconDir
/usr/local/bin/wsupdate ~/Desktop/IconDir

/bin/chmod -R 0755 ~/Desktop/IconDir
ls -al ~/Desktop/IconDir/*


# geticon
sips -i file.jpg
geticon -t icns file.jpg


# list files with resource forks
# cf. http://www.entropy.ch/blog/Mac+OS+X/2005/03/30/,
# http://forums.macosxhints.com/showthread.php?t=70224 and
# "can UNIX resolve OS X aliases?", http://forums.macosxhints.com/showthread.php?t=19960

cd ~/Desktop/IconDir
ls -1 | while IFS= read -r i; do if [[ -s ${i}/..namedfork/rsrc ]]; then ls -l "${i}/..namedfork/rsrc"; fi; done
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -n1 -I '{}' ls -l '{}'/..namedfork/rsrc
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -n1 -I '{}' sed -n -e 'l' '{}'/..namedfork/rsrc
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -n1 -I '{}' ruby -n -e 'p $_.to_s' '{}'/..namedfork/rsrc
find . -mindepth 1 -maxdepth 1 -type f -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -n1 -I '{}' strings '{}'/..namedfork/rsrc

# delete resource fork
find . -type f -maxdepth 1 -exec test -s {}/..namedfork/rsrc \; -print0 | xargs -0 -I '{}' /bin/cp /dev/null '{}/..namedfork/rsrc'

ditto --norsrc file.jpg{,.bak}
ditto --norsrc file.jpg.bak file.jpg

# some additional resource fork tools
man Rez
man DeRez
man RezWack     # create a flattened file from resource and data fork
man UnRezWack
man SplitForks
man FixupResourceForks

/Developer/Tools/Rez --help
/Developer/Tools/DeRez --help
/Developer/Tools/RezWack --help
/Developer/Tools/UnRezWack --help
/Developer/Tools/SplitForks --help
/System/Library/CoreServices/FixupResourceForks --help

ls -1 /Developer/Tools/*
/Developer/Tools/RezDet --help
/Developer/Tools/RezDet -d file; echo $?
/Developer/Tools/RezDet -s file; echo $?

man CpMac | less -N -j -9 -p 'As of Mac OS X 10.4' 
man MvMac | less -N -j -11 -p 'As of Mac OS X 10.4' 

# http://tclresource.sourceforge.net
man tclresource


Further information:

- Mac 101: Change Your Icons
- Mac OS X: Changing the Icon for a File Type
- How Do I Change File Icons And The Default Application On My Mac?
- Set Icon
- Mac Icon FAQ
- Setting an Icon From the CLI
- osxutils Documentation
- Aliases, symbolic links, Path Finder aliases... Help!
- BlueHarvest
- Making Resource-Fork-Aware Backups with rsync on Mac OS X

"openterminal" contextual menu item with Automator

Right-click on a file or folder in a Finder window and select Automator -> openterminal to open it in Terminal.app.
(i.e. cd to the folder or open the file in the nano text editor)

open -a Automator

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

Drag or add actions here to build your workflow:
Library: Finder -> Action: Get Selected Finder Items
Library: Automator -> Action: Run Shell Script
                                 - Shell: /bin/sh
                                 - Pass input: as arguments


if [[ $# -gt 1 ]]; then exit 0; fi

if [[ -d "$@" ]]; then

   printf "%s" "$@" | /usr/bin/pbcopy
   #/usr/local/bin/cpath # cf. http://osxutils.sourceforge.net

   /usr/bin/open -a Terminal

   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "printf \"\\e[8;26;115;t\"; printf \"\\e[3;300;240;t\"; cd \"$(/usr/bin/pbpaste)\"; /usr/bin/clear" in (first window whose name contains " ")'

# alternative without /usr/bin/clear (experimental)
# requires:
# defaults write com.apple.Terminal Autowrap NO
# or:
# Terminal menu -> Window Settings ...  -> Buffer -> in the Scrollback 
# section check the box next to "Wrap lines that are too long" 
# -> click "Use Settings as Defaults"

#   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "cd \"$(/usr/bin/pbpaste)\"; /usr/bin/tput cup 0 0; /usr/bin/tput dl1; /usr/bin/tput el; /usr/bin/tput dl1; /usr/bin/tput el" in (first window whose name contains " ")'

   exit 0

elif [[ -r "$@" ]]; then 

   printf "%s" "$@" | /usr/bin/pbcopy

   /usr/bin/open -a Terminal

   /usr/bin/osascript -e 'tell application "Terminal" to do script with command "printf \"\\e[8;26;115;t\"; printf \"\\e[3;300;240;t\"; /usr/bin/clear; /bin/sleep 0.3; /usr/bin/nano \"$(/usr/bin/pbpaste)\";" in (first window whose name contains " ")'

exit 0

fi

exit 0


# now save the openterminal Automator workflow as a contextual menu item:
# Automator -> File -> Save As Plug-in ... -> Save Plug-in As: openterminal -> Plug-in for: Finder -> click Save


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


open -a Automator ~/Library/Workflows/Applications/Finder/openterminal.workflow

/usr/bin/automator ~/Library/Workflows/Applications/Finder/openterminal.workflow >/dev/null 2>&1


More on how to cd effectively:

- cdto - Finder Toolbar button to open a Terminal window
- History of visited directories in BASH
- bash Tips and Tricks: $CDPATH
- Bash Shell Customisation: cdable_vars
- The Definitive Guide to Bash Command Line History
- Working Productively in Bash's Vi Command Line Editing Mode (with Cheat Sheet)
- Leopard's bash auto-completion vs. symlinked directories
- zg.rb (ZoomGo)
- fuzzy_file_finder.rb
- cd to Directory within Terminal
- Pimp my shell
- Per-directory bash history
- Terminal Tricks: A Fuzzy cd Command
- Cdots - Change directory back - 1-7 times - and forth with TAB-completion
- Fine-tuning tab completion in Bash
- recd on sourceforge
- recd - cd again via regular expression
Download with: curl -L -O http://heanet.dl.sourceforge.net/sourceforge/recd/recd.sh

newfolder contextual menu item with Automator

Right-click on a folder in a Finder window and select Automator -> newfolder to create a "NewFolder".


open -a Automator

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

Drag or add actions here to build your workflow:
Library: Finder -> Action: Get Selected Finder Items
Library: Automator -> Action: Run Shell Script
                                 - Shell: /bin/sh
                                 - Pass input: as arguments

current_dir="$@"
name_of_new_dir="NewFolder"

if [[ -d "${current_dir}" ]] && [[ -w "${current_dir}" ]]; then
   /bin/mkdir -p "${current_dir}${name_of_new_dir}"
fi

exit 0


# now save the newfolder Automator workflow as a contextual menu item
Automator -> File -> Save As Plug-in ... -> Save Plug-in As: newfolder -> Plug-in for: Finder -> Save

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

open ~/Library/Workflows/Applications/Finder/newfolder.workflow

# now open your Home directory, right-click on a folder and select Automator -> newfolder to create a "NewFolder" 
open ~

Mount Strongspace to a folder in Ubuntu

1) Install the software
sudo apt-get install sshfs


2) Add fuse to /etc/modules
sudo nano /etc/modules


3) Add yourself to the 'fuse' group, then log out and log in again.
sudo adduser your-username fuse


4) Create a mountpoint and give yourself ownership
sudo mkdir /media/mount-name
sudo chown your-username /media/mount-name


5) Mount the filesystem
sshfs remote-system-name:/remote-folder /media/mount-name


6) Unmount the filesystem
fusermount -u /media/mount-name


Directions lifted from Ubuntu forums and here also.

For myself, I had better results running the following command in the same directory that the file that I mounted resides in...

sudo sshfs user_name@subdomain.strongspace.com: folder_name


Where folder_name is the name of the folder that you are mounting strongspace to.