Action

open all forward links

Posted by FlohGro, Last update 8 months ago

created by @FlohGro / more on my Blog

open all forward links

finds [[wiki-style-links]] in the current Draft. If one or more are found the action will open them differently depending on the platform you’re running it.
- On macOS it will open each forward link in a new tab.
- On iPadOS it will present a prompt and open the selected Draft in a new window
- On iOS it will present a prompt and load the selected Draft in the editor


If you find this useful and want to support me you can donate or buy me a coffe

Buy Me A Coffee

Steps

  • script

    openAllForwardLinksFromDraft(draft)
    
    /**
     * opens a selected forward link in wiki-style-format in the given draft (if existing)
     * @param {Draft} d the Draft in which you want to find wiki-style-links
     */
    function openAllForwardLinksFromDraft(d) {
        let content = d.content;
    
        const regex = /\[\[([^\]]+)\]\]/g;
        const matches = [];
    
        let match;
        while ((match = regex.exec(content)) !== null) {
            const linkText = match[1];
            if(!matches.includes(linkText) && linkText != draft.displayTitle){
                matches.push(linkText);
            }
        }
    
        if (matches.length > 0) {
            let draftsToOpen = []
            matches.forEach((match) => {
                draftsToOpen = draftsToOpen.concat(Draft.queryByTitle(match))
            })
    
            let window = app.currentWindow
            if(window.canCreateTab){
                draftsToOpen.forEach((curDraft) => {
                    window.openInNewTab(curDraft)
                })
            } else {
                // not a mac → user needs to select the draft to open
                let p = new Prompt()
                p.title = "select draft to open"
                p.message = "tabs are not supported on iPhone / iPad, please select the draft to open"
                
            draftsToOpen.forEach((curDraft) => {
                p.addButton(curDraft.displayTitle,curDraft)
            })
            if(p.show()){
                let selectedDraft = p.buttonPressed
                if(device.model == "iPad"){
                    app.openInNewWindow(selectedDraft)
                } else {
                    editor.load(selectedDraft)
                }  
            }
              
            }
        }else {
            app.displayWarningMessage("no forward links found in draft")
        }
    }

Options

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