« Newer Snippets
Older Snippets »
50 total  XML / RSS feed 

Make iTunes arrows point to your own music, not the iTS

defaults write com.apple.iTunes invertStoreLinks -bool YES


This makes those little right-pointing arrows in iTunes point to your library, not the iTunes Store. If you want the iTunes Store, Option-click instead.

Convert plist files from binary to xml and back again

Enter these two commands into the terminal to toggle plist preferences files between XML and Binary

To convert a binary .plist file to XML format for editing, type this in the Terminal:
plutil -convert xml1 some_file.plist

To convert an XML .plist file to binary for use:
plutil -convert binary1 some_other_file.plist

XML or HTML Tag Parser

This handler is intended to accept a string input, and remove the contents of a specific tag within that sting input. This is great for getting string contents out of xml or html files. For example, if you want to get the title of an html document you call
tagParse(yourFileContents, "", ")
it would return the text between the two title tags.
on tagParse(thisText, openTag, closeTag)
        set new_text to ""
        set strlen to the length of thisText
        set taglen to (the length of openTag) - 1
        set closeTagLen to (the length of closeTag) - 1
        set stridx to 1
        repeat until (stridx + taglen) is greater than strlen
                set thisString to the contents of characters stridx through (stridx + taglen) of thisText as text
                log thisString
                if thisString is openTag then
                        set firstChar to stridx + taglen + 1
                        --display dialog "the title text starts at character " & firstChar
                        repeat until (stridx + closeTagLen) is greater than strlen
                                set thisString to the contents of characters stridx through (stridx + closeTagLen) of thisText as text
                                log thisString
                                if thisString is closeTag then
                                        return the contents of characters firstChar through (stridx - 1) of thisText as text
                                end if
                                set stridx to stridx + 1
                        end repeat
                end if
                set stridx to stridx + 1
        end repeat
        return "Error:: tag " & openTag & " not found."
end tagParse

Log items to tab delimited text file

this script is intended to be used to write logs to a tab delimited text file

logfilename is a string
theFields must be a list
logfilename is just the name of your log file

on logFile(theFields, logFileName)
        try
                tell application "Finder"
                        if not (file ((path to desktop folder as text) & logFileName) exists) then
                                set theLogFile to make file at folder (path to desktop folder as text)
                                set the name of theLogFile to logFileName
                                set myFile to open for access file ((path to desktop folder as text) & logFileName) with write permission
                        else
                                set myFile to open for access file ((path to desktop folder as text) & logFileName) with write permission
                        end if
                        set endOfFile to get eof myFile
                        repeat with eachField in theFields
                                set theFieldString to theFieldString & item eachField of theFields & tab
                        end repeat
                        write (theFieldString & return) to myFile starting at (endOfFile + 1)
                        close access myFile
                end tell
        on error err
                display dialog "log file error" & err giving up after 5
                try
                        close access ((path to desktop folder as text) & logFileName)
                end try
        end try
end logFile

Folder Maker

This handler creates a folder of name NameOnly to the location of thePath.
thePath is the path to the folder you want the folder to be created in
NameOnly is the name you want to give the folder

both handler inputs should be strings (e.g. Drive:folder:)
on folderMaker(thePath, NameOnly)
        tell application "Finder"
                if (folder (thePath & NameOnly) exists) is not true then
                        try
                                make new folder at folder (thePath) with properties {name:NameOnly}
                        on error the_error
                                display dialog the_error giving up after 10
                        end try
                end if
        end tell
end folderMaker

Remove Files older than X days

This script is intended to be in your script menu, or run as an applet.
It will ask you to choose a path to a folder that you want to clear out
files older then X days. It runs very fast because it uses the do shell
script command rather than the finder to get the time stamp of the
file. It's safe to use on network drives as well.
try
        set myFolder to (choose folder)
        set myTime to display dialog "Delete files older than how many days?" default answer "60"
        set myWildcard to display dialog "Enter a wildcard search string." default answer "PV*.txt"
        set myScript to "find \"" & POSIX path of myFolder & "\" -type f -name \"" & text returned of myWildcard & "\" -mtime +" & text returned of myTime & " -exec rm {} \\;"
        do shell script myScript
        display dialog "Deletion completed successfully"
on error err
        display dialog err
end try

Load Text File to string

The purpose of this handler is to load a text file from a posix path to a string

on loadTextFile(thepath)
        try
                set thestring to ""
                set theScript to ("cat " & "'" & thepath & "'")
                set thestring to do shell script theScript
                return thestring
        on error err
                log ("Cat text file error: " & err)
        end try
end loadTextFile

Is a file busy

Uses a do shell script to search for busy files

on isFileBusy(thePath)
--Tests to see if a file is in use
        try
                set myscript to "if ( lsof -Fp " & thePath & " | grep -q p[0-9]* ) then echo 'file is busy'; else echo 'not busy';fi"
                set myResult to do shell script myscript
                if myResult is "file is busy" then
                        return true
                else
                        return false
                end if
        on error err
                Display Dialog ("Error: isFileBusy " & err) giving up after 5
        end try
end isFileBusy

Mount Multiple Volumes or Disks

This snippet will mount required drives for your scripts. set the requiredDrives

set requiredDrives to {{theName:"drive1name", theMountScript:"afp://username:password@ip/drive1name"}, {theName:"drive2name", theMountScript:"afp://username:password@ip/drive2name"}} as list

set theDisks to list disks

repeat with i from 1 to (length of requiredDrives)
        if theDisks does not contain theName of item i of requiredDrives then
                mount volume theMountScript of item i of requiredDrives
        end if
end repeat

AppleScript String Parsing

This example will find the word "work" in the string "Bob went to work." and replace it with "the beach".

set myResult to snr("Bob went to work.", "work", "the beach")
display dialog myResult

(**** fast search and replace methods ****)
on snr(the_string, search_string, replace_string)
        return my list_to_string((my string_to_list(the_string, search_string)), replace_string)
end snr

on list_to_string(the_list, the_delim)
        my atid(the_delim)
        set the_string to (every text item of the_list) as string
        my atid("")
        return the_string
end list_to_string

on string_to_list(the_string, the_delim)
        my atid(the_delim)
        set the_list to (every text item of the_string) as list
        my atid("")
        return the_list
end string_to_list

on atid(the_delim)
        set AppleScript's text item delimiters to the_delim
end atid

Sort Apple Clipboard Contents

This applescript will convert anything in the finder clipboard to text, and then sort that text. Procedure is to copy an item. Run this script from the script menu, then paste.

set the clipboard to list_to_string(ASCII_Sort(string_to_listclass ktxt» of ((the clipboard as text) as record), return)), return)

on ASCII_Sort(my_list)
        --from apple
        set the index_list to {}
        set the sorted_list to {}
        repeat (the number of items in my_list) times
                set the low_item to ""
                repeat with i from 1 to (number of items in my_list)
                        if i is not in the index_list then
                                set this_item to item i of my_list as text
                                if the low_item is "" then
                                        set the low_item to this_item
                                        set the low_item_index to i
                                else if this_item comes before the low_item then
                                        set the low_item to this_item
                                        set the low_item_index to i
                                end if
                        end if
                end repeat
                set the end of sorted_list to the low_item
                set the end of the index_list to the low_item_index
        end repeat
        return the sorted_list
end ASCII_Sort


on snr(the_string, search_string, replace_string)
        return my list_to_string((my list_to_string(the_string, search_string)), replace_string)
end snr

on list_to_string(the_list, the_delim)
        my atid(the_delim)
        set the_string to (every text item of the_list) as string
        my atid("")
        return the_string
end list_to_string

on string_to_list(the_string, the_delim)
        my atid(the_delim)
        set the_list to (every text item of the_string) as list
        my atid("")
        return the_list
end string_to_list

on atid(the_delim)
        set AppleScript's text item delimiters to the_delim
end atid

Memcached StartupItem for Mac OS X


/Library/StartupItems/Memcached/StartupParameters.plist

{
  Provides        = ("Memcached");
  Description     = "Memcache Daemon";
  Uses            = ("Network");
  OrderPreference = "None";
}


/Library/StartupItems/Memcached/Memcached

#!/bin/bash
#
# /Library/StartupItems/Memcached/Memcached
#
# Script to startup memcached with OS X. Tested on 10.4 Tiger
#
# To enable, copy this file and StartupParameters.plist to
# /Library/StartupItems/Memcached and add "MEMCACHED=-YES-"
# to /etc/hostconfig ... You can then reboot or execute 
# "sudo StartItem start Memcached" from a terminal to start
# it up.
#
# I should mention that this file uses my preferred development 
# settings. You can edit them before copying this script over 
# or they can be overriden by creating a config file named 
# memcached.conf in /etc, /opt/etc, or /usr/local/etc with the 
# following contents (modified to suit, of course):
#
#   MEMCACHED_EXE=/usr/local/bin/memcached
#   MEMCACHED_PIDFILE=/var/run/memcached.pid
#   MEMCACHED_MAX_MEM=128
#   MEMCACHED_INTERFACE=127.0.0.1
#   MEMCACHED_PORT=1121
#   MEMCACHED_RUN_AS=nobody 
#
# ------------------------------------------------------------------- 
# NOTE: Memcached < 1.2.0 has issues specifying the interface via the
# '-l' switch on the Mac for some reason so if the service fails to 
# start with the following error:
#
#    bind(): Can't assign requested address
#    failed to listen
#
# then you will need to either upgrade to version 1.2.0+ or forgo
# specifying an interface to listen on by removing the references
# to A_INTERFACE on lines 65 and 75 of this script. 
# ------------------------------------------------------------------- 
#
# Tim Ferrell 
#

# Suppress the annoying "$1: unbound variable" error 
if [ -z $1 ] ; then
  echo "Usage: $0 [start|stop|restart] "
  exit 1
fi

# Source the common setup functions for startup scripts
test -r /etc/rc.common || exit 1
source /etc/rc.common

NAME=memcached
DESC="Memcached server"

# look for a config file name ${NAME}.conf in /etc, /opt/etc, 
# and /usr/local/etc ... in that order.
[ -r /etc/${NAME}.conf ] && source /etc/${NAME}.conf
[ -r /opt/etc/${NAME}.conf ] && source /opt/etc/${NAME}.conf
[ -r /usr/local/etc/${NAME}.conf ] && source /usr/local/etc/${NAME}.conf

# these are _my_ defaults... 
DAEMON="${MEMCACHED_EXE:=/usr/local/bin/memcached}"
A_PIDFILE="-P ${MEMCACHED_PIDFILE:=/var/run/memcached.pid}"
A_MAXMEM="-m ${MEMCACHED_MAX_MEM:=128}"
A_INTERFACE="-l ${MEMCACHED_INTERFACE:=127.0.0.1}"
A_PORT="-p ${MEMCACHED_PORT:=11211}"
A_RUNAS="-u ${MEMCACHED_RUN_AS:=nobody}"

StartService () 
{
  if [ "${MEMCACHED:=-NO-}" = "-YES-" ] && ! GetPID ${NAME} > /dev/null; then
    if [ -f /var/run/${NAME}.StartupItem ] ; then exit ; fi
    touch /var/run/${NAME}.StartupItem
    echo "Starting ${DESC}"
    ${DAEMON} -d ${A_PIDFILE} ${A_MAXMEM} ${A_INTERFACE} ${A_PORT} ${A_RUNAS} 
  fi
}

StopService ()
{
  if PID=$(GetPID ${NAME}); then
    echo "Stopping ${DESC}, (PID ${PID})"
    kill -TERM "${PID}"
    rm -f ${A_PIDFILE}
  else
    echo "${DESC} not running."
  fi
  rm -f /var/run/${NAME}.StartupItem
}

RestartService () { StopService; StartService; }

if test -x $DAEMON ; then
  RunService "$1"
else
  echo "Could not find ${DAEMON}!"
fi


Generate DSA Keys

// generates SSH DSA keys in ~/.ssh/

ssh-keygen -d

Change the hostname in Mac OS X

sudo scutil --set HostName servername.example.com

rsync syntax

rsync -avz --eahfs --progress --delete /source-dir-without-trailing-slash /destination-dir-with-trailing-slash


// explanation of switches
// -a, archive mode, equivalent to -rlptgoD which does things like recurse through all the dirs, preserves times, etc.
// -v, verbose mode
// -z, compress - makes the copy go faster. doesn't actually compress into a zip file
// --eahfs - (could also use -E, I think)
// --progress - show copy status of each item
// --delete, delete files on destination that aren't on source (sync)

Use SVN, Ant and Tomcat in XCode

I found Tim Fanelli's Blog ( http://www.timfanelli.com/cgi-bin/pyblosxom.cgi/j2ee/ ) entry quite helpful

In xcode, create a new empty project with the same dir any sources were checked out from.

Name the project with the same name used as the SVN module. NOTE: the .xcodeproj file should end up in the same directory as your build.xml file.

From the finder, dragged the src/ directory into the top level project name item in the project window.

Removed any unwanted items from the folder hierarchy now in xcode. For example, remove all generated build .jar files.

Project -> New Target. Chose 'Shell Script Target'.

In the new Target, double clicked on "Run Script". Under the "General" tab, entered, "/usr/bin/ant;"

Project -> New Build Phase -> New Copy Files Build Phase

Control + Click "Copy Files" (or right click if you have Mighty Mouse)

Add "Empty File in Project". Choose the dist/ directory. Create a file matching the output from your Ant build e.g. Project.war

Double click on the "Copy Files" phase. Type the absolute path to your tomcat deployment directory.

Project -> New Custom Executable. Enter "Tomcat" and make the path "/usr/bin/java".

Select the Arguments Tab enter:

-classpath /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/commons-logging-api.jar
-Dcatalina.home=/opt/tomcat
-Xmx128m
-Xms64m
-Xincgc
-Djava.awt.headless=true
org.apache.catalina.startup.Bootstrap -debug start


Under the Debugging tab, choose "Java Debugger".

Open Firefox in Safe Mode

// opens Firefox with extensions disabled. Also gives you the ability/option to reset Firefox to defaults.

// More info and code for other platforms can be found at http://kb.mozillazine.org/Safe_mode

/Applications/Firefox.app/Contents/MacOS/firefox -safe-mode

shell/vim .rc's for Japanese support (UTF-8)

Get Japanese (and other multibyte, ascii-unfriendly languages) working in the Terminal.

.inputrc (bash)
set convert-meta off
set meta-flag on
set output-meta on


.cshrc (tcsh)
set dspmbyte=utf8


.vimrc
:set enc=utf-8
:set fenc=utf-8


And don't forget 'ls -w' or 'ls -v' to display files and directories.

Preferences: Uncheck 'Emulation > Escape non-ASCII characters';

More at Apple Support - Topic: Displaying foreign characters in the Terminal command line.

add user in os x or os x server with NetInfo

Adding an OS X/OS X Server user with NetInfo. Antiquated by servermrg, but still functional.

niutil -create . /users/fred
niutil -createprop . /users/fred gid [groupID]
niutil -createprop . /users/fred uid [uniqueNumberOver1000]
niutil -createprop . /users/fred shell /bin/tcsh
niutil -createprop . /users/fred home /Users/fred
niutil -createprop . /users/fred realname "fred jones"
niutil -createprop . /users/fred passwd '*'
mkdir /Users/fred
mkdir /Users/fred/.ssh
chown -R fred /Users/fred
chgrp -R [groupID] /Users/fred
chmod 755 /Users/fred

Disable Dashboard

defaults write com.apple.dashboard mcx-disabled -boolean YES
« Newer Snippets
Older Snippets »
50 total  XML / RSS feed