-
-
Save ged/855b631ccd82cac4b81e to your computer and use it in GitHub Desktop.
A little script for adding directories to TextMate2's Favorites from the command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'fileutils' | |
# A little script for adding TextMate2 Favorites via the command line. | |
# | |
# # Add the current directory as a favorite | |
# $ tm2fave.rb | |
# | |
# # Add Ruby source directories as favorites if they aren't already | |
# $ tm2fave.rb ~/source/ruby/* | |
# | |
# # Remove a directory from Favorites | |
# $ tm2fave.rb -r ~/source/ruby/scratchspace | |
# | |
# | |
# $Id$ | |
# | |
# Authors: | |
# - Michael Granger <ged@FaerieMUD.org> | |
# | |
# Improvements/bugfixes: | |
# - Curt Sellmer <sellmerfud@gmail.com> | |
# | |
FAVORITE_DIR = File.join( File.expand_path("~"), 'Library/Application Support/TextMate/Favorites' ) | |
include FileUtils | |
include FileUtils::Verbose if $VERBOSE | |
include FileUtils::DryRun if $DEBUG | |
# Ensure the favorites directory exists and is a directory | |
if !File.exist?( FAVORITE_DIR ) | |
mkdir_p( FAVORITE_DIR ) | |
elsif !File.directory?( FAVORITE_DIR ) | |
abort "Favorite directory #{FAVORITE_DIR} already exists, but isn't a directory!" | |
end | |
# Remove favorites instead with -r | |
removemode = ARGV.delete( '-r' ) | |
# Use either the arguments as the directories to favorite, or the current | |
# working directory if there weren't any arguments | |
targets = if ARGV.empty? | |
[ Dir.pwd ] | |
else | |
ARGV.to_a | |
end | |
def favorite( tgt ) | |
$stderr.puts "Favoriting %s" % [ tgt ] | |
link = File.join( FAVORITE_DIR, File.basename(tgt) ) | |
# Check for existing symlinks | |
if File.symlink?( link ) | |
old_tgt = File.readlink( link ) | |
# If the desired symlink already exists, that's okay | |
if old_tgt == tgt | |
$stderr.puts " already favorited." | |
return | |
# ...but if it points somewhere else, that's an error. | |
else | |
abort "A favorite with that name (-> #{old_tgt}) already exists!" | |
end | |
# If it's anything but a symlink, bail out | |
elsif File.exist?( link ) | |
abort "Can'overwrite existing #{link}!" | |
end | |
ln_s( tgt, link ) | |
end | |
def unfavorite( tgt ) | |
$stderr.puts "Un-favoriting %s" % [ tgt ] | |
link = File.join( FAVORITE_DIR, File.basename(tgt) ) | |
# Check for existing symlinks | |
if !File.symlink?( link ) | |
abort "Won't unlink non-symlink #{link}!" | |
end | |
remove( link ) | |
end | |
# Symlink each target directory into the favorites dir | |
targets.collect {|dir| File.expand_path(dir) }.each do |tgt| | |
if removemode | |
unfavorite( tgt ) | |
else | |
favorite( tgt ) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment