Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In OS X Lion, asking the Finder for the current selection, returns the wrong file

I've confirmed that this is only happening in Lion. I've uncovered what appears to be a bug where you need to focus a window twice in order for Finder to return the correct result over the scripting bridge or AppleScript. This only happens when opening a folder from the desktop or another space.

Here's how to duplicate:

  1. Close, hide or minimize all windows until the desktop is showing.
  2. Open any folder on the desktop, a new finder window will appear. Select any file in that finder window by clicking on it once.
  3. Open your AppleScript Editor and run the following script:

    tell application "Finder"
        return selection
    end tell
    

The script will return only the path to the folder on the desktop, not the selected file in the finder window.

If you refocus the window, or click on the file again, everything works as expected and the script returns the correct path.

This seems like a bug in Lion. Snow Leopard returns the correct path 100% of the time.

I've tried the following methods with no success:

  1. Using the scripting bridge and https://github.com/davedelong/BetterInfo/blob/master/Finder.h
  2. Using "System Events" apple script to click "Edit"->"Copy"
  3. Using AppleScript to get the current selection from the finder.

All seem to have have the same issue.

Am I missing something obvious or is this truly a problem with Lion?

like image 722
Levi Nunnink Avatar asked Dec 08 '11 18:12

Levi Nunnink


1 Answers

Wow, I can confirm (at least through applescript) most of what you describe. I was getting an empty list returned, not even the desktop path you mention. I was testing this using a script in the applescript menu. The only way I could get the selection from a fresh Finder window was like this...

tell application "System Events" to activate
tell application "Finder"
    activate
    set a to (get selection) as text
    display dialog a
end tell
like image 200
regulus6633 Avatar answered Oct 12 '22 01:10

regulus6633