finder

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?

WhichApp?

Update: It turns out that when you set an applet as the default application for some particular file type, it doesn’t “stick.” So now I suppose I need to go to ApplescriptObjC, since AppleScript Studio is dead… Ugh.

Do you ever find yourself unintentionally opening an HTML file in Safari, when you really want to open it in BBEdit; or watch Photoshop open up when you just wanted to preview a JPEG?

WhichApp is a little applet that aims to fix this problem. When you open a file with WhichApp, it will present you with a configurable list of applications you can use to open the file. Pick your favorite app, and you’re in business!

Just double click on the applet to build your list of applications. Get Info on a file, choose “Other…” as the default application, and then select WhichApp. Now every time you open that file, you can pick which program ought to open it! Change the setting for all files of that type, and you can avoid the frustration of opening the wrong program every time you open that kind of file!

And, yes, you can duplicate WhichApp and rename it as many times as you want so you can have different selection lists for different file types.

WhichApp was developed on Snow Leopard and hasn’t been tested anywhere else. It’s just an AppleScript, though, so it should work everywhere.

I have disabled comments due to an overwhelming amount of comment spam, that I cannot seem to stop, no matter how hard I try.

Syndicate content