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

« Newer Snippets
Older Snippets »
Showing 11-12 of 12 total

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 11-12 of 12 total