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:
   1  
   2  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:
   1  
   2  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

   1  
   2  <?xml version="1.0" encoding="UTF-8"?>
   3  <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
   4  <plist version="1.0">
   5  <dict>
   6          <key>Label</key>
   7          <string>local.my-mac.rsnapshot-daily</string>
   8          <key>ProgramArguments</key>
   9          <array>
  10                  <string>/opt/local/bin/rsnapshot</string>
  11                  <string>daily</string>
  12          </array>
  13          <key>StartCalendarInterval</key>
  14          <dict>
  15                  <key>Hour</key>
  16                  <integer>3</integer>
  17          </dict>
  18  </dict>
  19  </plist>

Quick search with Quicksilver and EasyFind

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

   1  
   2  (*
   3  A script to quickly search files and folders using Quicksilver [1] and EasyFind [2].
   4  Written by Simon Dorfman, January 8, 2006. www.SimonDorfman.com
   5  
   6  Save this script to ~/Library/Application Support/Quicksilver/Actions/EasyFind.scpt
   7  You'll need to have "Enable access for assisstive devices" enabled under Universal Access in System Preferences.
   8  To use:
   9  	1. activate Quicksilver
  10  	2. type "." (i.e. hit the period key)
  11  	3. type the text you want to search for (i.e. httpd.conf)
  12  	4. press tab
  13  	5. start typing "EasyFind.scpt"
  14  	6. once Quicksilver finds "EasyFind.scpt", press return
  15  	
  16  [1] http://www.versiontracker.com/dyn/moreinfo/macosx/22549
  17  [2] http://www.versiontracker.com/dyn/moreinfo/macosx/11706
  18  *)
  19  
  20  using terms from application "Quicksilver"
  21  	on process text t
  22  		tell application "EasyFind" to activate
  23  		tell application "System Events"
  24  			tell process "EasyFind"
  25  				set value of text field 1 of window 1 to t --type text in search field
  26  				--uncomment your preferred search option, or save separate scripts with each option
  27  				click radio button 1 of radio group 1 of window 1 --"Files & Folders"
  28  				--click radio button 2 of radio group 1 of window 1 --"Only Files"
  29  				--click radio button 3 of radio group 1 of window 1 --"Only Folders"
  30  				--click radio button 4 of radio group 1 of window 1 --"File Contents"
  31  				click button 4 of window 1 --start search
  32  			end tell
  33  		end tell
  34  	end process text
  35  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.

   1  
   2  -- An applescript that resizes the front two windows of the active application side by side, filling the screen.
   3  -- Written by Simon Dorfman, January 4, 2006. www.SimonDorfman.com
   4  
   5  -- 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
   6  -- I use keyboard shortcut Control + Option + C to run it.  I think of the C as standing for "Compare".
   7  -- You'll need to have "Enable access for assisstive devices" enabled under Universal Access in System Preferences.
   8  
   9  --set screen dimension variables
  10  set menubarHeight to 22
  11  set screenWidth to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number
  12  set screenHeight to word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number
  13  --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:
  14  --set screenWidth to 1280
  15  --set screenHeight to 854
  16  {screenWidth, screenHeight}
  17  
  18  tell application "System Events"
  19  	set frontApp to name of first application process whose frontmost is true
  20  end tell
  21  
  22  --some apps are wacky and put the windows higher for some reason, adjust for this bug.
  23  if (frontApp is equal to "Finder" or frontApp is equal to "Microsoft Entourage") then
  24  	set menubarHeight to 44
  25  end if
  26  --leave room for the Excel Toolbar
  27  if (frontApp is equal to "Microsoft Excel") then
  28  	set menubarHeight to 55
  29  end if
  30  
  31  try
  32  	tell application frontApp
  33  		set bounds of window 1 to {0, menubarHeight, (screenWidth / 2), screenHeight}
  34  		set bounds of window 2 to {(screenWidth / 2), menubarHeight, screenWidth, screenHeight}
  35  	end tell
  36  on error the error_message number the error_number
  37  	display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
  38  end try

Play DVD from Hard Drive Shortcut

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

   1  
   2  -- a quick way to play a DVD that has been saved to the hard drive
   3  
   4  -- adapted from a script found in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297
   5  
   6  -- Instructions for use:
   7  -- 1. Paste this script into Script Editor and save as an application bundle to ~/Library/Scripts/Applications/Finder/Play DVD Folder
   8  -- 2. Control-click and drag it to the top of a finder window so it appears in every finder window.
   9  -- 3. Next, we'll give our script the DVD Player icon to make it's use more intuitive by entering these commands in Terminal:
  10  --     $ cd ~/Library/Scripts/Applications/Finder/Play\ DVD\ Folder.app/Contents/Resources/
  11  --     $ rm droplet.icns
  12  --     $ cp /Applications/DVD\ Player.app/Contents/Resources/app.icns droplet.icns
  13  --     $ touch ~/Library/Scripts/Applications/Finder/Play\ DVD\ Folder.app
  14  -- 4. In the Finder, drag a DVD Folder onto the "Play DVD Folder at the top of that finder window".
  15  -- 5. Enjoy the movie. :-)
  16  
  17  
  18  -- script was opened by click in toolbar
  19  on run
  20  	tell application "Finder"
  21  		try
  22  			set currFolder to (folder of the front window as string)
  23  		on error
  24  			display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
  25  		end try
  26  	end tell
  27  	CD_to(currFolder, false)
  28  end run
  29  
  30  -- script run by draging file/folder to icon
  31  on open (theList)
  32  	set newWindow to false
  33  	repeat with thePath in theList
  34  		set thePath to thePath as string
  35  		if not (thePath ends with ":") then
  36  			set x to the offset of ":" in (the reverse of every character of thePath) as string
  37  			set thePath to (characters 1 thru -(x) of thePath) as string
  38  		end if
  39  		CD_to(thePath, newWindow)
  40  		set newWindow to true -- create window for any other files/folders
  41  	end repeat
  42  	return
  43  end open
  44  
  45  -- open and play the movie
  46  on CD_to(theDir, newWindow)
  47  	try
  48  		tell application "DVD Player"
  49  			activate
  50  			if theDir ends with "VIDEO_TS:" then
  51  				set videoTS to theDir as alias
  52  			else
  53  				set videoTS to theDir & "VIDEO_TS:" as alias
  54  			end if
  55  			open VIDEO_TS videoTS
  56  			set viewer full screen to true
  57  			play dvd
  58  		end tell
  59  	on error the error_message number the error_number
  60  		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
  61  	end try
  62  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.

   1  
   2  -- Opens the current finder window folder in TextMate. Or open a file or folder in TextMate by dragging it onto this script.
   3  -- adopted from a script I found in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297
   4  
   5  -- Instructions for use:
   6  -- paste this script into Script Editor and save as an application to ~/Library/Scripts/Applications/Finder/open in TextMate
   7  -- run via the AppleScript Menu item (http://www.apple.com/applescript/scriptmenu/)
   8  -- Or better yet, Control-click and drag it to the top of a finder window so it appears in every finder window.
   9  -- Activate it by clicking on it or dragging a file or folder onto it.
  10  
  11  -- Another nice touch is to give the saved script the same icon as TextMate.
  12  -- To do this, in the finder, Get info (Command-I) of both TextMate and this saved script.
  13  -- Click the TextMate icon (it will highlight blue) and copy it by pressing Comand-C.
  14  -- Click on this script's icon and paste by pressing Command-V.
  15  
  16  -- Another way to give it the same icon as TextMate is to save the script as an application bundle (instead of an application),
  17  --  then copy the icon by entering these commands in Terminal:
  18  -- $ cd ~/Library/Scripts/Applications/Finder/open\ in\ TextMate.app/Contents/Resources/
  19  -- $ rm droplet.icns
  20  -- $ cp /Applications/TextMate.app/Contents/Resources/TextMate.icns droplet.icns
  21  -- $ touch ~/Library/Scripts/Applications/Finder/open\ in\ TextMate.app
  22  
  23  -- script was opened by click in toolbar
  24  on run
  25  	tell application "Finder"
  26  		try
  27  			set currFolder to (folder of the front window as string)
  28  		on error
  29  			display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
  30  		end try
  31  	end tell
  32  	CD_to(currFolder, false)
  33  end run
  34  
  35  -- script run by draging file/folder to icon
  36  on open (theList)
  37  	CD_to(theList, false)
  38  end open
  39  
  40  -- open the file/folder in TextMate
  41  on CD_to(theDir, newWindow)
  42  	try
  43  		set theDir to quoted form of POSIX path of theDir as string
  44  		do shell script "open -a TextMate " & theDir
  45  	on error the error_message number the error_number
  46  		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
  47  	end try
  48  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

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

Show/Hide Hidden Files

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

   1  
   2  -- found this script here: http://forums.macosxhints.com/showpost.php?p=251001&postcount=3
   3  -- i added the "delay 5" because after running script, finder didn't relaunch, maybe that's not needed on a faster machine
   4  -- it's for toggling invisible files on and off in the finder, use for when i need to copy .htaccess files via a GUI
   5  
   6  on run
   7  	set hiddenState to (do shell script "/usr/bin/defaults read com.apple.Finder AppleShowAllFiles")
   8  	try
   9  		if hiddenState = "0" or hiddenState contains "does not exist" then
  10  			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"
  11  			if button returned of showPrompt = "Show Hidden Files" then
  12  				tell application "Finder" to quit
  13  				do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles 1"
  14  				delay 5 --give finder time to quit so it can be relaunched below
  15  			else if button returned of showPrompt = "Cancel" then
  16  				return 0
  17  			end if
  18  		else if hiddenState = "1" then
  19  			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"
  20  			if button returned of showPrompt = "Hide Hidden Files" then
  21  				tell application "Finder" to quit
  22  				do shell script "/usr/bin/defaults write com.apple.Finder AppleShowAllFiles 0"
  23  				delay 5 --give finder time to quit so it can be relaunched below
  24  			else if button returned of showPrompt = "Cancel" then
  25  				return 0
  26  			end if
  27  		end if
  28  	on error theError
  29  		FinderCheck()
  30  		display alert theError as critical message "Changes may or may not have been made."
  31  	end try
  32  	FinderCheck()
  33  end run
  34  
  35  on FinderCheck()
  36  	tell application "System Events"
  37  		if (name of every process) does not contain "Finder" then
  38  			tell application "Finder" to launch
  39  		end if
  40  	end tell
  41  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.

   1  
   2  -- cd to the current finder window folder in iTerm. Or drag a folder onto this script to cd to that folder in iTerm.
   3  -- found this script in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297
   4  
   5  -- Instructions for use:
   6  -- paste this script into Script Editor and save as an application to ~/Library/Scripts/Applications/Finder/cd to in iTerm
   7  -- run via the AppleScript Menu item (http://www.apple.com/applescript/scriptmenu/)
   8  -- Or better yet, Control-click and drag it to the top of a finder window so it appears in every finder window.
   9  -- Activate it by clicking on it or dragging a folder onto it.
  10  
  11  -- Another nice touch is to give the saved script the same icon as iTerm.
  12  -- To do this, in the finder, Get info (Command-I) of both iTerm and this saved script.
  13  -- Click the iTerm icon (it will highlight blue) and copy it by pressing Comand-C.
  14  -- Click on this script's icon and paste by pressing Command-V.
  15  
  16  -- Another way to give it the same icon as iTerm is to save the script as an application bundle (instead of an application),
  17  --  then copy the icon by entering these commands in iTerm:
  18  -- $ cd ~/Library/Scripts/Applications/Finder/cd\ to\ in\ iTerm.app/Contents/Resources/
  19  -- $ rm droplet.icns
  20  -- $ cp /Applications/iTerm.app/Contents/Resources/iTerm.icns droplet.icns
  21  -- $ touch ~/Library/Scripts/Applications/Finder/cd\ to\ in\ iTerm.app
  22  
  23  -- script was opened by click in toolbar
  24  on run
  25  	tell application "Finder"
  26  		try
  27  			set currFolder to (folder of the front window as string)
  28  		on error
  29  			set currFolder to (path to desktop folder as string)
  30  		end try
  31  	end tell
  32  	CD_to(currFolder, false)
  33  end run
  34  
  35  -- script run by draging file/folder to icon
  36  on open (theList)
  37  	set newWindow to false
  38  	repeat with thePath in theList
  39  		set thePath to thePath as string
  40  		if not (thePath ends with ":") then
  41  			set x to the offset of ":" in (the reverse of every character of thePath) as string
  42  			set thePath to (characters 1 thru -(x) of thePath) as string
  43  		end if
  44  		CD_to(thePath, newWindow)
  45  		set newWindow to true -- create window for any other files/folders
  46  	end repeat
  47  	return
  48  end open
  49  
  50  -- cd to the desired directory in iterm
  51  on CD_to(theDir, newWindow)
  52  	set theDir to quoted form of POSIX path of theDir as string
  53  	tell application "iTerm"
  54  		activate
  55  		delay 1
  56  		-- talk to the first terminal 
  57  		tell the first terminal
  58  			try
  59  				-- launch a default shell in a new tab in the same terminal 
  60  				launch session "Default Session"
  61  			on error
  62  				display dialog "There was an error creating a new tab in iTerm." buttons {"OK"}
  63  			end try
  64  			tell the last session
  65  				try
  66  					-- cd to the finder window
  67  					write text "cd " & theDir
  68  				on error
  69  					display dialog "There was an error cding to the finder window." buttons {"OK"}
  70  				end try
  71  			end tell
  72  		end tell
  73  	end tell
  74  end CD_to
« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS