mimmicking up & down keystrokes in the Finder.

I’m trying to run an applescript that will select the next file/folder in the Finder window, normally I use up & down arrows, but I want to select files while my focus is on another application.

I know I can get the container of my selected file by

tell app “Finder”
container of (selection as alias)
end tell

and I can select the file by using the reveal command.
But I don’t know how to get the number position of my selection file, so I can tell it to select the file next to it.

And also, Is there a way to get selection of specific Finder window?

Hi fhill2,

Do you mean that you want to get the next item alphanumerically?

gl,
kel

Yes, alphanumerically :slight_smile:

This one may help :

tell application "Finder"
	set selectedFile to item 1 of (get selection) # as alias
	set selectedName to name of selectedFile
	set theFolder to container of selectedFile # as alias
	set allNames to name of files of theFolder
	# Here you may sort the list to fit your needs
	repeat with i from 1 to count allNames
		if item i of allNames is selectedName then
			set nextIndex to i + 1
			if nextIndex > (count allNames) then set nextIndex to 1
			(item nextIndex of allNames) as text
			select file result of theFolder
			exit repeat
		end if
	end repeat
end tell

As we stay speaking to the Finder I don’t know if it’s useful to coerce the file/folder references into aliases.

Yvan KOENIG (VALLAURIS, France) mercredi 23 avril 2014 18:11:09

Hi,

if you could live with the fact that the Finder is sorting case insensitive and AppleScript is sorting case sensitive,
this is a quite simple solution, it just increases the index number.


tell application "Finder"
	set selectedItems to selection
	if selectedItems is not {} then
		set selectedItem to item 1 of selectedItems
		set parentFolder to container of selectedItem
		set selectedIndex to index of selectedItem
		if selectedIndex < (count items of parentFolder) then
			set selectedIndex to selectedIndex + 1
		end if
		select item selectedIndex of parentFolder
	end if
end tell

Oops, I completely forgot the index property.

Yvan KOENIG (VALLAURIS, France) mercredi 23 avril 2014 20:33:37

Great, thanks for the answers!

This script doesn’t work on the desktop folder though (no idea why) although is irrelevent as I will not be using it for selecting files on my desktop.

However, I’ve encountered 2 problems.

1st problem

I’m using the List View mode in Finder (Command + 2 keystroke) here and revealing a file thats not on the top level of the directory will open a new window, with the window’s target set to the parent of the file. Which is not desired¨.

Is it possible to reveal/select the file in the same Finder window, regardless of it’s location in the ‘List View’ hierachal folder structure?

2nd problem

I’ve had to change the way I’ve sorted my files, so is there a way to select/reveal files corresponding to how they are sorted in the Finder window.
In my case, I have sorted my files by Spotlight comment column.

In my case, each file has a spotlight comment with an integer value.

How would I select/reveal the next/prev file by the closest integer stored in the spotlight comments field in the Finder?

:slight_smile:

I’m puzzled because here, Os X 10.9.2 running in French, when I run the script posted by StefanK with a file selected in a window in List mode, the file selected by the script is displayed in the original window.

Of course, if the Finder window was masked by an other window, it’s brought to front but it seems to be normal.

Yvan KOENIG (VALLAURIS, France) samedi 26 avril 2014 21:16:05

Say If my window’s target was my Home Folder, but I ran the script on a selected file which location is in my Music folder. (Still in Home Folder window)
Then it opens a new window.

Is this true on your system?

When I’m in such situation, if I ask the Finder to return its selection, I get the Finder reference to the subfolder containing the items, not a reference to the item.

tell application "Finder"
	get selection
	# when the home window is frontmost
	--> {folder "Music" of folder "¢¢¢¢¢¢¢¢¢¢" of folder "Users" of startup disk of application "Finder"}
	# if the Music window is frontmost
	--> {document file "(Disc 12) 05 - Regarde (Live A L'Hippodrome De Pantin _ 1981).mp3" of folder "Music" of folder "¢¢¢¢¢¢¢¢¢¢" of folder "Users" of startup disk of application "Finder"}
	select file "Macintosh HD:Users:¢¢¢¢¢¢¢¢¢¢:Music:(Disc 12) 04 - L'Homme En Habit Rouge (Album Version).mp3"
end tell

To be short, to get a reference to a selected item, the window containing the item must be open.
If the window remains open but is moved behind an other one, selecting an other item in it brings it to front.

If I have a selected item in a window and close the window, selecting an other item in it opens the window.

Given that, my understanding is that in your case, the window containing the items is not open when you try to select a new item but that it was open at front when you grabbed the reference to the originally selected file.

Yvan KOENIG (VALLAURIS, France) mardi 29 avril 2014 09:59:07

Sorry, but a bit confused here. I will try and explain the problem a bit better…

Please see image uploaded here.
http://tinypic.com/view.php?pic=2qvfls8&s=8

I have my Finder window (List View) set as target to “Home” Folder, and I have a file selected in a subfolder.

In this case, when I run the script I want the selected file to change from Folder 3 to Folder 4, without opening a new window.

I repeat that when you are in the situation displayed on your screenshot,
tell application “Finder”
get selection

doesn’t report a reference to the file possibly selected in the folder “Folder 4”
but a reference to “Folder 4” itself

To get a reference to the file selected in “Folder 4” and/or change the selection, “Folder 4” must be open which is why yo get it’s window appearing.
But in your description you forgot to say that in fact,
Folder 4 was open allowing you to select, say file1
then it was closed bringing to what we see on your screenshot.

What may be surprising is the fact that when Folder 4 is revealing its contents in list mode (black triangle pointing to the bottom), asking a script so deselect File1 and select File2 in Folder 4 doesn’t apply in the list window but open a new one.
Worse, File1 remains highlighted as selected in the original window and File2 is highlighted in the new window.

You may see a screenshot at :
https://app.box.com/s/dp3golzdofy8jdtxyv27

Yvan KOENIG (VALLAURIS, France) mercredi 30 avril 2014 16:52:14

Thanks for the reply!

So it seems this won’t work in list view, as I don’t think there is a workaround to this situation:
(Where the file is revealed in a new window, and not the original window)

And is there a way to select/reveal files corresponding to how they are sorted in the Finder window.
In my case, I have sorted my files by Spotlight comment column. In my case, each file has a spotlight comment with an number.

See here

http://tinypic.com/view.php?pic=hv80hu&s=8

Running the script will select the next file with “naming order” and not “comment order”, which is undesired here.

How would I select/reveal the next/prev file by the number stored in the spotlight comments field in the Finder?

One more time :
you can’t change the selection in a window which is not open !

If you try to do that, in every mode, the folder containing the file will open its window at front.
Of course your script may close it immediately but the process will be visible.

Yvan KOENIG (VALLAURIS, France) mercredi 30 avril 2014 21:41:21

I understand now, sorry for confusion.
Thanks for help on my thread. :slight_smile:

Just a question.

Is it really necessary to select a file to achieve your goals ?

I never do that to fit mines.
I would extract the list of the files embedded in the target folder then ask it to play them (or execute an other action upon them) one by one.

I searched in available resources and found no way to sort the contents of a folder according to spotlight comments.

Maybe ASObjC offer a way to achieve that but I’m unaware of that.

Yvan KOENIG (VALLAURIS, France) jeudi 1 mai 2014 11:23:33

I’m designing my DJ setup which selects next/prev file in Finder and performs an action. For example, load into deck, copy name to clipboard.

I would just use simply use the up and down arrow commands however…
this requires Finder to be in focus.

The select next/prev file command must behind the scenes , so I can have my focus set on iTunes while still selecting Finder files with my DJ MIDI controller.

But for everyday Finder use, you are correct, this script is completely unnesseccary…

Thanks for help again :slight_smile: I wish to visit France soon !