Nik's Blog

Geekery, witty insights, software (of dubious quality) and more!

finder

Nik's Picks: XShelf

XShelf is a freeware application that provides a "shelf" in the Finder, much like the legacy NeXTStep shelf, or the shelf in Path Finder.

The shelf acts as a temporary place to hold files while you move and copy them in the Finder. So you can move one or more files to the shelf, navigate to the folder you want to move the files to, and then drag the files from the shelf to the folder. This is fantastic for smaller screens, or when you're gathering multiple files and copying or moving them to a single location.

As if that wasn't enough, XShelf also provides a command line utility to add items to the shelf, as well as a system-wide service to do the same.

It's a great, simple, utility that makes navigating the Finder tremendously easier. And it's free. What could be better?

You can learn more and download XShelf here.

AppleScript tricks: What to Do When the Frontmost Window Isn't Frontmost

The Problem: Scripts often target the current document window of an application. This is usually easily handled with something like...


Tell application "Finder" to get window 1

The problem with this approach is that it returns a useless window if the wrong kind of window is in front. In the Finder, this will often be a Get Info window, folder options window, or preferences. Some applications have special classes of windows to make this easier, but many do not. To find the frontmost window of these applications, you need to do this manually.

To make this work, you need to loop through your windows until you find one of the right type. You could do this by the name of the window (e.g. find Finder windows whose names don't contain "Finder Preferences"), but that requires you to catalog the names of every non-standard window, and can cause problems if, for example, you have a folder named "Finder Preferences."

A better approach is to identify a property that only the right kind of windows contain. So, for Finder windows, you can look for the folder of the window, as in...

Tell application "Finder"
set theFolder to (path to desktop folder as alias)
repeat with i from 0 to (count of windows)
try
get folder of window i
set theFolder to result as alias
exit repeat
end try
end repeat
end tell

In this code, the "try" handler gracefully handles windows without the "folder" property. Likewise, by setting theFolder to the Desktop to begin with, it also handles the situation where there are no open windows.

Using this technique, the following script will open the current Finder window in a new tab in the frontmost Terminal window. If there isn't a terminal window available, it opens a new window. The code gets pretty involved for the Terminal (including having to go to UI scripting to make a new tab), but it avoids the pitfalls of multiple open windows in each application, not all of which are useful folder or terminal windows.

on run
get quoted form of POSIX path of my getCurrentFolder()
my termScriptInNewTab("cd " & result)
end run

on termScriptInNewTab(cmd)
tell application "Terminal"
activate
set termWins to count of windows
set frontWin to 0
repeat with i from 1 to (count of windows)
try
set tabCount to count of tabs of window i
set frontWin to i
exit repeat
end try
end repeat
if frontWin is 0 then
do script cmd
else
set frontmost of window frontWin to true
tell application "System Events" to tell (first application process whose name is "Terminal") to keystroke "t" using {command down}
set xi to 0
repeat until (count of tabs of window 1) is (tabCount + 1)
if xi ≤ 4 then -- don't wait more than one second
delay 0.25
else
set xi to -1
exit repeat
end if
end repeat
if xi is not -1 then
do script cmd in (last tab of window 1)
else
do script cmd
end if
end if
end tell
end termScriptInNewTab

on getCurrentFolder()
tell application "Finder"
set theFolder to (path to desktop folder as alias)
repeat with i from 0 to (count of windows)
try
get folder of window i
set theFolder to result
exit repeat
end try
end repeat
return theFolder as alias
end tell
end getCurrentFolder

Extra credit: How can you adapt the termScriptInNewTab handler to run the command in the first tab that isn't busy, rather than a new tab?

Subscribe to RSS - finder

@inik

inik: I love Dropbox because it's indispensible on an iPhone. Was the top of the list of things I recommended to my bro. http://t.co/AnMAp1K4 >
inik: RT @alexismadrigal: Yup, @Salon cut the number of stories they post, increased their quality, and got a 40% traffic boost. http://t.co/X ... >
inik: RT @mikememoli: "The NFL: We're working on that whole brain injury thing." >
inik: I am a sucker for dog ads. Add Ponda Baba and it's like bliss. >
inik: Live stream only available to Verizon customers. Wonder how much that cost them. Anyone know? #superbowl >

Google+

I love Seth's quote at the top. I think that's my new motto.. ; )

Powered by Plu.sr
>
Griping about OS X Lion? Here's two nifty tools that replace a variety of poorly supported third party tools: Command-line and Automator access to video and audio conversion. Super easy to use, and very flexible and supports any format that Quicktime can encode/decode. (So Perian is a must-install if you want to handle DivX/3viX, etc.)

Yes, ffmpeg, Handbrake and...
>
Fix Google Reader's horrible new interface with this user script! Now it fits nicely on my MacBook's small screen. >
Happy 11/11/11 11:11:11! >
What makes this ad awesome is not the true-to-life irony, because the idea is hardly innovative, but rather the excellent execution. Reminds me a bit of that excellent Nutri-Grain spec commercial. Quick delivery, good actors, hit all the high notes. Love it. >