get selection (file name) in finder

Hi

I use Mountain Lion, last release.

When I open a finder window (anything) and want to get a file name via a selection, I never get the right result the first time I execute my script. Only the second time. Any hints or tips?

I use the following instruction:

tell application “Finder” to set obj to selection

Thanks and regards
Lazy

Hi,

that’s a bug (at least since Lion).
You can avoid this behavior by sending the activate command before retrieving the selection

Hi

I have been having same unpredictable behaviour. Have added activate, but still get times get selection returns an empty list (wrongly).

Wish Apple could fix this

Hello.

Try addressing the window, like

set mywin to Finder Window 1
set index of mywin to 1

for the hope that that will help to kind of make Finder “wake up”.

I also think that

get selection

may help.

This looks promising. First tests make me feel warm and fuzzy…Looks like I am getting more like expected behaviour. Will report back if it goes weird on me…

Thanks

I think every body that is on Mountain Lion should file it as a bug. If they can get away with it in Finder…

Still not woking right. If I select a document in a Finder window, then open new Finder window and select new documents, when I run script

tell application "Finder"
	activate
	set mywin to Finder window 1
	set index of mywin to 1
	set theSelection to selection
end tell

… it returns the selection in the background (original) Finder window. I was hoping your tweak would force the selection from the open frontmost Finder window.

But no joy!

Try this: this should tickle!


tell application "Finder"
	(get target of its Finder window 1)
end tell

Still behaving badly. When I open new Finder window and select in it, get selection still picks up documents selected in background Finder window.

Annoying bug this:)

If I click out of the newly created window then click back into it, the get selection works fine. Perhaps this action can be scripted or emulated to get around this bug…?

Christopher Stone posted these workarounds to the AppleScript mailing list:

tell application "Finder"
	set newWin to make new Finder window
	close newWin
	set sel to selection as alias list
end tell

OR

tell application "SystemUIServer" to activate
tell application "Finder"
	activate
	set sel to selection as alias list
end tell

Did this get worse in Mountain Lion? In Lion, this works for any finder window selection:

tell application "Finder"
	activate
	set sel to get selection as alias list
end tell

From Chris’s message: “The specific sequence you used to create the failure may or may not be reliably repeatable on Lion, but the bug exists nevertheless.” IOW, it’s intermittent.

Adam, I was not having luck with your suggestion. Opening a new window and selecting within it, then running the script, would return nil or the selection in background window.

Shane’s suggestion works for me, thanks (until further notice/testing):

tell application "SystemUIServer" to activate
tell application "Finder"
   activate
   set sel to selection as alias list
end tell

The window flashes very quickly as well which is a nice side effect of this script, as it gives immediate feedback that something is happening (I use Quicksilver to invoke the script - which sends the selected document(s) to my iPad via AirDoc Sender, a brilliant little app for transferring documents etc from Mac to iPad BTW).

Cheers

I wonder if you could try this Kiwi, and report back.


do shell script "open -b \"com.apple.finder\""

I have some hopes for it.

Hi McUsr

No good I am afraid.

Still selects the background selection when new Finder window is opened.

The SystemUIServer way seems to be the solution for me…I am happy with it as a workaround.

Cheers

Absolutly! Thanks for trying it!

You see, I really wondered how the open command worked, regarding how it do activate apps.

What SystemUIServer really do by becoming the foreground, is that it somehow forces Finder to refresh its internal datastructures.(Maybe it will work by activating any other app. I think the reason for using SystemUIServer is that it is such small, and easily available app.)
I was wondering if open would do the same, force a deactivation, then an activation of Finder.

I actually thought it would work.

Happy to trial stuff. Bottom line is Apple’s Finder needs a little makeover here…:slight_smile: