Cannot search in Downloads Folder

I am trying to move some files out of the Downloads folder to another folder inside the Documents folder.
The file selection does not find anything. If I try to do a find by file extension in the Finder through the user interface, the files also don’t show up. Is the Downloads folder not filterable/searchable ?
The script is below :

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- Get current user's name
tell application "System Events"
	set currentUser to (name of current user)
end tell

tell application "Finder"
	open folder "Downloads" of folder currentUser of folder "Users" of startup disk
	set selectedFiles to select (file whose its name ends with "lha")
end tell

Many thanks

danieldlds. There is a path to command that returns the documents and downloads folders of the current user. This command is part of Standard Additions scripting addition and, as a general rule, is best used outside a Finder tell statement.

Files in Finder have a name extension property which can be used in a whose clause to get files with a particular extension. It’s important to note that the extension has to be recognized by Finder as a valid extension. If it’s not, Finder returns nothing.

The following version of your script worked on my Sonoma computer:

set downloadFolder to path to downloads folder -- an alias

tell application "Finder"
	set theFiles to every file in downloadFolder whose name extension is "txt"
	select theFiles
end tell

To move files as you want, I would suggest the following. I tested it without issue with TXT files on my Sonoma computer:

set sourceFolder to (path to downloads folder) -- an alias
set targetFolder to ((path to documents folder as text) & "Test Folder" & ":") as alias -- returns an error if the folder doesn't exist

tell application "Finder"
	set theFiles to every file in sourceFolder whose name extension is "txt"
	move theFiles to targetFolder
end tell

If the sample code from @peavine doesn’t work on your Mac, perhaps there is a permission issue? The downloads folder requires permission for an app to access it, so depending from what app context your script code runs, macOS may be blocking it. I don’t remember off the top of my head how you can make sure that running a script will request permission, but that should happen by default, I think?

Hi @peavine .
Your code worked.
Thanks a lot

1 Like

Your script fails because this line does not do what you think it does

	set selectedFiles to select (file whose its name ends with "lha")

As its name suggests, the select command in the Finder selects items. However, this is a command, not a function, so it does NOT pass the selected files back to your script, and the selectedFiles variable is never set to anything.

I’d go further and suggest that selecting the files is actually the wrong way to go anyway. Much better to directly reference the files and do what you want to them (e.g. move them to a different location). No need to visually select them at all:

tell application "Finder"
	set FilesToMove to every file of (path to downloads folder) whose name extension is "lha"
	move FilesToMove to folder "Subfolder Name" of (path to downloads folder)
end tell

this script directly references the files and moves them.

Thanks for your explanation @Camelot . I only have one question : why finder does not show anything when i filter it like in the attached screen shot ?

Just to make sure: can you also post a screenshot that demonstrates that files with the lha extension actually do exist in this folder?

Sure. Here goes:
Screenshot 2024-06-24 at 00.48.47

Interesting. And does Finder find it if you search just by name without any other conditions, for example:



Then I assume something is going wrong with Spotlight (which is notoriously unreliable). The volume isn’t indexed for some reason?

As a test, try other search utilities such as EasyFind and Find Any File - will they find your file?