Is there any way to determine the currently active window, or a folder, in the Finder? I need this to determine, in some sense, an appropriate "default" location in which to do some particular things in my app.
Actually, does this question even make sense? Does this concept of a "currently active Finder window/folder" even exist in the first place? If it does not, I kindly ask how to get the currently selected Finder item.
Yes, the concept of the currently active Finder window does exist, as well as the currently selected item.
For example, the following AppleScript gets the selection
which is the current selection in the frontmost window. Since this returns a list of files or folders even if there is a single item, the next line gets the first item out of that list (after making sure that the count of the list is greater than 0). You can then ask the Finder for the container window
of the selected item, which will return a Finder window
object.
tell application "Finder"
set selectedItems to selection
if ((count of selectedItems) > 0) then
set selectedItem to (item 1 of selectedItems) as alias
container window of selectedItem
end if
end tell
I'm pretty sure the code sidyll posted will work okay in 10.5 and earlier, but it errors out in 10.6 due to the inevitable changes and quirkiness that AppleScript seems to have from one version of OS X to the next.
[EDIT] Actually, I just figured out what's going on. I usually have the Finder's Inspector window open all the time (the dynamic Get Info window you get through Command-Option-i), the upper right panel in the image below:
That image shows 3 different classes of windows:
1) The upper left, a Get Info window, is an information window
, which inherits from the generic window
class.
2) The upper right, an Inspector window, is a plain window
.
3) The lower image shows a Finder window
, which inherits from the generic window
class.
If I run the following script with the setup of windows shown above:
tell app "Finder"
every window
end tell
it returns the following result:
{window "mdouma46 Info" of application "Finder", information window "mdouma46 Info" of application "Finder", Finder window id 1141 of application "Finder"}
So, what was happening in my case is that, since the Inspector window is a floating utility panel, if it's currently being shown, asking the Finder for window 1
will always return the Inspector panel, since it's always floating in front of the other windows.
So the error I was getting when running the code was:
error "Can’t make «class fvtg» of window 1 of application \"Finder\" into type alias." number -1700 from «class fvtg» of window 1 to alias
(In other words, the Inspector window, a plain window
, doesn't have the FileViewer target (fvtg
) property; only Finder window
s do).
So, your code will work fine as long as the user doesn't have the Inspector window, the Preferences window, or a Get Info window that is frontmost. By changing window
to Finder window
, though, you can make sure that you only look at the file viewer windows that have the target
property.
So, like this:
NSDictionary *errorMessage = nil;
NSAppleScript *script = [[[NSAppleScript alloc] initWithSource:
@"tell application \"Finder\"\n"
" if ((count of Finder windows) > 0) then\n"
" return (POSIX path of (target of Finder window 1 as alias))\n"
"end if\n"
"end tell"] autorelease];
if (script == nil) {
NSLog(@"failed to create script!");
return nil;
}
NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];
if (result) {
// POSIX path returns trailing /'s, so standardize the path
NSString *path = [[result stringValue] stringByStandardizingPath];
return path;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With