Jump to content

Write Text File to folder selected from list


Recommended Posts

Hi there, I've searched and searched, and I feel like I'm close, but can't quite get there. 

 

I have a workflow that uses Write a Text File, and I want to save it to a dynamic location within a specific scope. 

 

For example, I have a parent folder with 10 children folders inside. I'd like to be presented with the 10 children folders, select one, that child folder gets saved as a variable, and then my Write Text File action gets saved to /{var:childfolder}/name.txt

 

I have used Dynamic File Search and File Filter to get me to the right place, I just want to save the folder as a variable, to be used shortly, not open the folder.

 

Link to comment

I don’t really understand what the problem is. You just need to do exactly the same thing as the first three steps: Connect the File Filter to an Arg & Vars to create a new variable (say dirpath), then configure the Write Text File to write to {var:dirpath}/{var:name} [{var:rand}].json

 

If the problem is "how do I get a dropdown list?", File Filters don't work that way. They only show search results. No search query, no results.

 

To get a list without a search query, you'd have to use a List Filter or Script Filter instead.

Link to comment

@deanisheThank you so much for taking a look! Ahh, so I think what I am after then is a list filter or a script filter, because I do want to be presented with a list of the 10 folders to choose from. That's where my confusion was. Said another way, the first 4 vars are set completely anew while the 5th var would be chosen from a group and I don't always remember what the group has without seeing the list.

 

What would be the best way to combine the simplicity of that file filter (return all of the child folders inside a static parent folder) with the list-showing capabilities of a list or script filter? 

 

 

Link to comment
2 minutes ago, lehcar3 said:

What would be the best way to combine the simplicity of that file filter (return all of the child folders inside a static parent folder) with the list-showing capabilities of a list or script filter?

 

If it's a short, static list of folders, just create a List Filter by hand.

 

To get a dynamic list, put this script in a Script Filter with “Argument Optional”, Language = “/usr/bin/osascript (JavaScript)” and “Alfred filters results”. Obviously, you’ll need to change targetPath at the top of the script to point to your snippets directory.

 

// Path to snippets directory
const targetDir = '~/Desktop';


function getSubdirectories(folder) {
	let dir = $(folder).stringByStandardizingPath;
		fm = $.NSFileManager.defaultManager;

	return ObjC.unwrap(fm.contentsOfDirectoryAtPathError(dir, null))
	.filter(name => !name.js.startsWith('.'))
	.map(name => dir.stringByAppendingPathComponent(name))
	.filter(path => {
		let isdir = Ref();
		_ = fm.fileExistsAtPathIsDirectory(path, isdir);
		return isdir[0];
	});
}

function run() {
	let dirs = getSubdirectories(targetDir);
	console.log(`${dirs.length} folder(s) in ${targetDir}`);
	let items = dirs.map(path => { return {
		title: path.lastPathComponent.js,
		arg: path.js,
		type: 'file',
		icon: {path: path.js, type: 'fileicon'},
	}});
	return JSON.stringify({items: items}, null, 2);
}

 

Link to comment

@deanisheThank you so much for taking the time to write that. It worked perfectly, and I know I will get further use out of it in future workflows. 

 

I am posting my final workflow here for anyone interested in importing one snippet to Alfred. 

 

Background

I often use this method (Github link) which works to BULK-import snippets into Alfred using a csv. But oftentimes, I'm going about my day and I find that there's a single snippet I'd like to save. It always feels unnecessarily cumbersome to have to open alfred and navigate to the Snippets area and click around to add a single snippet.

 

This workflow

This workflow allows you to invoke Alfred and create a single new snippet directly from the Alfred entry window.

 

Entry:

There are two methods:

A ) You can enter a comma delimited string for snippet,name,value all at once.

B ) You are prompted step by step for snippet, name, and value.

 

Folder selection:

It prompts you to choose which preexisting snippets folder you want to save it in (it's possible you may need to change the snippets directory in the Script Filter). This uses the lovely script written by @deanishe above.

 

Snippet creation:

It creates a json file within the appropriate folder. A random identifier is required within the file itself and as part of the filename, so you'll see some random variable cleanup happening in the workflow.

 

Reveal:

For fun and validation, it reveals to you the snippet file in Finder (delete this step if not needed).

 

Using the snippet:

I found through testing that the snippet is not immediately available for use after it's created. It appeared eventually, but after I checked immediately and it wasn't able to be called in the snippets window, I checked again and it was there like ~10 minutes later. My guess is that Alfred loads its snippets on startup, or periodically, or by manual interaction with the snippets preferences (idk). So, the last step in this workflow restarts Alfred in case you want to use it immediately. If this step is found to be annoying, or if you don't think you will need the snippet immediately (it's probably on your clipboard :)) this step can be removed. 

The script used here is from:

 

Download this workflow:

https://drive.google.com/file/d/1Oe1XXViYe2fHWTgQqRvjT7KeUEAEwzBf/view?usp=sharing

 

Disclaimer:

This workflow is not perfect and could definitely be streamlined (e.g., the random variable cleanup might not be necessary, maybe there's something less aggressive than restarting Alfred). Also sorry if I didn't follow layout best practices for long workflows, I couldn't find what they are and tried to make it as neat as I could.

Link to comment
6 hours ago, lehcar3 said:

A random identifier is required within the file itself and as part of the filename

 

The filename doesn't matter. Alfred adds a UUID to prevent any name clashes, but it's not required.

 

6 hours ago, lehcar3 said:

the random variable cleanup might not be necessary

 

Variables only exist while the workflow is actually running. If you didn't explicitly save the variable to info.plist, there is nothing to clean up.

 

6 hours ago, lehcar3 said:

My guess is that Alfred loads its snippets on startup, or periodically, or by manual interaction with the snippets preferences (idk)

 

Alfred watches the filesystem for changes. As long as you don’t have any symlinks in your prefs bundle, Alfred should notice new snippets immediately.

 

6 hours ago, lehcar3 said:

 

Thanks. I'll have a look at it in the morning. It's time for bed now.

Link to comment

Discovered I need to escape this text or it breaks wildly in the generated .json with any special characters :) — is there a best way to do this, given the above workflow? I searched for some workflows but couldn't find something that looked useful for this case. 

Link to comment
5 hours ago, lehcar3 said:

is there a best way to do this

 

If you're going to put characters in the snippets that aren't allowed by JSON, you'll need to use a real JSON library to generate the JSON in order to escape the illegal characters properly. That is to say, you need to write a script.

 

Why are you generating the UUID yourself instead of using the "Add UUID to filename" option in Write Text File?

Edited by deanishe
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...