Searching for folders containing specific file extension

While searching Apple Discussions for a solution I came across VikingOSX's AppleScript generously written for another user. It works great at identifying folders containing MP3 files on the local drive but I need it to work on the active folder—often on an external disk—and also to bring the found folders up in the finder rather than at the bottom of the script. Will someone confirm this is possible in AppleScript and maybe point me in the right direction?


Here's VinkingOSX's Handiwork:

use scripting additions

set mp3_fold to paragraphs of (do shell script "/bin/zsh -s <<'EOF' - " & "
typeset -a dirs=()
dirs=( $HOME/**/*.(mp3)(.N) )
print -l ${dirs:h}
EOF")

log mp3_fold
return

MacBook Pro 15″

Posted on Jul 13, 2023 6:43 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 14, 2023 4:56 PM

Here is a revised solution that will open each folder containing one or more .mp3 files as individual Finder windows, and then with a keyboard shortcut, you can tell Finder to cascade these open windows in an orderly fashion. You can then cycle through these windows using cmd+backtick (`), and close all of them with another single keystroke (option+cmd+W).


You want to open Apple's Script Editor via (Dock : Launchpad : Other : Script Editor. Copy and paste the following code into the Script Editor, click the hammer icon to compile the copied code, and then you want to do these in order:


  1. Initially save the script source via File > Save
    1. File format: Text
    2. Line Endings: macOS
    3. Save As: main.applescript
    4. Location: Desktop
  2. Option+Save As…
    1. File Format: Application
    2. Save As: main.app
    3. Locaation: Desktop
    4. Options: none
    5. Once you have saved this application, drag and drop it to your Dock just to the left of the last | separator.
  3. In Finder View menu, select Show Path Bar


Code source:


(*
	main.applescript
	
	This script cannot be run from the Script Editor. It must be saved as
	an application that sits in your Dock, so when you select the source
	folder in the Finder, it remains selected. A single-click of this
	app in the Dock will open one Finder window per found folder
	containing an .mp3 audio file.
	
	Initially, this will spray Finder windows around the Desktop, but one
	can cascade them by pressing the option key and choosing
	"Arrange in Front" from the Finder Window menu.
	
	Cascaded windows can be cycled through by clicking cmd+` (back tick),
	and all open Finder windows can be closed at once by option+cmd+W.
*)
use scripting additions

tell application "Finder"
	
	if selection is {} then return
	
	set selFolder to (selection as text) as alias
	if not kind of selFolder is "Folder" then return
	
	set selFolder to POSIX path of selFolder
	
	-- recursively return list of folders containing .mp3 files beneath original selection
	set mp3_fold to paragraphs of (do shell script "/bin/zsh -s <<'EOF' - " & selFolder & "
typeset -a dirs=()
dirs=( ${1}/**/*.(mp3)(.N) )
print -l ${dirs:h}
EOF")
	
	-- get the folder paths in HFS file format so reveal will work on them
	repeat with i from 1 to (the count (mp3_fold as list))
		try
			set item i of mp3_fold to (((item i of mp3_fold as text) as POSIX file) as alias)
		on error errmsg number errnum
			display dialog errnum & ": " & errmsg
		end try
	end repeat
	
	reveal mp3_fold
	
end tell
return


You will need to preselect the folder you want to process before single-clicking the app in the Dock. The next event will be be found folders opened in separate windows on your Desktop. This is where you press the option key and from the Finder Window menu, you choose Arrange in Front. This triggers the organized cascade of overlapping Finder windows. Each window will have the full path displayed at the bottom of the window to the folder in it. During the same login session, the Finder remembers you were using a cascade and subsequent .mp3 folder selections are presented in a cascade too.


You can rotate through the individual cascaded Finder windows using cmd+backtick (`) and close all of the cascaded windows with just a option+cmd+w keystroke.


Tested: macOS Ventura 13.4.1 (c).

Similar questions

12 replies
Question marked as Top-ranking reply

Jul 14, 2023 4:56 PM in response to OrganicBooks

Here is a revised solution that will open each folder containing one or more .mp3 files as individual Finder windows, and then with a keyboard shortcut, you can tell Finder to cascade these open windows in an orderly fashion. You can then cycle through these windows using cmd+backtick (`), and close all of them with another single keystroke (option+cmd+W).


You want to open Apple's Script Editor via (Dock : Launchpad : Other : Script Editor. Copy and paste the following code into the Script Editor, click the hammer icon to compile the copied code, and then you want to do these in order:


  1. Initially save the script source via File > Save
    1. File format: Text
    2. Line Endings: macOS
    3. Save As: main.applescript
    4. Location: Desktop
  2. Option+Save As…
    1. File Format: Application
    2. Save As: main.app
    3. Locaation: Desktop
    4. Options: none
    5. Once you have saved this application, drag and drop it to your Dock just to the left of the last | separator.
  3. In Finder View menu, select Show Path Bar


Code source:


(*
	main.applescript
	
	This script cannot be run from the Script Editor. It must be saved as
	an application that sits in your Dock, so when you select the source
	folder in the Finder, it remains selected. A single-click of this
	app in the Dock will open one Finder window per found folder
	containing an .mp3 audio file.
	
	Initially, this will spray Finder windows around the Desktop, but one
	can cascade them by pressing the option key and choosing
	"Arrange in Front" from the Finder Window menu.
	
	Cascaded windows can be cycled through by clicking cmd+` (back tick),
	and all open Finder windows can be closed at once by option+cmd+W.
*)
use scripting additions

tell application "Finder"
	
	if selection is {} then return
	
	set selFolder to (selection as text) as alias
	if not kind of selFolder is "Folder" then return
	
	set selFolder to POSIX path of selFolder
	
	-- recursively return list of folders containing .mp3 files beneath original selection
	set mp3_fold to paragraphs of (do shell script "/bin/zsh -s <<'EOF' - " & selFolder & "
typeset -a dirs=()
dirs=( ${1}/**/*.(mp3)(.N) )
print -l ${dirs:h}
EOF")
	
	-- get the folder paths in HFS file format so reveal will work on them
	repeat with i from 1 to (the count (mp3_fold as list))
		try
			set item i of mp3_fold to (((item i of mp3_fold as text) as POSIX file) as alias)
		on error errmsg number errnum
			display dialog errnum & ": " & errmsg
		end try
	end repeat
	
	reveal mp3_fold
	
end tell
return


You will need to preselect the folder you want to process before single-clicking the app in the Dock. The next event will be be found folders opened in separate windows on your Desktop. This is where you press the option key and from the Finder Window menu, you choose Arrange in Front. This triggers the organized cascade of overlapping Finder windows. Each window will have the full path displayed at the bottom of the window to the folder in it. During the same login session, the Finder remembers you were using a cascade and subsequent .mp3 folder selections are presented in a cascade too.


You can rotate through the individual cascaded Finder windows using cmd+backtick (`) and close all of the cascaded windows with just a option+cmd+w keystroke.


Tested: macOS Ventura 13.4.1 (c).

Jul 14, 2023 7:54 AM in response to OrganicBooks

I can augment the original script with AppleScript resulting in an Automator Quick Action, so all you have to do is select the preferred starting Folder in the Finder, right-click and choose Quick Actions -> MP3 Folder Detect, where the latter is the arbitrary name given the quick action. The selected folder is passed into the Quick Action.


The result is:



AppleScript display dialogs do not have vertical or horizontal scrolling features without considerably more complicated code, so by default long lines wrap, and there is a risk that too many folders will expand the dialog off the bottom of the screen. The tilde character is just shorthand for /Users/username.


When you tell the Finder to reveal a list of folder locations as shown above, it opens a separate Finder window for each folder entry. You probably don't want a screen full of Finder windows to deal with or close.


Is this something you want to pursue?

Jul 14, 2023 2:53 PM in response to OrganicBooks

OrganicBooks wrote:

Indeed it would be wonderful, Viking! Your generosity is a delightful surprise. In fact my aged eyes would welcome a screen full of windows more than the wrap-around method, if it's not too much trouble.

Be careful what you wish for… if there are 50 folders containing .mp3 files, that will be 50 instantaneous Finder windows that open all over your Desktop in no particular order. Closing all of those Finder windows in one keystroke is however:


option-key + Finder File menu : Close All (option+cmd+W).


I will post the instructions and code in another reply.

Jul 14, 2023 6:17 PM in response to VikingOSX

The care you put into not only your script but also your exquisitely detailed instructions says a lot about your work, Viking, which makes me feel even worse that I must report it’s not working and it’s likely my fault. While my question does indicate Ventura I’m actually on the ancient Catalina. I’d tried appealing for help under Older Systems and had no responses. I told myself it wouldn’t make much difference but I see now that’s not the case. After pressing the app in the dock I got a dialogue to accept then every try thereafter flashed the gray parts of Finder white for a second then that's all. I tried waiting then tried applying it to a folder with a handful of MP3 folders, with no change.


Here are two screenshots of my Script Editor’s saving dialogue in case it’s a simple adjustment. “Script Bundle” is one of the dropdown options. Please be assured I followed your installation guide to the letter otherwise. You made it foolproof.




Someone with your gifts no doubt has a life to get back to and I don’t expect you to modify your work due to my error. Unless you object I can ask AI to change it for my OS then let you know how it goes? Regardless you’re the kindest person I’ve encountered on any forum. If it were another site you’d get ALL the awards.

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.

Searching for folders containing specific file extension

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