What does this button do?
This 1-click-button checks whether the clipboard contains a URL and then creates a .url file in the current folder with a predefined file name ("subdomain_domain_tld-PageName.url"). A dialog (for renaming) is displayed only if a file with that name already exists.
Edited 2023-06-30: The button now checks if the URL contains a somewhat meaningful page/file name and if it doesn't, it uses the parent folder to generate the file name ("subdomain_domain_tld-ParentFolder.url").
It now also selects the newly created .url file.
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>Paste as .url file</label>
<icon1>/iconsets/MoreOpus12FlatIcons/SaveClipboardAsUrlFile_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> // 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> var ElementBeforeLast = URLParts[URLParts.length - 2];</instruction>
<instruction />
<instruction> // Use regular expressions to match and extract the web file name in case URLParts contains query parameters</instruction>
<instruction> var pattern = /([^/?]+)\.([^/?]+)(\?.*)?$/;</instruction>
<instruction> var matches = LastElement.match(pattern);</instruction>
<instruction> var LastUrlElementMinusExtension = matches ? matches[1] : LastElement;</instruction>
<instruction> </instruction>
<instruction> // Remove invalid characters from LastUrlElementMinusExtension and ElementBeforeLast</instruction>
<instruction> LastUrlElementMinusExtension = LastUrlElementMinusExtension.replace(/[<>:"/\\|?*]/g, '');</instruction>
<instruction> ElementBeforeLast = ElementBeforeLast.replace(/[<>:"/\\|?*]/g, '');</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 />
<instruction> // Check if LastUrlElementMinusExtension contains a meaningful word or phrase</instruction>
<instruction> var meaningfulPattern = /[a-zA-Z]{3,}/;</instruction>
<instruction> var isMeaningful = meaningfulPattern.test(LastUrlElementMinusExtension);</instruction>
<instruction />
<instruction> // Display the result</instruction>
<instruction> DOpus.Output("Is meaningful: " + isMeaningful);</instruction>
<instruction />
<instruction />
<instruction> if (isMeaningful) {</instruction>
<instruction> </instruction>
<instruction> var MeaningfulGeneratedName = DomainName + "-" + LastUrlElementMinusExtension;</instruction>
<instruction> </instruction>
<instruction> } else {</instruction>
<instruction />
<instruction> var MeaningfulGeneratedName = DomainName + "-" + ElementBeforeLast;</instruction>
<instruction />
<instruction> </instruction>
<instruction> </instruction>
<instruction> }</instruction>
<instruction />
<instruction />
<instruction />
<instruction />
<instruction> //</instruction>
<instruction> </instruction>
<instruction> var FolderPath = clickData.func.SourceTab.Path;</instruction>
<instruction />
<instruction> // Display variables</instruction>
<instruction> DOpus.Output("-------------------------------------------------------------");</instruction>
<instruction> DOpus.Output("Creating .url from the URL copied to the clipboard");</instruction>
<instruction> DOpus.Output("URL: " + ClipboardData);</instruction>
<instruction> DOpus.Output("Edited domain name: " + DomainName);</instruction>
<instruction> DOpus.Output("FTP folder containing page/file: " + ElementBeforeLast);</instruction>
<instruction> DOpus.Output("Edited page/file name: " + LastUrlElementMinusExtension);</instruction>
<instruction> DOpus.Output("Saving to folder path: " + FolderPath);</instruction>
<instruction> DOpus.Output("Saving file as: " + MeaningfulGeneratedName + ".url");</instruction>
<instruction />
<instruction> // Check if the file exists</instruction>
<instruction> if (DOpus.FSUtil().Exists(FolderPath + '\\' + MeaningfulGeneratedName + '.url')) {</instruction>
<instruction> DOpus.Output("File already exists: " + FolderPath + '\\' + MeaningfulGeneratedName + '.url');</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 UserSpecifiedFileName = dlg.getstring("Overwrite or enter filename (without file extension):", DomainName + "-" + MeaningfulGeneratedName, 250, "Save|Cancel", "Filename already exists");</instruction>
<instruction />
<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> } else {</instruction>
<instruction> // Display result of renaming in log</instruction>
<instruction> if (UserSpecifiedFileName === DomainName + "-" + MeaningfulGeneratedName){</instruction>
<instruction />
<instruction> DOpus.Output("Overwriting existing file: " + UserSpecifiedFileName + '.url');</instruction>
<instruction />
<instruction> } else {</instruction>
<instruction> DOpus.Output("Saving under file name entered by you: " + UserSpecifiedFileName + '.url');</instruction>
<instruction> }</instruction>
<instruction> }</instruction>
<instruction />
<instruction />
<instruction> // Create a .url file with the file name specified by the user</instruction>
<instruction />
<instruction> // Create a FileSystemObject</instruction>
<instruction> var objFSO = new ActiveXObject('Scripting.FileSystemObject');</instruction>
<instruction> </instruction>
<instruction />
<instruction> // Create a new text file with the file name specified by the user</instruction>
<instruction> var objFile = objFSO.CreateTextFile(FolderPath + '\\' + UserSpecifiedFileName + '.url', true);</instruction>
<instruction />
<instruction> // Write the contents of the URL shortcut to the file</instruction>
<instruction> objFile.Write('[InternetShortcut]\r\n');</instruction>
<instruction> objFile.Write('URL=' + ClipboardData + '\r\n');</instruction>
<instruction />
<instruction> // Close the file</instruction>
<instruction> objFile.Close();</instruction>
<instruction />
<instruction> clickData.func.command.ClearFiles();</instruction>
<instruction> clickData.func.command.RunCommand("Select DATE=newest DESELECTNOMATCH");</instruction>
<instruction> //clickData.func.command.RunCommand("Select "UserSpecifiedFileName + '.url'" EXACT"); </instruction>
<instruction />
<instruction> } else {</instruction>
<instruction> // Create a FileSystemObject</instruction>
<instruction> var objFSO = new ActiveXObject('Scripting.FileSystemObject');</instruction>
<instruction />
<instruction> // Create a new text file with the generated file name</instruction>
<instruction> var objFile = objFSO.CreateTextFile(FolderPath + '\\' + MeaningfulGeneratedName + '.url', true);</instruction>
<instruction />
<instruction> // Write the contents of the URL shortcut to the file</instruction>
<instruction> objFile.Write('[InternetShortcut]\r\n');</instruction>
<instruction> objFile.Write('URL=' + ClipboardData + '\r\n');</instruction>
<instruction />
<instruction> // Close the file</instruction>
<instruction> objFile.Close();</instruction>
<instruction> </instruction>
<instruction> clickData.func.command.ClearFiles();</instruction>
<instruction> clickData.func.command.RunCommand("Select DATE=newest DESELECTNOMATCH");</instruction>
<instruction> //clickData.func.command.RunCommand("Select "MeaningfulGeneratedName + '.url'" EXACT"); </instruction>
<instruction> </instruction>
<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 JavaScript (current version)
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];
var ElementBeforeLast = URLParts[URLParts.length - 2];
// Use regular expressions to match and extract the web file name in case URLParts contains query parameters
var pattern = /([^/?]+)\.([^/?]+)(\?.*)?$/;
var matches = LastElement.match(pattern);
var LastUrlElementMinusExtension = matches ? matches[1] : LastElement;
// Remove invalid characters from LastUrlElementMinusExtension and ElementBeforeLast
LastUrlElementMinusExtension = LastUrlElementMinusExtension.replace(/[<>:"/\\|?*]/g, '');
ElementBeforeLast = ElementBeforeLast.replace(/[<>:"/\\|?*]/g, '');
// Extract the domain name from the URL
var domainPattern = /^https?:\/\/([^/?]+)/;
var domainMatch = ClipboardData.match(domainPattern);
var DomainName = domainMatch ? domainMatch[1].replace(/\./g, '_') : '';
// Check if LastUrlElementMinusExtension contains a meaningful word or phrase
var meaningfulPattern = /[a-zA-Z]{3,}/;
var isMeaningful = meaningfulPattern.test(LastUrlElementMinusExtension);
// Display the result
DOpus.Output("Is meaningful: " + isMeaningful);
if (isMeaningful) {
var MeaningfulGeneratedName = DomainName + "-" + LastUrlElementMinusExtension;
} else {
var MeaningfulGeneratedName = DomainName + "-" + ElementBeforeLast;
}
//
var FolderPath = clickData.func.SourceTab.Path;
// Display variables
DOpus.Output("-------------------------------------------------------------");
DOpus.Output("Creating .url from the URL copied to the clipboard");
DOpus.Output("URL: " + ClipboardData);
DOpus.Output("Edited domain name: " + DomainName);
DOpus.Output("FTP folder containing page/file: " + ElementBeforeLast);
DOpus.Output("Edited page/file name: " + LastUrlElementMinusExtension);
DOpus.Output("Saving to folder path: " + FolderPath);
DOpus.Output("Saving file as: " + MeaningfulGeneratedName + ".url");
// Check if the file exists
if (DOpus.FSUtil().Exists(FolderPath + '\\' + MeaningfulGeneratedName + '.url')) {
DOpus.Output("File already exists: " + FolderPath + '\\' + MeaningfulGeneratedName + '.url');
// If file exists, display dialog to overwrite or enter a new file name
var dlg = DOpus.dlg;
dlg.select = true;
var UserSpecifiedFileName = dlg.getstring("Overwrite or enter filename (without file extension):", DomainName + "-" + MeaningfulGeneratedName, 250, "Save|Cancel", "Filename already exists");
// Check if the user clicked "Cancel" in the dialog
if (dlg.result == 0) {
DOpus.Output("Download canceled.");
return;
} else {
// Display result of renaming in log
if (UserSpecifiedFileName === DomainName + "-" + MeaningfulGeneratedName){
DOpus.Output("Overwriting existing file: " + UserSpecifiedFileName + '.url');
} else {
DOpus.Output("Saving under file name entered by you: " + UserSpecifiedFileName + '.url');
}
}
// Create a .url file with the file name specified by the user
// Create a FileSystemObject
var objFSO = new ActiveXObject('Scripting.FileSystemObject');
// Create a new text file with the file name specified by the user
var objFile = objFSO.CreateTextFile(FolderPath + '\\' + UserSpecifiedFileName + '.url', true);
// Write the contents of the URL shortcut to the file
objFile.Write('[InternetShortcut]\r\n');
objFile.Write('URL=' + ClipboardData + '\r\n');
// Close the file
objFile.Close();
clickData.func.command.ClearFiles();
clickData.func.command.RunCommand("Select DATE=newest DESELECTNOMATCH");
//clickData.func.command.RunCommand("Select "UserSpecifiedFileName + '.url'" EXACT");
} else {
// Create a FileSystemObject
var objFSO = new ActiveXObject('Scripting.FileSystemObject');
// Create a new text file with the generated file name
var objFile = objFSO.CreateTextFile(FolderPath + '\\' + MeaningfulGeneratedName + '.url', true);
// Write the contents of the URL shortcut to the file
objFile.Write('[InternetShortcut]\r\n');
objFile.Write('URL=' + ClipboardData + '\r\n');
// Close the file
objFile.Close();
clickData.func.command.ClearFiles();
clickData.func.command.RunCommand("Select DATE=newest DESELECTNOMATCH");
//clickData.func.command.RunCommand("Select "MeaningfulGeneratedName + '.url'" EXACT");
}
} 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 web file name in case URLParts contains query parameters
var pattern = /([^/?]+)\.([^/?]+)(\?.*)?$/;
var matches = LastElement.match(pattern);
var WebFileName = matches ? matches[1] : LastElement;
var Filetype = matches ? "." + matches[2] : "";
// Remove invalid characters from WebFileName
WebFileName = WebFileName.replace(/[<>:"/\\|?*]/g, '');
// Extract the domain name from the URL
var domainPattern = /^https?:\/\/([^/?]+)/;
var domainMatch = ClipboardData.match(domainPattern);
var DomainName = domainMatch ? domainMatch[1].replace(/\./g, '_') : '';
var PathName = clickData.func.SourceTab.Path + '\\' + DomainName + '-' + WebFileName + '.url';
// Display variables
DOpus.Output("-------------------------------------------------------------");
DOpus.Output("Creating .url from the URL copied to the clipboard");
DOpus.Output("URL: " + ClipboardData);
DOpus.Output("Edited domain name: " + DomainName);
DOpus.Output("Edited page (file) name: " + WebFileName);
DOpus.Output("Saving to folder path: " + clickData.func.SourceTab.Path);
DOpus.Output("Saving file as: " + DomainName + "-" + WebFileName + ".url");
// Check if the file exists
if (DOpus.FSUtil().Exists(PathName)) {
DOpus.Output("File already exists: " + PathName);
// 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):", DomainName + "-" + WebFileName, 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;
}
// Create a .url file with the new file name
// Create a FileSystemObject
var objFSO = new ActiveXObject('Scripting.FileSystemObject');
// Specify the file name
var FileName = clickData.func.SourceTab.Path + '\\' + NewFileName + '.url';
// Create a new text file with the filename
var objFile = objFSO.CreateTextFile(FileName, true);
// Write the contents of the URL shortcut to the file
objFile.Write('[InternetShortcut]\r\n');
objFile.Write('URL=' + ClipboardData + '\r\n');
// Close the file
objFile.Close();
} else {
// Create a FileSystemObject
var objFSO = new ActiveXObject('Scripting.FileSystemObject');
// Create a new text file with the filename
var objFile = objFSO.CreateTextFile(PathName, true);
// Write the contents of the URL shortcut to the file
objFile.Write('[InternetShortcut]\r\n');
objFile.Write('URL=' + ClipboardData + '\r\n');
// Close the file
objFile.Close();
}
} else {
DOpus.Output("No valid URL found in clipboard.");
}
}
Icons
Related info
Note that there is a similar button available in this forum, that I haven't seen before creating this one.
Button: Create .url file with icon from URL in clipboard by Julianon
However, the other button is different in that it doesn't suggest a name but instead displays two dialog boxes, where you can choose a name and a favicon located on your file system.
So choose whichever best suits your needs.
There's also an older thread with some info that might be interesting if you're working on something similar: Paste URL from clipboard text?