Action

update workspace (add tags in category to ws filter)

Posted by @FlohGro, Last update over 3 years ago

update workspace (add tags in category to ws filter)

created by @FlohGro

  • this action will update (or create) a configurable workspace (by its name).
  • the action will add all “childtags” with the same configurable prefix to the workspace filter and apply the workspace afterwards.
  • settings like e.g. the actiongroup which should be applied after applying the workspace can simply be set in the configuration menu of the workspace itself. they won‘t be affected / changed by this action
  • if you need the action for several workspaces just duplicate it and change the configuration inside the script.

usecase:

You are structuring your darfts in a private and work related workspaces. You’re using tag prefixes for the different parts of your life, e.g. each work related draft gets a tag like “work/customer” or each private draft gets a tag like “personal/home”. Since workspace filters of drafts can’t include a wildcard filter you need to manually update the workspaces for the different part of your life if you create a new tag in this area (you need to open the config of the workspace and add each new tag manually). You will of course forget to update the workspace in the future - this action is the solution to this problem. It will update the workspace accordingly with all tags with the same prefix. If you need this action for different areas and workspaces you can simply duplicate it and reconfigure it accordingly. This could also be used if you structure your tags in with a system like P.A.R.A

Attention: this action needs manual configuration in the script step of the action

Buy Me A Coffee

Steps

  • script

    // Created by @FlohGro
    // update workspace (add tags in category to ws filter)
    
    // this action will update (or create) a configurable workspace (by its name).
    // the action will add all "childtags" with the same configurable prefix to the workspace filter and apply the workspace afterwards.
    // usecase:
    // you are structuring your darfts in a private and work related workspaces. You're using tag prefixes for the different parts of your life, e.g. each work related draft gets a tag like "work/customer" or each private draft gets a tag like "personal/home". Since workspace filters of drafts can't include a wildcard filter you need to manually update the workspaces for the different part of your life if you create a new tag in this area (you need to open the config of the workspace and add each new tag manually). You will of course forget to update the workspace in the future - this action is the solution to this problem. It will update the workspace accordingly with all tags with the same prefix. If you need this action for different areas and workspaces you can simply duplicate it and reconfigure it accordingly.
    // this could also be used if you structure your tags in with a system like P.A.R.A
    
    // -------------------
    // configuration of the action:
    // the name the workspace has (or should have if it not already exists)
    var ws_name = "Work";
    // the prefix of each tag in that category / area of life
    var ws_tagPrefix = "work";
    
    // configuration of the worksace settings
    // these options will affect the workspace settings and overwrite existing settings in the workspace
    // all the options are well documented in the [drafts documentation](https://docs.getdrafts.com/docs/drafts/workspaces) and the [scripting reference](https://scripting.getdrafts.com/classes/workspace) if you have questions reach out to @FlohGro in the forum or on twitter :)
    
    const showPreview = false;
    const showTags = true;
    const showLastAction = false;
    const inboxIncludesFlagged = false;
    const archiveIncludesFlagged = true;
    const setAllSort_sortBy = "modified";
    const setAllSort_sortDescending = true;
    const setAllSort_sortFlaggedToTop = true;
    const setFlaggedSort_sortBy = "modified";
    const setFlaggedSort_sortDescending = true;
    const setFlaggedSort_sortFlaggedToTop = true;
    const setInboxSort_sortBy = "modified";
    const setInboxSort_sortDescending = true;
    const setInboxSort_sortFlaggedToTop = true;
    
    // attention if you change this to "true", every found tag in that category must be included in a draft to be displayed in this workspace - normally this should stay false
    const tagFilterRequireAll = false;
    
    // end of configuraion
    // -------------------
    
    // all tags are required 
    
    function allTags() {
      return [...new Set([].concat(...Draft.query("", "all", []).map((currentDraft) => currentDraft.tags)))].sort();
    }
    
    var tags = allTags();
    var ws_setTags = [];
    for (tag of tags) {
      //	alert(tag);
      if (tag.startsWith(ws_tagPrefix)) {
        ws_setTags.push(tag);
      }
    }
    
    var ws_tags = ws_setTags.toString();
    
    var ws = Workspace.find(ws_name);
    
    if (!ws) {
    alert("creating new workspace because \"" + ws_name + "\" does not exist!");
      ws = Workspace.create()
      ws.name = ws_name;
    }
    
    ws.tagFilter = ws_tags;
    ws.tagFilterRequireAll = tagFilterRequireAll;
    ws.showPreview = showPreview;
    ws.showTags = showTags;
    ws.showLastAction = showLastAction;
    ws.inboxIncludesFlagged = inboxIncludesFlagged;
    ws.archiveIncludesFlagged = archiveIncludesFlagged;
    ws.setAllSort(setAllSort_sortBy,setAllSort_sortDescending,setAllSort_sortFlaggedToTop);
    ws.setFlaggedSort(setFlaggedSort_sortBy,setFlaggedSort_sortDescending,setFlaggedSort_sortFlaggedToTop);
    ws.setInboxSort(setInboxSort_sortBy,setInboxSort_sortDescending,setInboxSort_sortFlaggedToTop);
    
    ws.update();
    app.applyWorkspace(ws);

Options

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