Query drafts by dates as well as content

Would love to be able to use the query function in the javascript capabilities to search for a list of Drafts not just by a string but also by dates. E.g., return list of drafts created between date x and y, or return list of drafts modified in the last 7 days, etc etc. this would be especially powerful combined with the workspace scripting and creating “temporary” workspaces as shared in a recent tip.

You can do that easily enough right now:

const weekAgo = new Date()
weekAgo.setDate( weekAgo.getDate() - 7 )

const drafts = Workspace.getAll()
                        .reduce( (dd, ws) => {
                            return dd.concat( ws.query('all') )
                        }, [] )
                        .filter( d => d.createdAt.getTime() >= weekAgo.getTime() )
console.log( drafts.length )

It wouldn’t be too difficult to create similar script steps for other date criteria.

But a built-in method would probably be nice too.

Thanks for sharing this. I’m having some trouble understanding the code though. How can I make this display the list of drafts that meet the criteria in the drafts list sidebar? I can’t figure out how to feed the result into a temporary or permanent workspace to then load via app.applyWorkspace

Hmm, doesn’t look like you can load a random assortment of Drafts into a Workspace. That’s a little disappointing.

What you would have to do then, I think, is apply a tag to the list of Drafts and the use that tag to build your Workspace. You could then have another action to remove that tag when you are done.

Something like:

const weekAgo = new Date()
weekAgo.setDate( weekAgo.getDate() - 7 )

const drafts = Workspace.getAll()
                        .reduce( (dd, ws) => {
                            return dd.concat( ws.query('all') )
                        }, [] )
                        .filter( d => d.createdAt.getTime() >= weekAgo.getTime() )
drafts.forEach( d => {
                         d.addTag( 'temp' )
                         d.update()
                     } )

const tmpWorkspace = Workspace.create()
tmpWorkspace.name = 'Temp'
tmpWorkspace.tagFilter = 'temp'
tmpWorkspace.queryString = ''

app.applyWorkspace( tmpWorkspace )
app.showDraftList()
1 Like

Yeah, that does it. Thanks for working it out.

While this does work, it’s a little cumbersome so I’ll just repeat the feature request for a way to query by date so the tag / untag process wouldn’t be necessary.