derekvan Posted January 17, 2022 Share Posted January 17, 2022 Is it possible to create a workflow that will show all files of a certain type in a folder? I know I can create a file filter to allow me to search for files, but for this context I don't want to search. For example, I want to say: show me all the PDF files in the downloads folder (in Alfred rather than in the finder). Is that possible? Thanks! Link to comment
deanishe Posted January 17, 2022 Share Posted January 17, 2022 This workflow lists/filters all PDFs in ~/Downloads. The script can easily be edited to search a different directory or for a different file extension. Note: This isn't the same as a File Filter. It only finds files that are children of the target folder. It doesn't search subdirectories. derekvan 1 Link to comment
derekvan Posted January 17, 2022 Author Share Posted January 17, 2022 Perfect! Thank you. Link to comment
derekvan Posted January 18, 2022 Author Share Posted January 18, 2022 I made some adjustments to the script to allow for searching multiple locations as well as multiple extensions. Thanks again @deanishe for helping out with the original script (which I never could have written but have just enough js knowledge to tinker with) #!/usr/bin/osascript -l JavaScript // Return list of files in folder with given extension. function findFiles(folder, extension) { let fm = $.NSFileManager.defaultManager, files = []; let root = $(folder).stringByExpandingTildeInPath; let contents = fm.contentsOfDirectoryAtPathError(root, null); for (let i = 0; i < contents.count; i++) { files.push(root.stringByAppendingPathComponent(contents.objectAtIndex(i))); } if (extension) files = files.filter(p => p.pathExtension.js == extension) return files; } // Convert NSString path to an Alfred feedback item. function alfredItem(path) { return { title: path.lastPathComponent.js, subtitle: path.stringByAbbreviatingWithTildeInPath.js, arg: path.js, uid: path.js, valid: true, type: 'file', icon: {path: path.js, type: 'fileicon'}, }; } function queryRegex(query) { let pat = []; let iter = query[Symbol.iterator](); let char = iter.next(); while (!char.done) { pat.push(char.value); char = iter.next(); } return new RegExp(pat.join('.*'), 'i'); } function run(argv) { let query = argv.length ? argv[0] : null; // Folder to search in var folder = '~/Downloads'; // Only show files with this extension var extension = 'epub'; let files = findFiles(folder, extension); var extension = 'mobi'; files = files.concat(findFiles(folder, extension)); folder = '~/Library/Mobile Documents/com~apple~CloudDocs/Downloads'; files = files.concat(findFiles(folder, extension)); var extension = 'epub'; files = files.concat(findFiles(folder, extension)); if (query) { let rx = queryRegex(query); files = files.filter(p => p.lastPathComponent.js.match(rx) !== null); } return JSON.stringify({items: files.map(alfredItem)}, null, 2); } Link to comment
TomBenz Posted May 19, 2022 Share Posted May 19, 2022 On 1/18/2022 at 5:11 PM, derekvan said: I made some adjustments to the script to allow for searching multiple locations as well as multiple extensions. Thanks again @deanishe for helping out with the original script (which I never could have written but have just enough js knowledge to tinker with) #!/usr/bin/osascript -l JavaScript // Return list of files in folder with given extension. function findFiles(folder, extension) { let fm = $.NSFileManager.defaultManager, files = []; let root = $(folder).stringByExpandingTildeInPath; let contents = fm.contentsOfDirectoryAtPathError(root, null); for (let i = 0; i < contents.count; i++) { files.push(root.stringByAppendingPathComponent(contents.objectAtIndex(i))); } if (extension) files = files.filter(p => p.pathExtension.js == extension) return files; } // Convert NSString path to an Alfred feedback item. function alfredItem(path) { return { title: path.lastPathComponent.js, subtitle: path.stringByAbbreviatingWithTildeInPath.js, arg: path.js, uid: path.js, valid: true, type: 'file', icon: {path: path.js, type: 'fileicon'}, }; } function queryRegex(query) { let pat = []; let iter = query[Symbol.iterator](); let char = iter.next(); while (!char.done) { pat.push(char.value); char = iter.next(); } return new RegExp(pat.join('.*'), 'i'); } function run(argv) { let query = argv.length ? argv[0] : null; // Folder to search in var folder = '~/Downloads'; // Only show files with this extension var extension = 'epub'; let files = findFiles(folder, extension); var extension = 'mobi'; files = files.concat(findFiles(folder, extension)); folder = '~/Library/Mobile Documents/com~apple~CloudDocs/Downloads'; files = files.concat(findFiles(folder, extension)); var extension = 'epub'; files = files.concat(findFiles(folder, extension)); if (query) { let rx = queryRegex(query); files = files.filter(p => p.lastPathComponent.js.match(rx) !== null); } return JSON.stringify({items: files.map(alfredItem)}, null, 2); } Pls post the updated workflow for searching multiple extension and how to use it. Are the extension hard-coded or passed through query. Link to comment
macrospect Posted May 7 Share Posted May 7 This is a terrific script that you collectively ended up with, @derekvan. Any chance that it could be reconfigured to reveal the files in alphabetical order? Right now I can't quite tell what the ordering is, but it's something like most recently opened. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now