Action

New Draft++

Posted by Garbonsai, Last update 19 days ago

Create a new draft, optionally using a template or within a workspace. Skips workspaces that don’t filter drafts using tags, or that exclusively filter tags out (using “-“).

Steps

  • script

    // 
    let f = () => {
    
    	// create a temporary workspace
    	let tempws = Workspace.create();
    	if ( !tempws ) return false;
    
    	// get a list of all drafts tagged 'template'
    	tempws.tagFilter = 'template';
    	tempws.setAllSort( 'name', false, true );
    	let templates = tempws.query( 'all' );
    
    	// start building the prompt
    	let prompt = Prompt.create();
    	prompt.title = 'New Draft++';
    	prompt.message = 'Create a new draft, optionally using a template or workspace.';
    
    	// add button for blank draft
    	prompt.addButton( 'New draft' );
    
    	// add buttons for each template
    	let i = 0;
    	for ( let template of templates ) {
    		prompt.addButton(
    			'“' + template.displayTitle + '” template',
    			{ type: 'template', value: i }
    		);
    		i++;
    	}
    
    	// add a button for the current workspace
    	prompt.addButton(
    		'Current workspace',
    		{ type: 'workspace', value: 'current' }
    	);
    
    	// add buttons for each workspace
    	let workspaces = Workspace.getAll();
    	let j = 0;
    	for ( let ws of workspaces ) {
    
    		// filter workspaces without tags or only with '!' tags
    		let tags = ws.tagFilter
    			.split( ',' )
    			.filter( tag =>
    				tag.trim().length > 0 &&
    				tag.trim()[ 0 ] != '!' &&
    				tag.trim() != 'untagged'
    			)
    			.join( ',' );
    		if ( tags.length > 0 ) {
    			prompt.addButton(
    				'“' + ws.name + '” workspace',
    				{ type: 'workspace', value: j }
    			);
    		}
    		j++;
    	}
    
    	// display the prompt
    	if ( !prompt.show() ) return false;
    	let selection = prompt.buttonPressed;
    
    	// create a new draft
    	let draft = Draft.create();
    	let tags = [];
    
    	// start building the draft based on the selected template
    	if ( selection.type == 'template' ) {
    		let template = templates[ selection.value ];
    		tags = template.tags.filter(
    			tag => tag.trim() != 'template' && tag.trim() != 'untagged'
    		);
    		draft.content = draft.processTemplate( template.content );
    		app.applyWorkspace();
    
    	// start building the draft based on the selected workspace
    	} else if ( selection.type == 'workspace' ) {
    		if ( selection.value == 'current' ) {
    			tags = app.currentWorkspace.tagFilter.split( ',' ).filter(
    				tag => tag.trim()[ 0 ] != '!' && tag.trim() != 'untagged'
    			);
    		} else {
    			tags = workspaces[ selection.value ].tagFilter.split( ',' ).filter(
    				tag => tag.trim()[ 0 ] != '!' && tag.trim() != 'untagged'
    			);
    			app.applyWorkspace( workspaces[ selection.value ] );
    		}
    	}
    
    	// assign the tags
    	for ( let tag of tags ) draft.addTag( tag );
    
    	// load the new draft
    	draft.update();
    	editor.load( draft );
    	editor.activate();
    	return true;
    }
    
    if ( !f() ) context.cancel();
    

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.