Action

Select file and insert link

Posted by @jsamlarose, Last update over 1 year ago

UPDATES

over 1 year ago

Fixed a breaking error. Doh. URLs once again correctly include Files app URL scheme prefix (“shareddocuments://”).

show all updates...

over 1 year ago

Fixed a breaking error. Doh. URLs once again correctly include Files app URL scheme prefix (“shareddocuments://”).

over 1 year ago

Removed an “alert” leftover from testing…

over 1 year ago

Update description!

over 1 year ago

Refined.

Lists files from a selected folder in a filterable menu. A link to the selected file is pasted in the current draft.

SETUP:

  • Install this first: https://directory.getdrafts.com/g/10X (won’t work without it)
  • When you run the action, you should be prompted to assign a folder to the bookmark specified in the script (“files”). Adjust the script or the destination for the bookmark for your needs, if you already have a “files” bookmark in your set-up.
  • Script allows you to specify prefix and suffix for the file URL; currently defaults to a URL wiki-link “[[url:name of file]]”, but you can obviously switch this for a markdown link if that better suits your preferences.

See https://docs.getdrafts.com/docs/settings/bookmarks for more information about folder bookmarks in Drafts.

Steps

  • includeAction

    name
    MGCheckListPrompt Library
  • script

    // Markdown link
    let prefix = "[fileURL:](shareddocuments:/"
    let suffix = ")"
    
    // If your syntax definition supports [[url:...]] wiki-links (all of the built-in Markdown syntaxes do...) you can comment out / delete the lines above and uncomment the next two lines below:
    // let prefix = "[[url:shareddocuments:/"
    // let suffix = "]]"
    
    // Set your bookmark here: 
    let bookmark = Bookmark.findOrCreate("files")
    
    let fm = FileManager.createForBookmark(bookmark)
    let directory = fm.listContents("/")
    let backlinks = []
    
        		var n=0
        		while(directory[n]){
    
    			backlinks.push({
    						title: directory[n],
    						description: "",
    						info: "",
            			});
        		n++;
        		}
    
    
    // Create an MGCheckListPrompt to choose some drafts.
    var prompt = new MGCheckListPrompt()
    prompt.message = "Select a file"
    prompt.addItems(backlinks)
    prompt.allowsTypeToFilter = true
    prompt.singleSelectionMode = true
    prompt.selectsImmediately = true
    
    // Show the prompt.
    var selectedItems = prompt.show()
    
    // Report the result.
    if (prompt.didShow) {
    	if (selectedItems != null) {
    		var numItemsChosen = selectedItems.length;
    		let path = prefix + fm.basePath + backlinks[selectedItems[0]].title + suffix
    		editor.setSelectedText(path.replaceAll(" ","%20").replaceAll("|","%7C"))
    		
    	} else {
    		app.displayInfoMessage("Prompt was cancelled.")
    	}
    } else {
    	app.displayErrorMessage("Something went wrong.")
    }
    
    
    

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.