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() !== 'text') return;</instruction>
<instruction />
<instruction> // Save the clipboard text in a variable called ClipboardData</instruction>
<instruction> var ClipboardData = DOpus.GetClip("url");</instruction>
<instruction />
<instruction> // Check if the string ClipboardData contains the substring "://"</instruction>
<instruction> if (ClipboardData.indexOf("://") > -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, '_') : '';</instruction>
<instruction />
<instruction> // Split the text in ClipboardData whenever a forward slash is encountered</instruction>
<instruction> var URLParts = ClipboardData.split("/");</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 ? "." + matches[2] : "";</instruction>
<instruction />
<instruction> // Remove invalid characters from Filename</instruction>
<instruction> Filename = Filename.replace(/[<>:"/\\|?*]/g, '');</instruction>
<instruction />
<instruction> // Resolve the source path</instruction>
<instruction> var SourcePath = clickData.func.sourcetab.path;</instruction>
<instruction> var NewPathName = SourcePath + "\\" + Filename + Filetype;</instruction>
<instruction />
<instruction> // Extract the last folder from the folder path</instruction>
<instruction> var FolderParts = SourcePath.split("\\");</instruction>
<instruction> var FolderName = FolderParts[FolderParts.length - 1];</instruction>
<instruction />
<instruction> // Display variables</instruction>
<instruction> DOpus.Output("-------------------------------------------------------------");</instruction>
<instruction> DOpus.Output("Downloading copied URL (clipboard) to current folder path... ");</instruction>
<instruction> DOpus.Output("URL: " + ClipboardData);</instruction>
<instruction> DOpus.Output("File name: " + Filename);</instruction>
<instruction> DOpus.Output("File type: " + Filetype);</instruction>
<instruction> DOpus.Output("Folder path: " + SourcePath);</instruction>
<instruction> DOpus.Output("Folder name: " + FolderName);</instruction>
<instruction> DOpus.Output("File path after download: " + NewPathName);</instruction>
<instruction />
<instruction> if (Filetype === "") {</instruction>
<instruction> Filetype = ".html";</instruction>
<instruction> DOpus.Output("No file type found. Not checking if file exists already.");</instruction>
<instruction> </instruction>
<instruction> if (Filename === "") {</instruction>
<instruction> DOpus.Output("No file name found.");</instruction>
<instruction> //var ElementBeforeLast = URLParts[URLParts.length - 2];</instruction>
<instruction> Filename = DomainName + "-" + 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("Overwrite or enter filename and file extension:", Filename + Filetype, 250, "Save|Cancel", "File exists already and/or no file type found");</instruction>
<instruction />
<instruction> // Display variable in log</instruction>
<instruction> DOpus.Output("New file name and type: " + NewFilenameFiletype);</instruction>
<instruction />
<instruction> // Check if the user clicked "Cancel" in the dialog</instruction>
<instruction> if (dlg.result == 0) {</instruction>
<instruction> DOpus.Output("Download canceled.");</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("WScript.Shell");</instruction>
<instruction> shell.Run('PowerShell.exe -Command "wget \\"' + ClipboardData + '\\" -OutFile \\"' + SourcePath + '\\' + NewFilenameFiletype + '\\""');</instruction>
<instruction> } else {</instruction>
<instruction> // Check if the file exists</instruction>
<instruction> if (DOpus.FSUtil().Exists(NewPathName)) {</instruction>
<instruction> DOpus.Output("File already exists: " + FolderName + "\\" + 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("Overwrite or enter filename (without file extension):", Filename, 250, "Save|Cancel", "Filename already exists");</instruction>
<instruction />
<instruction> // Display variable in log</instruction>
<instruction> DOpus.Output("New file name: " + NewFilename);</instruction>
<instruction />
<instruction> // Check if the user clicked "Cancel" in the dialog</instruction>
<instruction> if (dlg.result == 0) {</instruction>
<instruction> DOpus.Output("Download canceled.");</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("WScript.Shell");</instruction>
<instruction> shell.Run('PowerShell.exe -Command "wget \\"' + ClipboardData + '\\" -OutFile \\"' + SourcePath + '\\' + NewFilename + Filetype + '\\""');</instruction>
<instruction> } else {</instruction>
<instruction> // Run the PowerShell command with the resolved paths and the original file name</instruction>
<instruction> var shell = new ActiveXObject("WScript.Shell");</instruction>
<instruction> shell.Run('PowerShell.exe -Command "wget \\"' + ClipboardData + '\\" -OutFile \\"' + NewPathName + '\\""');</instruction>
<instruction> }</instruction>
<instruction> }</instruction>
<instruction> } else {</instruction>
<instruction> DOpus.Output("No valid URL found in clipboard.");</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
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!