Never been to DZone Snippets 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

About this user

Simon Dorfman http://www.SimonDorfman.com/

« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS 

Transfer Wordpress MySQL database to new webhost

// Transfer Wordpress MySQL database to new webhost in 3 steps (assumes: A. files have already been copied over, B. database created on new server, C. database user created on new server, D. wp-config.php updated on new server)
// 1. login to old host via ssh, run this command:
mysqldump -h DB_HOST -u DB_USER -p DB_NAME > dump.sql

// replace DB_HOST, DB_USER, & DB_NAME with info from the local wp-config.php file
// when asked for password, enter DB_PASSWORD from the local wp-config.php file

// 2. copy dump.sql to your new host using sftp

// 3. login go new host via ssh, run this command:
mysql -h DB_HOST -u DB_USER -p DB_NAME < dump.sql

// replace DB_HOST, DB_USER, & DB_NAME with info from the local wp-config.php file
// when asked for password, enter DB_PASSWORD from the local wp-config.php file

// adopted from this page: http://technosailor.com/2007/04/06/wordpress-faq-how-do-i-move-my-blog-to-a-new-host/

Use Launchd to run Rsnapshot daily

// run rsnapshot daily at 3am

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>local.my-mac.rsnapshot-daily</string>
        <key>ProgramArguments</key>
        <array>
                <string>/opt/local/bin/rsnapshot</string>
                <string>daily</string>
        </array>
        <key>StartCalendarInterval</key>
        <dict>
                <key>Hour</key>
                <integer>3</integer>
        </dict>
</dict>
</plist>

Quick search with Quicksilver and EasyFind

// A script to quickly search files and folders using Quicksilver and EasyFind.

(*
A script to quickly search files and folders using Quicksilver [1] and EasyFind [2].
Written by Simon Dorfman, January 8, 2006. www.SimonDorfman.com

Save this script to ~/Library/Application Support/Quicksilver/Actions/EasyFind.scpt
You'll need to have "Enable access for assisstive devices" enabled under Universal Access in System Preferences.
To use:
	1. activate Quicksilver
	2. type "." (i.e. hit the period key)
	3. type the text you want to search for (i.e. httpd.conf)
	4. press tab
	5. start typing "EasyFind.scpt"
	6. once Quicksilver finds "EasyFind.scpt", press return
	
[1] http://www.versiontracker.com/dyn/moreinfo/macosx/22549
[2] http://www.versiontracker.com/dyn/moreinfo/macosx/11706
*)

using terms from application "Quicksilver"
	on process text t
		tell application "EasyFind" to activate
		tell application "System Events"
			tell process "EasyFind"
				set value of text field 1 of window 1 to t --type text in search field
				--uncomment your preferred search option, or save separate scripts with each option
				click radio button 1 of radio group 1 of window 1 --"Files & Folders"
				--click radio button 2 of radio group 1 of window 1 --"Only Files"
				--click radio button 3 of radio group 1 of window 1 --"Only Folders"
				--click radio button 4 of radio group 1 of window 1 --"File Contents"
				click button 4 of window 1 --start search
			end tell
		end tell
	end process text
end using terms from

Resize Front Two Windows Side by Side, Filling Screen

// An applescript that resizes the front two windows of the active application side by side, filling the screen.

-- An applescript that resizes the front two windows of the active application side by side, filling the screen.
-- Written by Simon Dorfman, January 4, 2006. www.SimonDorfman.com

-- Save this script to ~/Library/Scripts/Applications/Run from QuickSilver Triggers, Don't move these/Front 2 Windows Side by Side - Control + Option + C.scpt
-- I use keyboard shortcut Control + Option + C to run it.  I think of the C as standing for "Compare".
-- You'll need to have "Enable access for assisstive devices" enabled under Universal Access in System Preferences.

--set screen dimension variables
set menubarHeight to 22
set screenWidth to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number
set screenHeight to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number
--if you plan to use this on just one computer where the screen dimensions won't change, this script will run faster if you just hard code your screen resolution with these two lines:
--set screenWidth to 1280
--set screenHeight to 854
{screenWidth, screenHeight}

tell application "System Events"
	set frontApp to name of first application process whose frontmost is true
end tell

--some apps are wacky and put the windows higher for some reason, adjust for this bug.
if (frontApp is equal to "Finder" or frontApp is equal to "Microsoft Entourage") then
	set menubarHeight to 44
end if
--leave room for the Excel Toolbar
if (frontApp is equal to "Microsoft Excel") then
	set menubarHeight to 55
end if

try
	tell application frontApp
		set bounds of window 1 to {0, menubarHeight, (screenWidth / 2), screenHeight}
		set bounds of window 2 to {(screenWidth / 2), menubarHeight, screenWidth, screenHeight}
	end tell
on error the error_message number the error_number
	display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try

Play DVD from Hard Drive Shortcut

// a quick way to play a DVD that has been saved to the hard drive

-- a quick way to play a DVD that has been saved to the hard drive

-- adapted from a script found in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297

-- Instructions for use:
-- 1. Paste this script into Script Editor and save as an application bundle to ~/Library/Scripts/Applications/Finder/Play DVD Folder
-- 2. Control-click and drag it to the top of a finder window so it appears in every finder window.
-- 3. Next, we'll give our script the DVD Player icon to make it's use more intuitive by entering these commands in Terminal:
--     $ cd ~/Library/Scripts/Applications/Finder/Play\ DVD\ Folder.app/Contents/Resources/
--     $ rm droplet.icns
--     $ cp /Applications/DVD\ Player.app/Contents/Resources/app.icns droplet.icns
--     $ touch ~/Library/Scripts/Applications/Finder/Play\ DVD\ Folder.app
-- 4. In the Finder, drag a DVD Folder onto the "Play DVD Folder at the top of that finder window".
-- 5. Enjoy the movie. :-)


-- script was opened by click in toolbar
on run
	tell application "Finder"
		try
			set currFolder to (folder of the front window as string)
		on error
			display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
		end try
	end tell
	CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
	set newWindow to false
	repeat with thePath in theList
		set thePath to thePath as string
		if not (thePath ends with ":") then
			set x to the offset of ":" in (the reverse of every character of thePath) as string
			set thePath to (characters 1 thru -(x) of thePath) as string
		end if
		CD_to(thePath, newWindow)
		set newWindow to true -- create window for any other files/folders
	end repeat
	return
end open

-- open and play the movie
on CD_to(theDir, newWindow)
	try
		tell application "DVD Player"
			activate
			if theDir ends with "VIDEO_TS:" then
				set videoTS to theDir as alias
			else
				set videoTS to theDir & "VIDEO_TS:" as alias
			end if
			open VIDEO_TS videoTS
			set viewer full screen to true
			play dvd
		end tell
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
	end try
end CD_to

Open current Finder window folder in TextMate

// Opens the current Finder window folder in TextMate. Or open a file or folder in TextMate by dragging it onto this script.

-- Opens the current finder window folder in TextMate. Or open a file or folder in TextMate by dragging it onto this script.
-- adopted from a script I found in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297

-- Instructions for use:
-- paste this script into Script Editor and save as an application to ~/Library/Scripts/Applications/Finder/open in TextMate
-- run via the AppleScript Menu item (http://www.apple.com/applescript/scriptmenu/)
-- Or better yet, Control-click and drag it to the top of a finder window so it appears in every finder window.
-- Activate it by clicking on it or dragging a file or folder onto it.

-- Another nice touch is to give the saved script the same icon as TextMate.
-- To do this, in the finder, Get info (Command-I) of both TextMate and this saved script.
-- Click the TextMate icon (it will highlight blue) and copy it by pressing Comand-C.
-- Click on this script's icon and paste by pressing Command-V.

-- Another way to give it the same icon as TextMate is to save the script as an application bundle (instead of an application),
--  then copy the icon by entering these commands in Terminal:
-- $ cd ~/Library/Scripts/Applications/Finder/open\ in\ TextMate.app/Contents/Resources/
-- $ rm droplet.icns
-- $ cp /Applications/TextMate.app/Contents/Resources/TextMate.icns droplet.icns
-- $ touch ~/Library/Scripts/Applications/Finder/open\ in\ TextMate.app

-- script was opened by click in toolbar
on run
	tell application "Finder"
		try
			set currFolder to (folder of the front window as string)
		on error
			display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
		end try
	end tell
	CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
	CD_to(theList, false)
end open

-- open the file/folder in TextMate
on CD_to(theDir, newWindow)
	try
		set theDir to quoted form of POSIX path of theDir as string
		do shell script "open -a TextMate " & theDir
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
	end try
end CD_to

Keyboard shortcut for the green "zoom" button

// Ever since I started using OS X as my main OS (back at version 10.1), I kept wishing for a keyboard shortcut for the green button at the top left of each window. I wanted a quick way to maximize a window. Sadly, such a shortcut never came into being. I finally took the time to write an applescript to do the job. You can paste this script into Script Editor and save it in your ~/Library/Scripts/ folder. I saved my script as "Click Green Maximize Button of Frontmost Window.scpt". I then used QuickSilver's Triggers feature to assign the keyboard shortcut Control-Option-Z to run the script. I picked that key combination because it's easy to press. I remember it by thinking of the Z as standing for Zoom. Get QuickSilver here: http://www.versiontracker.com/dyn/moreinfo/macosx/22549

tell application "System Events"
	if UI elements enabled then
		set FrontApplication to (get name of every process whose frontmost is true) as string
		tell process FrontApplication
			click button 2 of window 1
			--button 2 is the green "zoom" button for all applications
			--window 1 is always the frontmost window.
		end tell
	else
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			display dialog "UI element scripting is not enabled. Check 'Enable access for assistive devices'"
		end tell
	end if
end tell

Show/Hide Hidden Files

// for toggling invisible files on and off in the finder

-- found this script here: http://forums.macosxhints.com/showpost.php?p=251001&postcount=3
-- i added the "delay 5" because after running script, finder didn't relaunch, maybe that's not needed on a faster machine
-- it's for toggling invisible files on and off in the finder, use for when i need to copy .htaccess files via a GUI

on run
	set hiddenState to (do shell script "/usr/bin/defaults read com.apple.Finder AppleShowAllFiles")
	try
		if hiddenState = "0" or hiddenState contains "does not exist" then
			set showPrompt to display alert "Are you sure you want to show all hidden files?" message "The Finder will quit and relaunch afterwards." buttons {"Cancel", "Show Hidden Files"} default button "Show Hidden Files"
			if button returned of showPrompt = "Show Hidden Files" then
				tell application "Finder" to quit
				do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles 1"
				delay 5 --give finder time to quit so it can be relaunched below
			else if button returned of showPrompt = "Cancel" then
				return 0
			end if
		else if hiddenState = "1" then
			set showPrompt to display alert "Are you sure you want to hide all hidden files?" message "The Finder will quit and relaunch afterwards." buttons {"Cancel", "Hide Hidden Files"} default button "Hide Hidden Files"
			if button returned of showPrompt = "Hide Hidden Files" then
				tell application "Finder" to quit
				do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles 0"
				delay 5 --give finder time to quit so it can be relaunched below
			else if button returned of showPrompt = "Cancel" then
				return 0
			end if
		end if
	on error theError
		FinderCheck()
		display alert theError as critical message "Changes may or may not have been made."
	end try
	FinderCheck()
end run

on FinderCheck()
	tell application "System Events"
		if (name of every process) does not contain "Finder" then
			tell application "Finder" to launch
		end if
	end tell
end FinderCheck

Open current Finder window in new iTerm tab

// cd to the current finder window folder in iTerm. Or drag a folder onto this script to cd to that folder in iTerm.

-- cd to the current finder window folder in iTerm. Or drag a folder onto this script to cd to that folder in iTerm.
-- found this script in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297

-- Instructions for use:
-- paste this script into Script Editor and save as an application to ~/Library/Scripts/Applications/Finder/cd to in iTerm
-- run via the AppleScript Menu item (http://www.apple.com/applescript/scriptmenu/)
-- Or better yet, Control-click and drag it to the top of a finder window so it appears in every finder window.
-- Activate it by clicking on it or dragging a folder onto it.

-- Another nice touch is to give the saved script the same icon as iTerm.
-- To do this, in the finder, Get info (Command-I) of both iTerm and this saved script.
-- Click the iTerm icon (it will highlight blue) and copy it by pressing Comand-C.
-- Click on this script's icon and paste by pressing Command-V.

-- Another way to give it the same icon as iTerm is to save the script as an application bundle (instead of an application),
--  then copy the icon by entering these commands in iTerm:
-- $ cd ~/Library/Scripts/Applications/Finder/cd\ to\ in\ iTerm.app/Contents/Resources/
-- $ rm droplet.icns
-- $ cp /Applications/iTerm.app/Contents/Resources/iTerm.icns droplet.icns
-- $ touch ~/Library/Scripts/Applications/Finder/cd\ to\ in\ iTerm.app

-- script was opened by click in toolbar
on run
	tell application "Finder"
		try
			set currFolder to (folder of the front window as string)
		on error
			set currFolder to (path to desktop folder as string)
		end try
	end tell
	CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
	set newWindow to false
	repeat with thePath in theList
		set thePath to thePath as string
		if not (thePath ends with ":") then
			set x to the offset of ":" in (the reverse of every character of thePath) as string
			set thePath to (characters 1 thru -(x) of thePath) as string
		end if
		CD_to(thePath, newWindow)
		set newWindow to true -- create window for any other files/folders
	end repeat
	return
end open

-- cd to the desired directory in iterm
on CD_to(theDir, newWindow)
	set theDir to quoted form of POSIX path of theDir as string
	tell application "iTerm"
		activate
		delay 1
		-- talk to the first terminal 
		tell the first terminal
			try
				-- launch a default shell in a new tab in the same terminal 
				launch session "Default Session"
			on error
				display dialog "There was an error creating a new tab in iTerm." buttons {"OK"}
			end try
			tell the last session
				try
					-- cd to the finder window
					write text "cd " & theDir
				on error
					display dialog "There was an error cding to the finder window." buttons {"OK"}
				end try
			end tell
		end tell
	end tell
end CD_to
« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS