Sonoma Breaks AppleScript for Finder Search/Find

macOS Sonoma broke my AppleScript that sets the criteria for Finder search.


BACKGROUND

Finder defaults to search "by Kind". I don’t want that. I want it to default to "Name begins with".


Finder’s SearchCriteria dictionary is this file:


/System/Library/CoreServices/Finder.app/Contents/Resources/default_smart.plist


You could modify the key in this file. However, no matter what you do — quit Finder, use sudo in Terminal, etc. — macOS tells you that you do not have permission to mess with that file. You can’t even replace this default_smart.plist with your saved version.


The alternative is an AppleScript. Until Sonoma, this worked:


tell application "System Events"

tell process "Finder"

click menu item "Find" of menu "File" of menu bar 1

tell pop up button 1 of group 2 of splitter group 1 of window 1

click

delay 0.05

click menu item "Name" of menu 1

end tell


tell pop up button 2 of group 2 of splitter group 1 of window 1

click

delay 0.5

click menu item "begins with" of menu 4

end tell


end tell


end tell


But Sonoma broke the last line, returning this error message:

System Events got an error: 

Can’t get menu 1 of pop up button 2 of group 2 of splitter group 1 of window 1 of process "Finder". Invalid index. 


Changing the index number has no effect. Does anyone have a solution to this problem?


Thanks.

Mac Studio (2022)

Posted on Dec 30, 2023 3:20 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 3, 2024 11:53 PM

This AppleScript is slower by a second, but it works!


It turns out that Shift-Tab cycles through all of the control buttons in a Finder search window. The macOS key code for Shift-Tab is 48.

This includes any shortcuts you might have atop the Finder window menu bar. So, in my case, it takes 20 presses of Shift-Tab to arrive at the search criteria popup, because I have 11 Finder shortcut buttons in mine.


That means you need to adjust the number of repeats in the first loop here accordingly.


on run
	tell application "Finder"
		activate
		tell application "System Events"
			keystroke "f" using command down # press Cmd-F
			delay 0.08
			repeat 20 times
				key code 48 using shift down # shift-tab
				delay 0.08
			end repeat
			key code 125 # down arrow (to activate the first criterion popup)
			delay 0.2
			key code 126 # up arrow (to choose the "Name" operator)
			delay 0.1
			key code 36 # return (to accept the "Name" operator)
			delay 0.1
			key code 48 using shift down # shift-tab
			delay 0.4
			repeat 3 times
				key code 125 # down arrow (to choose the "Begins with" operator)
				delay 0.4
			end repeat
			delay 0.25
			key code 36 # return
		end tell
	end tell
end run

Similar questions

8 replies
Question marked as Top-ranking reply

Jan 3, 2024 11:53 PM in response to Al Hatch

This AppleScript is slower by a second, but it works!


It turns out that Shift-Tab cycles through all of the control buttons in a Finder search window. The macOS key code for Shift-Tab is 48.

This includes any shortcuts you might have atop the Finder window menu bar. So, in my case, it takes 20 presses of Shift-Tab to arrive at the search criteria popup, because I have 11 Finder shortcut buttons in mine.


That means you need to adjust the number of repeats in the first loop here accordingly.


on run
	tell application "Finder"
		activate
		tell application "System Events"
			keystroke "f" using command down # press Cmd-F
			delay 0.08
			repeat 20 times
				key code 48 using shift down # shift-tab
				delay 0.08
			end repeat
			key code 125 # down arrow (to activate the first criterion popup)
			delay 0.2
			key code 126 # up arrow (to choose the "Name" operator)
			delay 0.1
			key code 36 # return (to accept the "Name" operator)
			delay 0.1
			key code 48 using shift down # shift-tab
			delay 0.4
			repeat 3 times
				key code 125 # down arrow (to choose the "Begins with" operator)
				delay 0.4
			end repeat
			delay 0.25
			key code 36 # return
		end tell
	end tell
end run

Jan 3, 2024 1:49 PM in response to Al Hatch

I’d suggest finding a different approach that preferably does not involve trying to imitate what malware does.


In addition to using Swift, zsh, Python or whatever via Spotlight or via traditional non-cached searches, there are third-party file-finding apps around, too.


If you would like Apple to expose an API here, log some feedback with Apple: Product Feedback - Apple


I’ve long found AppleScript useful for what it does, but that and Shortcuts both tend to reach their respective limits sooner than I’d have preferred.

Jan 3, 2024 1:31 PM in response to Al Hatch

The ultimate, simplest solution is to make changes in this file.


/System/Library/CoreServices/Finder.app/Contents/Resources/default_smart.plist


However, when the changes are made, Xcode issues this alert:


The file “default_smart.plist” could not be unlocked. The file is stored in a read-only volume. Try making the volume writable or copying the file to another volume.


I presume this refers to the APFS System Volume. Is there any way to unlock this file without messing with the entire System Volume? Say, changing its chflags?


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Sonoma Breaks AppleScript for Finder Search/Find

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.