Action

New Draft++

Posted by garbonsai, Last update about 2 years ago

UPDATES

about 2 years ago

  • Bugfixes for sorting templates
show all updates...

about 2 years ago

  • Bugfixes for sorting templates

over 2 years ago

  • Use template’s syntax for new draft instead of default syntax

about 3 years ago

  • Added a “New draft with timestamp” option. Edit the “timestamp” template tag to modify the formatting.

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

  • defineTemplateTag

    name
    Dated
    template
    ### [[date|%a, %d %b %Y %T %z]]
    
    
  • 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, false );
    	let templates = tempws.query( 'all' );
    
    	// start building the prompt
    	let prompt = Prompt.create();
    	prompt.title = 'New Draft++';
    	prompt.message = 'Create a new draft, optionally with the clipboard, using a template, or in a workspace.';
    
    	// add a button for blank draft
    	prompt.addButton( 'New Draft' );
    
    	// add a button for clipboard
    	prompt.addButton(
    		'New with Clipboard',
    		{ type: 'clipboard' }
    	);
    
    	// add buttons for each template
    	let i = 0;
    	for ( let template of templates ) {
    		prompt.addButton(
    			'Using “' + template.displayTitle + '” Template',
    			{ type: 'template', value: i, syntax: template.syntax }
    		);
    		i++;
    	}
    
    	// add a button for the current workspace
    	prompt.addButton(
    		'In 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(
    				'In “' + 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 d = Draft.create();
    	let tags = [];
    
    	// insert the contents of the system clipboard
    	if ( selection.type == 'clipboard' ) {
    		d.content = app.getClipboard();
    
    	// start building the draft based on the selected template
    	} else if ( selection.type == 'template' ) {
    		let template = templates[ selection.value ];
    		tags = template.tags.filter(
    			tag => tag.trim() != 'template' && tag.trim() != 'untagged'
    		);
    		d.content = draft.processTemplate( template.content );
    		d.syntax = selection.syntax;
    		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 ) d.addTag( tag );
    
    	// load the new draft
    	d.update();
    	editor.load( d );
    	editor.activate();
    
    	// if applicable, queue an ND++ extension
    	let extension = Action.find( 'ND++ ' + d.displayTitle + ' Extension' );
    	if ( extension !== undefined ) app.queueAction( extension, d );
    	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.