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

fsinfo

fsinfo - get file status information with Apple's FSMegaInfo sample code
See: FSMegaInfo by Apple and FSMegaInfoGUI by Ross Tulloch

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

/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 -LO http://developer.apple.com/samplecode/FSMegaInfo/FSMegaInfo.zip
unzip -qq FSMegaInfo.zip

open -e ~/Desktop/FSMegaInfo/Read\ Me\ About\ FSMegaInfo.txt


file -ik ~/Desktop/FSMegaInfo/build/Debug/FSMegaInfo
otool -Lmv $_
otool -hmVv $_
otool64 -hmVv $_
lipo -detailed_info $_


/usr/bin/sudo /bin/cp -i ~/Desktop/FSMegaInfo/build/Debug/FSMegaInfo /usr/local/bin
ls -l /usr/local/bin/FSMegaInfo

/usr/bin/sudo /bin/ln -is /usr/local/bin/FSMegaInfo /usr/local/bin/fsinfo
ls -l /usr/local/bin/fsinfo


# create a test file
testfile="${HOME}/Desktop/testfile.txt"
jot -b 'sample text' 10 | cat -n > $testfile


FSMegaInfo help
fsinfo help
fsinfo help 2>&1 | grep -i info

fsinfo -v help stat
fsinfo stat $testfile

fsinfo -v help FSGetCatalogInfo
fsinfo -v help FSGetCatalogInfo 2>&1 | grep -i finder
fsinfo -v FSGetCatalogInfo $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoGettableInfo $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoGettableInfo $testfile 2>&1 | grep -i encod


# createDate

fsinfo -v FSGetCatalogInfo -kFSCatInfoCreateDate $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoAccessDate,kFSCatInfoContentMod,kFSCatInfoAttrMod,kFSCatInfoCreateDate $testfile

stat -x $testfile | tail -n 3
stat -f $'%N:\nlast accessed:\t\t%Sa\nlast modified:\t\t%Sm\nlast inode change:\t%Sc' $testfile

touch -f $testfile

stat -x $testfile | tail -n 3
stat -f $'%N:\nlast accessed:\t\t%Sa\nlast modified:\t\t%Sm\nlast inode change:\t%Sc' $testfile

fsinfo -v FSGetCatalogInfo -kFSCatInfoCreateDate $testfile
fsinfo -v FSGetCatalogInfo -kFSCatInfoAccessDate,kFSCatInfoContentMod,kFSCatInfoAttrMod,kFSCatInfoCreateDate $testfile

Webdav upload utility

Command line utility to upload files to a webdav server.

Requires net_digest_auth.rb (snippet)

NB: Does not handle MKCOL, so the directory where you place the file must already exist.

Usage

$ ruby upload.rb cert.yml source destination


where cert.yml is a file of the form

username: USERNAME
password: PASSWORD


source is the path to the file to upload

destination is the URL of a directory on a webdav server to upload to.

# upload.rb
# Command line webdav upload script. Based off of 
# http://theexciter.com/articles/bingo

require 'net_digest_auth'
require 'yaml'
require 'uri'

abort("Usage: #{$0} <credentials.yml> <src> <dst> ") unless ARGV.size==3

auth = YAML.load_file(ARGV[0])
username = auth['username']
password = auth['password']

src = ARGV[1]
dst = ARGV[2]

if File.exists?(src)
  url = URI.parse(dst)
  Net::HTTP.start(url.host) do |http|
    res = http.put(url.request_uri, 'hello') # try putting something so
                                             # the server will return a
                                             # www-authenticate header
    req = Net::HTTP::Put.new("#{url.path}#{File.basename(src)}")
    req.digest_auth(username, password, res)
    response = http.request(req, File.open(src).read)
    puts response.code + " " + response.message
  end
else
  puts "No such file #{src.inspect}"
end

IdGenerator in java

// IdGenerator

public class IdGenerator {
	
	private long maxId; 
	
	public IdGenerator(long start) {
		maxId = start;
	}
	
	public long getNextId () {
		return ++maxId;
	}

} // IdGenerator

Update Title

// simple function to update a page's title

updateTitle: function(title) { document.title = title; }

Browser Dimensions

// various functions related to viewable browser dimensions and scroll offset

putCenter: function(item) {
  item = $(item);
  var xy = item.getDimensions();
  var win = this.windowDimensions();
  var scrol = this.scrollOffset();
  item.style.left = (win[0] / 2) + scrol[0] - (xy.width / 2) + "px";
  item.style.top = (win[1] / 2) + scrol[1] - (xy.height / 2) + "px";
},
fullScreen: function(item) {
  item = $(item);
  var win = this.windowDimensions();
  var scrol = this.scrollOffset();
  item.style.height = scrol[1] + win[1] + "px";
},
windowDimensions: function() {
  var x, y;
  if (self.innerHeight) {
    // all except Explorer
    x = self.innerWidth;
    y = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    // Explorer 6 Strict Mode
    x = document.documentElement.clientWidth;
    y = document.documentElement.clientHeight;
  } else if (document.body) {
    // other Explorers
    x = document.body.clientWidth;
    y = document.body.clientHeight;
  }
  if (!x) x = 0;
  if (!y) y = 0;
  arrayWindowSize = new Array(x,y);
  return arrayWindowSize;
},
scrollOffset: function() {
  var x, y;
  if (self.pageYOffset) {
    // all except Explorer
    x = self.pageXOffset;
    y = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    // Explorer 6 Strict
    x = document.documentElement.scrollLeft;
    y = document.documentElement.scrollTop;
  } else if (document.body) {
    // all other Explorers
    x = document.body.scrollLeft;
    y = document.body.scrollTop;
  }
  if (!x) x = 0;
  if (!y) y = 0;
  arrayScrollOffset = new Array(x,y);
  return arrayScrollOffset;
}

Execute Event

// executes an event using the DOM element and event name (take out the throw $break to execute all of them)

execEvent: function(el, type) {
  Event.observers.each(function(item,index) {
    if (item[0] == el && item[1] == type) { item[2](); throw $break; }
  });
}

Get HTML

// a stupid function that gets a node's HTML, including the node itself

getHTML: function(node) {
  var div = document.createElement('DIV');
  div.appendChild(node.cloneNode(true));
  return div.innerHTML;
}

Insert Into Array

// insert item into array by specifying the array, insertion index - 1, and the item

insertArray: function(array, afterIndex, item) {
  var first = array.slice(0, afterIndex + 1);
  var second = array.slice(afterIndex + 1);
  first.push(item);
  return first.concat(second);
}

Get CSS Rule w/ Selector

// gets the rule for a specific CSS selector

getRule: function(selector) {
  var mysheet = document.styleSheets[0];
  var myrules = mysheet.cssRules ? mysheet.cssRules : mysheet.rules;
  for (i = 0; i < myrules.length; i++)
    if (myrules[i].selectorText.toLowerCase() == selector)
      return myrules[i];
  return;
}

childOfID

// moves upward in the tree until an ID or class name is found, returns element

childOfID: function(element, id, isClass) {
  element = $(element);
  do {
    if (isClass && element.hasClassName(id)) return element; 
    if (!isClass && element.id == id) return element;
  } while (element = $(element.parentNode));
  return false;
}