Download file from clipboard URL directly to folder

What does this button do?
This 1-click-button checks whether the clipboard contains a URL and then downloads the file to the current folder. If a file with that name already exits, it displays a dialog where you can choose whether to overwrite/change the name or cancel the download.
If there's no file extension in the URL, it will also display a dialog, where you can enter file name and extension. (In this case, there's no separate check if the file exists already.)

Edited 2023-06-30: The button now suggests a file name in the overwrite/no file name dialog.

Use case:
I'm using this button to download directly to a folder instead of having to use the download dialog of a browser to choose the file location. It's also useful if you get a URL file location in plain text (as opposed to a link that you can click).
It only downloads a single file and cannot rip a website. Also, I wouldn't use this for larger files. In such a case, using a download manager like Internet Download Manager (IDM) or the external wget application is better.

How to install: Expand the XML code > Copy the code > right-click Dopus toolbar > Customize... > right-click somewhere on the toobar > Paste

Button as XML (current version)
<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
	<label>Download URL target to folder</label>
	<icon1>/iconsets/MoreOpus12FlatIcons/DownloadClipboardURLtoFolderPath_32x32.png,0</icon1>
	<function type="script">
		<instruction>@script JScript</instruction>
		<instruction>function OnClick(clickData) {</instruction>
		<instruction>    clickData.func.command.deselect = false; // Prevent automatic deselection</instruction>
		<instruction />
		<instruction>    // Check if the clipboard contains text</instruction>
		<instruction>    if (DOpus.GetClipFormat() !== &apos;text&apos;) return;</instruction>
		<instruction />
		<instruction>    // Save the clipboard text in a variable called ClipboardData</instruction>
		<instruction>    var ClipboardData = DOpus.GetClip(&quot;url&quot;);</instruction>
		<instruction />
		<instruction>    // Check if the string ClipboardData contains the substring &quot;://&quot;</instruction>
		<instruction>    if (ClipboardData.indexOf(&quot;://&quot;) &gt; -1) {</instruction>
		<instruction />
		<instruction>		// Extract the domain name from the URL</instruction>
		<instruction>        var domainPattern = /^https?:\/\/([^/?]+)/;</instruction>
		<instruction>        var domainMatch = ClipboardData.match(domainPattern);</instruction>
		<instruction>        var DomainName = domainMatch ? domainMatch[1].replace(/\./g, &apos;_&apos;) : &apos;&apos;;</instruction>
		<instruction />
		<instruction>        // Split the text in ClipboardData whenever a forward slash is encountered</instruction>
		<instruction>        var URLParts = ClipboardData.split(&quot;/&quot;);</instruction>
		<instruction>        var LastElement = URLParts[URLParts.length - 1];</instruction>
		<instruction>		</instruction>
		<instruction>        // Use regular expressions to match and extract the filename in case URLParts contains query parameters</instruction>
		<instruction>        var pattern = /([^/?]+)\.([^/?]+)(\?.*)?$/;</instruction>
		<instruction>        var matches = LastElement.match(pattern);</instruction>
		<instruction>        var Filename = matches ? matches[1] : LastElement;</instruction>
		<instruction>        var Filetype = matches ? &quot;.&quot; + matches[2] : &quot;&quot;;</instruction>
		<instruction />
		<instruction>		// Remove invalid characters from Filename</instruction>
		<instruction>        Filename = Filename.replace(/[&lt;&gt;:&quot;/\\|?*]/g, &apos;&apos;);</instruction>
		<instruction />
		<instruction>        // Resolve the source path</instruction>
		<instruction>        var SourcePath = clickData.func.sourcetab.path;</instruction>
		<instruction>        var NewPathName = SourcePath + &quot;\\&quot; + Filename + Filetype;</instruction>
		<instruction />
		<instruction>        // Extract the last folder from the folder path</instruction>
		<instruction>        var FolderParts = SourcePath.split(&quot;\\&quot;);</instruction>
		<instruction>        var FolderName = FolderParts[FolderParts.length - 1];</instruction>
		<instruction />
		<instruction>        // Display variables</instruction>
		<instruction>        DOpus.Output(&quot;-------------------------------------------------------------&quot;);</instruction>
		<instruction>        DOpus.Output(&quot;Downloading copied URL (clipboard) to current folder path... &quot;);</instruction>
		<instruction>        DOpus.Output(&quot;URL: &quot; + ClipboardData);</instruction>
		<instruction>        DOpus.Output(&quot;File name: &quot; + Filename);</instruction>
		<instruction>        DOpus.Output(&quot;File type: &quot; + Filetype);</instruction>
		<instruction>        DOpus.Output(&quot;Folder path: &quot; + SourcePath);</instruction>
		<instruction>        DOpus.Output(&quot;Folder name: &quot; + FolderName);</instruction>
		<instruction>        DOpus.Output(&quot;File path after download: &quot; + NewPathName);</instruction>
		<instruction />
		<instruction>        if (Filetype === &quot;&quot;) {</instruction>
		<instruction>            Filetype = &quot;.html&quot;;</instruction>
		<instruction>            DOpus.Output(&quot;No file type found. Not checking if file exists already.&quot;);</instruction>
		<instruction>			</instruction>
		<instruction>			if (Filename === &quot;&quot;) {</instruction>
		<instruction>					DOpus.Output(&quot;No file name found.&quot;);</instruction>
		<instruction>					//var ElementBeforeLast = URLParts[URLParts.length - 2];</instruction>
		<instruction>					Filename = DomainName + &quot;-&quot; + URLParts[URLParts.length - 2];</instruction>
		<instruction>			</instruction>
		<instruction>			</instruction>
		<instruction />
		<instruction>            // If no filetype was found, display dialog to enter file name and file type</instruction>
		<instruction>            var dlg = DOpus.dlg;</instruction>
		<instruction>            dlg.select = true;</instruction>
		<instruction>            var NewFilenameFiletype = dlg.getstring(&quot;Overwrite or enter filename and file extension:&quot;, Filename + Filetype, 250, &quot;Save|Cancel&quot;, &quot;File exists already and/or no file type found&quot;);</instruction>
		<instruction />
		<instruction>            // Display variable in log</instruction>
		<instruction>            DOpus.Output(&quot;New file name and type: &quot; + NewFilenameFiletype);</instruction>
		<instruction />
		<instruction>            // Check if the user clicked &quot;Cancel&quot; in the dialog</instruction>
		<instruction>            if (dlg.result == 0) {</instruction>
		<instruction>                DOpus.Output(&quot;Download canceled.&quot;);</instruction>
		<instruction>                return;</instruction>
		<instruction>            }</instruction>
		<instruction>			}</instruction>
		<instruction />
		<instruction>            // Run the PowerShell command with the resolved paths and the new file name</instruction>
		<instruction>            var shell = new ActiveXObject(&quot;WScript.Shell&quot;);</instruction>
		<instruction>            shell.Run(&apos;PowerShell.exe -Command &quot;wget \\&quot;&apos; + ClipboardData + &apos;\\&quot; -OutFile \\&quot;&apos; + SourcePath + &apos;\\&apos; + NewFilenameFiletype + &apos;\\&quot;&quot;&apos;);</instruction>
		<instruction>        } else {</instruction>
		<instruction>            // Check if the file exists</instruction>
		<instruction>            if (DOpus.FSUtil().Exists(NewPathName)) {</instruction>
		<instruction>                DOpus.Output(&quot;File already exists: &quot; + FolderName + &quot;\\&quot; + Filename + Filetype);</instruction>
		<instruction />
		<instruction>                // If file exists, display dialog to overwrite or enter a new file name</instruction>
		<instruction>                var dlg = DOpus.dlg;</instruction>
		<instruction>                dlg.select = true;</instruction>
		<instruction>                var NewFilename = dlg.getstring(&quot;Overwrite or enter filename (without file extension):&quot;, Filename, 250, &quot;Save|Cancel&quot;, &quot;Filename already exists&quot;);</instruction>
		<instruction />
		<instruction>                // Display variable in log</instruction>
		<instruction>                DOpus.Output(&quot;New file name: &quot; + NewFilename);</instruction>
		<instruction />
		<instruction>                // Check if the user clicked &quot;Cancel&quot; in the dialog</instruction>
		<instruction>                if (dlg.result == 0) {</instruction>
		<instruction>                    DOpus.Output(&quot;Download canceled.&quot;);</instruction>
		<instruction>                    return;</instruction>
		<instruction>                }</instruction>
		<instruction />
		<instruction>                // Run the PowerShell command with the resolved paths and the new file name</instruction>
		<instruction>                var shell = new ActiveXObject(&quot;WScript.Shell&quot;);</instruction>
		<instruction>                shell.Run(&apos;PowerShell.exe -Command &quot;wget \\&quot;&apos; + ClipboardData + &apos;\\&quot; -OutFile \\&quot;&apos; + SourcePath + &apos;\\&apos; + NewFilename + Filetype + &apos;\\&quot;&quot;&apos;);</instruction>
		<instruction>            } else {</instruction>
		<instruction>                // Run the PowerShell command with the resolved paths and the original file name</instruction>
		<instruction>                var shell = new ActiveXObject(&quot;WScript.Shell&quot;);</instruction>
		<instruction>                shell.Run(&apos;PowerShell.exe -Command &quot;wget \\&quot;&apos; + ClipboardData + &apos;\\&quot; -OutFile \\&quot;&apos; + NewPathName + &apos;\\&quot;&quot;&apos;);</instruction>
		<instruction>            }</instruction>
		<instruction>        }</instruction>
		<instruction>    } else {</instruction>
		<instruction>        DOpus.Output(&quot;No valid URL found in clipboard.&quot;);</instruction>
		<instruction>    }</instruction>
		<instruction>}</instruction>
	</function>
</button>


Button as JScript
function OnClick(clickData) {
    clickData.func.command.deselect = false; // Prevent automatic deselection

    // Check if the clipboard contains text
    if (DOpus.GetClipFormat() !== 'text') return;

    // Save the clipboard text in a variable called ClipboardData
    var ClipboardData = DOpus.GetClip("url");

    // Check if the string ClipboardData contains the substring "://"
    if (ClipboardData.indexOf("://") > -1) {

		// Extract the domain name from the URL
        var domainPattern = /^https?:\/\/([^/?]+)/;
        var domainMatch = ClipboardData.match(domainPattern);
        var DomainName = domainMatch ? domainMatch[1].replace(/\./g, '_') : '';

        // Split the text in ClipboardData whenever a forward slash is encountered
        var URLParts = ClipboardData.split("/");
        var LastElement = URLParts[URLParts.length - 1];
		
        // Use regular expressions to match and extract the filename in case URLParts contains query parameters
        var pattern = /([^/?]+)\.([^/?]+)(\?.*)?$/;
        var matches = LastElement.match(pattern);
        var Filename = matches ? matches[1] : LastElement;
        var Filetype = matches ? "." + matches[2] : "";

		// Remove invalid characters from Filename
        Filename = Filename.replace(/[<>:"/\\|?*]/g, '');

        // Resolve the source path
        var SourcePath = clickData.func.sourcetab.path;
        var NewPathName = SourcePath + "\\" + Filename + Filetype;

        // Extract the last folder from the folder path
        var FolderParts = SourcePath.split("\\");
        var FolderName = FolderParts[FolderParts.length - 1];

        // Display variables
        DOpus.Output("-------------------------------------------------------------");
        DOpus.Output("Downloading copied URL (clipboard) to current folder path... ");
        DOpus.Output("URL: " + ClipboardData);
        DOpus.Output("File name: " + Filename);
        DOpus.Output("File type: " + Filetype);
        DOpus.Output("Folder path: " + SourcePath);
        DOpus.Output("Folder name: " + FolderName);
        DOpus.Output("File path after download: " + NewPathName);

        if (Filetype === "") {
            Filetype = ".html";
            DOpus.Output("No file type found. Not checking if file exists already.");
			
			if (Filename === "") {
					DOpus.Output("No file name found.");
					//var ElementBeforeLast = URLParts[URLParts.length - 2];
					Filename = DomainName + "-" + URLParts[URLParts.length - 2];
			
			

            // If no filetype was found, display dialog to enter file name and file type
            var dlg = DOpus.dlg;
            dlg.select = true;
            var NewFilenameFiletype = dlg.getstring("Overwrite or enter filename and file extension:", Filename + Filetype, 250, "Save|Cancel", "File exists already and/or no file type found");

            // Display variable in log
            DOpus.Output("New file name and type: " + NewFilenameFiletype);

            // Check if the user clicked "Cancel" in the dialog
            if (dlg.result == 0) {
                DOpus.Output("Download canceled.");
                return;
            }
			}

            // Run the PowerShell command with the resolved paths and the new file name
            var shell = new ActiveXObject("WScript.Shell");
            shell.Run('PowerShell.exe -Command "wget \\"' + ClipboardData + '\\" -OutFile \\"' + SourcePath + '\\' + NewFilenameFiletype + '\\""');
        } else {
            // Check if the file exists
            if (DOpus.FSUtil().Exists(NewPathName)) {
                DOpus.Output("File already exists: " + FolderName + "\\" + Filename + Filetype);

                // If file exists, display dialog to overwrite or enter a new file name
                var dlg = DOpus.dlg;
                dlg.select = true;
                var NewFilename = dlg.getstring("Overwrite or enter filename (without file extension):", Filename, 250, "Save|Cancel", "Filename already exists");

                // Display variable in log
                DOpus.Output("New file name: " + NewFilename);

                // Check if the user clicked "Cancel" in the dialog
                if (dlg.result == 0) {
                    DOpus.Output("Download canceled.");
                    return;
                }

                // Run the PowerShell command with the resolved paths and the new file name
                var shell = new ActiveXObject("WScript.Shell");
                shell.Run('PowerShell.exe -Command "wget \\"' + ClipboardData + '\\" -OutFile \\"' + SourcePath + '\\' + NewFilename + Filetype + '\\""');
            } else {
                // Run the PowerShell command with the resolved paths and the original file name
                var shell = new ActiveXObject("WScript.Shell");
                shell.Run('PowerShell.exe -Command "wget \\"' + ClipboardData + '\\" -OutFile \\"' + NewPathName + '\\""');
            }
        }
    } else {
        DOpus.Output("No valid URL found in clipboard.");
    }
}
Old Version 2023-06-19
function OnClick(clickData) {
    clickData.func.command.deselect = false; // Prevent automatic deselection

    // Check if the clipboard contains text
    if (DOpus.GetClipFormat() !== 'text') return;

    // Save the clipboard text in a variable called ClipboardData
    var ClipboardData = DOpus.GetClip("url");

    // Check if the string ClipboardData contains the substring "://"
    if (ClipboardData.indexOf("://") > -1) {

        // Split the text in ClipboardData whenever a forward slash is encountered
        var URLParts = ClipboardData.split("/");
        var LastElement = URLParts[URLParts.length - 1];

        // Use regular expressions to match and extract the filename in case URLParts contains query parameters
        var pattern = /([^/?]+)\.([^/?]+)(\?.*)?$/;
        var matches = LastElement.match(pattern);
        var Filename = matches ? matches[1] : LastElement;
        var Filetype = matches ? "." + matches[2] : "";

		// Remove invalid characters from Filename
        Filename = Filename.replace(/[<>:"/\\|?*]/g, '');

        // Resolve the source path
        var SourcePath = clickData.func.sourcetab.path;
        var NewPathName = SourcePath + "\\" + Filename + Filetype;

        // Extract the last folder from the folder path
        var FolderParts = SourcePath.split("\\");
        var FolderName = FolderParts[FolderParts.length - 1];

        // Display variables
        DOpus.Output("-------------------------------------------------------------");
        DOpus.Output("Downloading copied URL (clipboard) to current folder path... ");
        DOpus.Output("URL: " + ClipboardData);
        DOpus.Output("File name: " + Filename);
        DOpus.Output("File type: " + Filetype);
        DOpus.Output("Folder path: " + SourcePath);
        DOpus.Output("Folder name: " + FolderName);
        DOpus.Output("File path after download: " + NewPathName);

        if (Filetype === "") {
            Filetype = ".html";
            DOpus.Output("No file type found. Not checking if file exists already.");

            // If no filetype was found, display dialog to enter file name and file type
            var dlg = DOpus.dlg;
            dlg.select = true;
            var NewFilenameFiletype = dlg.getstring("Overwrite or enter filename and file extension:", Filename + Filetype, 250, "Save|Cancel", "File exists already and/or no file type found");

            // Display variable in log
            DOpus.Output("New file name and type: " + NewFilenameFiletype);

            // Check if the user clicked "Cancel" in the dialog
            if (dlg.result == 0) {
                DOpus.Output("Download canceled.");
                return;
            }

            // Run the PowerShell command with the resolved paths and the new file name
            var shell = new ActiveXObject("WScript.Shell");
            shell.Run('PowerShell.exe -noexit -Command "wget \\"' + ClipboardData + '\\" -OutFile \\"' + SourcePath + '\\' + NewFilenameFiletype + '\\""');
        } else {
            // Check if the file exists
            if (DOpus.FSUtil().Exists(NewPathName)) {
                DOpus.Output("File already exists: " + FolderName + "\\" + Filename + Filetype);

                // If file exists, display dialog to overwrite or enter a new file name
                var dlg = DOpus.dlg;
                dlg.select = true;
                var NewFilename = dlg.getstring("Overwrite or enter filename (without file extension):", Filename, 250, "Save|Cancel", "Filename already exists");

                // Display variable in log
                DOpus.Output("New file name: " + NewFilename);

                // Check if the user clicked "Cancel" in the dialog
                if (dlg.result == 0) {
                    DOpus.Output("Download canceled.");
                    return;
                }

                // Run the PowerShell command with the resolved paths and the new file name
                var shell = new ActiveXObject("WScript.Shell");
                shell.Run('PowerShell.exe -Command "wget \\"' + ClipboardData + '\\" -OutFile \\"' + SourcePath + '\\' + NewFilename + Filetype + '\\""');
            } else {
                // Run the PowerShell command with the resolved paths and the original file name
                var shell = new ActiveXObject("WScript.Shell");
                shell.Run('PowerShell.exe -Command "wget \\"' + ClipboardData + '\\" -OutFile \\"' + NewPathName + '\\""');
            }
        }
    } else {
        DOpus.Output("No valid URL found in clipboard.");
    }
}

Icons
DownloadClipboardURLtoFolderPath_32x32 DownloadClipboardURLtoFolderPath_22x22

Related info
Here's the related Help & support discussion:
How to download a file from clipboard to folder path?
If you need/are working on a button to do something similar using IDM, wget or another download manager, feel free to post there!

2 Likes