Paste URL from clipboard as .url file

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() !== &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>        // 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>		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(/[&lt;&gt;:&quot;/\\|?*]/g, &apos;&apos;);</instruction>
		<instruction>		ElementBeforeLast = ElementBeforeLast.replace(/[&lt;&gt;:&quot;/\\|?*]/g, &apos;&apos;);</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 />
		<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(&quot;Is meaningful: &quot; + isMeaningful);</instruction>
		<instruction />
		<instruction />
		<instruction>		if (isMeaningful) {</instruction>
		<instruction>		</instruction>
		<instruction>		var MeaningfulGeneratedName = DomainName + &quot;-&quot; + LastUrlElementMinusExtension;</instruction>
		<instruction>		</instruction>
		<instruction>		} else {</instruction>
		<instruction />
		<instruction>		var MeaningfulGeneratedName = DomainName + &quot;-&quot; + 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(&quot;-------------------------------------------------------------&quot;);</instruction>
		<instruction>        DOpus.Output(&quot;Creating .url from the URL copied to the clipboard&quot;);</instruction>
		<instruction>        DOpus.Output(&quot;URL: &quot; + ClipboardData);</instruction>
		<instruction>        DOpus.Output(&quot;Edited domain name: &quot; + DomainName);</instruction>
		<instruction>		DOpus.Output(&quot;FTP folder containing page/file: &quot; + ElementBeforeLast);</instruction>
		<instruction>        DOpus.Output(&quot;Edited page/file name: &quot; + LastUrlElementMinusExtension);</instruction>
		<instruction>        DOpus.Output(&quot;Saving to folder path: &quot; + FolderPath);</instruction>
		<instruction>        DOpus.Output(&quot;Saving file as: &quot; + MeaningfulGeneratedName + &quot;.url&quot;);</instruction>
		<instruction />
		<instruction>        // Check if the file exists</instruction>
		<instruction>        if (DOpus.FSUtil().Exists(FolderPath + &apos;\\&apos;  + MeaningfulGeneratedName + &apos;.url&apos;)) {</instruction>
		<instruction>            DOpus.Output(&quot;File already exists: &quot; + FolderPath + &apos;\\&apos;  + MeaningfulGeneratedName + &apos;.url&apos;);</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(&quot;Overwrite or enter filename (without file extension):&quot;, DomainName + &quot;-&quot; + MeaningfulGeneratedName, 250, &quot;Save|Cancel&quot;, &quot;Filename already exists&quot;);</instruction>
		<instruction />
		<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>            } else {</instruction>
		<instruction>			// Display result of renaming in log</instruction>
		<instruction>            if (UserSpecifiedFileName === DomainName + &quot;-&quot; + MeaningfulGeneratedName){</instruction>
		<instruction />
		<instruction>			DOpus.Output(&quot;Overwriting existing file: &quot; + UserSpecifiedFileName + &apos;.url&apos;);</instruction>
		<instruction />
		<instruction>			} else {</instruction>
		<instruction>			DOpus.Output(&quot;Saving under file name entered by you: &quot; + UserSpecifiedFileName + &apos;.url&apos;);</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(&apos;Scripting.FileSystemObject&apos;);</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 + &apos;\\&apos; + UserSpecifiedFileName + &apos;.url&apos;, true);</instruction>
		<instruction />
		<instruction>            // Write the contents of the URL shortcut to the file</instruction>
		<instruction>            objFile.Write(&apos;[InternetShortcut]\r\n&apos;);</instruction>
		<instruction>            objFile.Write(&apos;URL=&apos; + ClipboardData + &apos;\r\n&apos;);</instruction>
		<instruction />
		<instruction>            // Close the file</instruction>
		<instruction>            objFile.Close();</instruction>
		<instruction />
		<instruction>        clickData.func.command.ClearFiles();</instruction>
		<instruction>		clickData.func.command.RunCommand(&quot;Select DATE=newest DESELECTNOMATCH&quot;);</instruction>
		<instruction>        //clickData.func.command.RunCommand(&quot;Select &quot;UserSpecifiedFileName + &apos;.url&apos;&quot; EXACT&quot;);	</instruction>
		<instruction />
		<instruction>        } else {</instruction>
		<instruction>            // Create a FileSystemObject</instruction>
		<instruction>            var objFSO = new ActiveXObject(&apos;Scripting.FileSystemObject&apos;);</instruction>
		<instruction />
		<instruction>            // Create a new text file with the generated file name</instruction>
		<instruction>            var objFile = objFSO.CreateTextFile(FolderPath + &apos;\\&apos; + MeaningfulGeneratedName + &apos;.url&apos;, true);</instruction>
		<instruction />
		<instruction>            // Write the contents of the URL shortcut to the file</instruction>
		<instruction>            objFile.Write(&apos;[InternetShortcut]\r\n&apos;);</instruction>
		<instruction>            objFile.Write(&apos;URL=&apos; + ClipboardData + &apos;\r\n&apos;);</instruction>
		<instruction />
		<instruction>            // Close the file</instruction>
		<instruction>            objFile.Close();</instruction>
		<instruction>			</instruction>
		<instruction>        clickData.func.command.ClearFiles();</instruction>
		<instruction>		clickData.func.command.RunCommand(&quot;Select DATE=newest DESELECTNOMATCH&quot;);</instruction>
		<instruction>        //clickData.func.command.RunCommand(&quot;Select &quot;MeaningfulGeneratedName + &apos;.url&apos;&quot; EXACT&quot;);			</instruction>
		<instruction>			</instruction>
		<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 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
SaveClipboardAsUrlFile_32x32 SaveClipboardAsUrlFile_22x22

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?

4 Likes