Button: Create .url file with icon from URL in clipboard

Purpose:
The JScript-in-a-button below produces a .url shortcut file in the source directory from a URL that is already in the clipboard. It also allows a distinctive favicon to be selected from a fixed favicon directory, so that in any list of .url files, each .url file may appear with its own favicon on its left.

History:
The script arises from this post involving PeterPanino, YankeeZulu, me and others, with hints and advice from Leo. It never seemed quite clear which script others should download, so I've now started with YankeeZulu's VBScript, changed it to JScript, added the ability to assign a distinctive favicon to each created .url file, and done some checking and trimming of the input .url filename.

Creating a simple text file with Ctrl+V seems so easy that I've ignored it — instead I've displayed the supposed URL from the clipboard in a popup and let the user decide whether it is a valid URL. This is an unrelated bonus, because the user can now use the button at any time to check what text, if any, is in the clipboard.

Installation:

  1. Download the button below to your computer.
  2. Put DOpus into Settings > Customise Toolbars mode, then drag the button to any toolbar or toolbar-menu.
  3. With DOpus still in Customise mode, open the button by double-clicking it (or right-click it and choose Edit).
  4. The script assumes that somewhere in your file system, you have a directory that you use to store favicons (or icons). Edit line 12 by changing C:\\ProgramData\\Favicons\\ to whatever that directory is. Do not use any extra inverted commas, even if the path has spaces, and change every occurrence of \ in the path to \\.
  5. The script also assumes that there is a "default icon" named Internet.ico in this directory. Create such an Internet.ico file, or else edit line 13 by changing "Internet" to the name (without extension) of some other .ico file that you want to use as the default favicon.

This is version 1.0, by Julianon and the others posting in this post. Date: 21/07/15
Create .url File with Icon from URL in Clipboard.dcf (9.25 KB)

Script Code:

The script code from the download above is reproduced here for reference.


function OnClick (ClickData) {

// IMPORTANT INSTRUCTIONS: 
// (a) EDIT THE PATH TO YOUR FAVICON DIRECTORY IN THE FIRST LINE OF CODE BELOW.  
// (b) ADD A DEFAULT FAVICON FILE CALLED Internet.ico TO THIS FAVICON DIRECTORY.  
//     THE NAME STEM OF THIS FAVICON FILE CAN BE CHANGED IN THE SECOND LINE OF CODE BELOW.

// STEP 1: Introduce the variables, and define the path to the favicons.

  TheFaviconDirectory = new String ("C:\\ProgramData\\Favicons\\") //EDIT THIS PATH TO YOUR FAVICONS.
  TheFaviconName = new String ("Internet") //POSSIBLY EDIT THE NAME OF THE DEFAULT FAVICON.
  
  TheURL = new String ("")
  TheURLFileName = new String ("")
  TheURLFilePath = new String (ClickData.Func.SourceTab.Path + "\\")
  TheURLFile = new Object
  var TheFaviconChoice 
  TheCancel = new String ("Continue")
  TheDlg = new Object
  TheFSO = new ActiveXObject ("Scripting.FileSystemObject");
  
// STEP 2: Input the URL from the clipboard, and check that it is text.
  if (!(DOpus.GetClipFormat == "text"))
    TheCancel = "The clip is not text.\r\nThe operation is therefore cancelled."

// STEP 3: Ask for a filename for the shortcut, trim it, and check it for validity and non-existence. 
// At each step, processing goes straight through to the error message whenever TheCancel is different from "Continue".
  if (TheCancel == "Continue") {
    TheURL = DOpus.GetClip

    TheDlg = ClickData.Func.Dlg
    TheDlg.title = "Create URL shortcut file"
    TheDlg.message = "The clipboard currently contains the following text:\r\n\r\n" + TheURL + "\r\n\r\nTo create a .url file that treats this clipboard text as a URL, enter the .url shortcut filename, without extension."
    TheDlg.buttons = "OK|Cancel"
    TheDlg.max = 256
    TheDlg.show
    TheURLFileName = TheDlg.input

    TheCancel = DoCheckFilename (TheURLFileName) // SEE FUNCTION DoCheckFileName BELOW
  }

  if (TheCancel == "Continue") {
	TheURLFilePath = TheURLFilePath + TheURLFileName + ".url"
	if (TheFSO.FileExists (TheURLFilePath))
	  TheCancel = "There is already a file in this directory named:\r\n\r\n" + TheURLFileName + ".url\r\n\r\nThe operation is therefore cancelled."
  }

// STEP 4: If things are OK, ask for the favicon filename, which is not checked.
  if (TheCancel == "Continue") {

    TheDlg = ClickData.Func.Dlg
    TheDlg.title = "Favicon name selection"
    TheDlg.message = 'Enter the favicon name, without extension.\r\n\r\nAlternatively, the default input yields "Internet.ico".'
    TheDlg.buttons = "OK|Cancel"
    TheDlg.max = 256
    TheDlg.defvalue = TheFaviconName
    TheDlg.select = true
    TheFaviconChoice = TheDlg.show
	TheFaviconName = TheDlg.input

	if (TheFaviconName == "")
	  TheFaviconName = "Internet"

	if (TheFaviconName == undefined)
	  TheCancel = "The operation was cancelled."
  }

// STEP 5: If things are still OK, create the internet shortcut file.  Otherwise issue an error message.
  if (TheCancel == "Continue") {
    TheURLFile = TheFSO.CreateTextFile(TheURLFilePath)
    TheURLFile.Write ("[InternetShortcut]\r\n")
    TheURLFile.Write ("URL=" + TheURL + "\r\n")
    TheURLFile.Write ("IconFile=" + TheFaviconDirectory + TheFaviconName + ".ico\r\n") 
    TheURLFile.Write ("IconIndex=0\r\n")
    TheURLFile.Close()
	
  } else {

    TheDlg = ClickData.Func.Dlg
    TheDlg.title = "Operation cancelled"
    TheDlg.message = TheCancel
    TheDlg.buttons = "OK"
    TheDlg.icon = "warn"
    TheDlg.show
  }
}

// THE CHECKFILENAME FUNCTION CHECKS FOR UNDEFINED OR EMPTY STRINGS AND FOR DISALLOWED CHARACTERS.
  function DoCheckFilename (TheString) {
    TheCancel = new String ("Continue")

    if (TheString == undefined) {
      TheCancel = "The operation was cancelled."
    } else {
      TheString = DoTrim (TheString) // SEE FUNCTION DoTrim BELOW
      if (TheString.length == 0) 
        TheCancel = "The filename cannot be empty or consist only of spaces.\r\n\r\nThe operation is therefore cancelled."
    }

    if (TheCancel == "Continue") {
      if (!(TheString.indexOf ("/") == -1 && TheString.indexOf ("\\") == -1 && TheString.indexOf ("<") == -1)) 
	    TheCancel = 'The characters \r\n\r\n/ \\ < > : | ? * " \r\n\r\nare not permitted in filenames, and the following is therefore not a valid filename:\r\n\r\n' + TheString
      if (!(TheString.indexOf (">") == -1 && TheString.indexOf (":") == -1 && TheString.indexOf ("|") == -1)) 
	    TheCancel = 'The characters \r\n\r\n/ \\ < > : | ? * " \r\n\r\nare not permitted in filenames, and the following is therefore not a valid filename:\r\n\r\n' + TheString
      if (!(TheString.indexOf ("?") == -1 && TheString.indexOf ("*") == -1 && TheString.indexOf ('"') == -1)) 
	    TheCancel = 'The characters \r\n\r\n/ \\ < > : | ? * " \r\n\r\nare not permitted in filenames, and the following is therefore not a valid filename:\r\n\r\n' + TheString
	}
    return TheCancel
  }  

// THE TRIM FUNCTION TRIMS SPACES FROM THE LEFT AND RIGHT OF A STRING THAT CANNOT BE UNDEFINED. 
  function DoTrim (TheString) {
    while (TheString.substr (0,1) == " ") 
      TheString = TheString.substr (1)
    while (TheString.substr (TheString.length-1) == " ") 
      TheString = TheString.substr (0, TheString.length-1)
	return TheString
  }