Finder: Symlink This Window to That One

Symlinks! They’re not just for Christmas. They are, in fact, just like aliases, enabling you to litter your hard drive with shortcuts in no time flat. You can make them in the terminal with the “ln -s” command, but if you’ve got the windows open in the Finder, hell! Why not use ‘em?

This little baby uses the front two Finder windows to figure out the source and destination. Just be careful that they are the front two windows, or you may get unexpected – perhaps terrifying – results. I took the time to build in a confirmation dialog; just edit the code if you feel like living dangerously.

The source code dances a merry jig, over yonder.

Download Symlink-this-window-to-that-one.rb.zip.

#!/usr/bin/ruby

# rb-appscript script by Ian Haigh (http://ianhaigh.com/)
# Thu Dec 17 16:50:08 EST 2009

# Looks at the front two Finder windows, and makes a symlink from the first
# window to the second. If there's something selected in the first window, it
# will loop through all the items and make them symlinks in the second.
# Otherwise, it will figure out the target of the first window, and symlink
# that.	Just make sure that the SOURCE WINDOW is the frontmost, and the
# destination window is next in the stacking order.

require 'appscript'
require "osax"
sa = OSAX.osax("standardadditions")

#  set "paranoid" to false if you don't want to confirm the symlink
PARANOID = true

finder = Appscript.app('Finder.app')

if finder.Finder_windows.get.length < 2 then
	sa.display_alert("I need two windows open: frontmost for source, the second for the destination.")
	exit
end

# I can't figure out how to get a bunch of files' paths all at once, without
# having to revert to this "collect" hack
source = finder.selection.get(:result_type => :alias).collect { |item| item.path }

if source.empty?
	# get the front window target instead
	source << finder.Finder_windows.get[0].target.get(:result_type => :alias).path
end

if PARANOID
	source_window = finder.Finder_windows.get[0].target.name.get
	dest_window = finder.Finder_windows.get[1].target.name.get

	plural = (source.length == 1) ? "" : "s"
	confirm_string = %Q{About to symlink #{source.length} item#{plural} from “#{source_window}” to “#{dest_window}”. You wanna go ahead?}

	begin
		result = sa.display_dialog(
		confirm_string,
		:buttons => %w{Cancel OK},
		:default_button => "OK"
		)
	rescue Exception => e
		exit
	end

	# exit unless result[:button_returned] == "OK"
end

destination = finder.Finder_windows.get[1].target.get(:result_type => :alias).path

source.each do |item|
	full_path = File.join(destination, File.basename(item))

	# printf("File.symlink(%s, %s)\n", item, full_path)
	File.symlink(item, full_path)
end
This entry was posted in finder. Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • What’s this all about then?

    rb-appscript is a powerful way to automate your Mac. It's similar to Applescript but it's built in Ruby, which means you get to leverage that language's powerful syntax and libraries. Giving you more time to make toasted sandwiches. You want long-winded explanations? We got those.

    Please note that these scripts are by no means bulletproof, and are intended to provide examples of application scripting. I take no resposibility for any loss of data or mishaps; you download and run everything at your own risk. That said, I use all of these scripts in a production environment every day (well – more accurately, I use some of these scripts in a production environment most days).