Jump to content

Show all files of type in folder?


Recommended Posts

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

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
  • 4 months later...
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...