Saturday, December 6, 2008

How to create an Internet Shortcut (.URL) file

How to create an Internet Shortcut (.URL) file
Unlike regular .LNK shortcuts (that point to a document or an application), Internet Shortcuts point to an URL (web document). Here's how to create an .URL file, Internet Shortcut, using Delphi.

Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
Related Resources
• How to extract the URL from an Internet Shortcut (.url) file
• How to create a standard .LNK shortcut
• Using INI files
• Get associated program icon

The Internet Shortcut object is used to create shortcuts to Internet sites or web documents. Internet shortcuts are diverse from regular shortcuts (which contain data in a binary file) that point to a document or an application. Such text files with a .URL extension have their content in INI file format.
Interent Shortcut (.URL)
To create an Internet Shortcut, simply drag and drop Web links directly to your Desktop (or in any other folder). If you are working with Internet Explorer, drag the System (the "e" on a blank document) icon from the left side of the Internet Explorer title bar to your desktop or a folder of your choice
A new file (with the .URL extension) is created; those links become shortcuts that you can e-mail and share over a network.

The easiest way to look inside an .URL file, is to open it inside Notepad. The content (in its simplest form) of an Internet Shortcut could look like this:

[InternetShortcut]
URL=http://delphi.about.com

As you can see, .URL files have an INI file format.
The URL represents the address location of the page to load. It must specify a fully qualifying URL with the format protocol://server/page.
For other possible fields, I suggest you to take a look at "An Unofficial Guide to the URL File Format"
"New ..." Internet Shortcut
Internet Shortcut to About Delphi ProgrammingYou can easily programmatically create an Internet shortcut if you have the URL of the page to which you want to link. When double-clicked, the default browser is launched and displays the site (or a web document) associated with the shortcut.

Here's a simple Delphi function to create an .URL file. The CreateInterentShortcut procedure creates a URL shortcut file with the provided file name (FileName parameter) for the given URL (LocationURL), overwriting any existing Internet Shortcut with the same name.

uses IniFiles;
...
procedure CreateInternetShortcut
(const FileName, LocationURL : string);
begin
with TIniFile.Create(FileName) do
try
WriteString(
'InternetShortcut',
'URL',
LocationURL);
finally
Free;
end;
end; (*CreateInterentShortcut*)

Here's a sample usage:

//create an .URL file named "About Delphi Programming"
//in the root folder of the C drive
//let it point to http://delphi.about.com
CreateInterentShortcut(
'c:\About Delphi Programming.URL ',
'http://delphi.about.com ');

Note 0: You could save a web page as MHT (web archive) than create an .URL shortcut to be able to access an offline version of a web document.

Note 1: You must provide a full file name, along with the .URL extension, for the FileName parameter.

Note 2: if you already have an Internet Shortcut, you are "interested in", you can easily extract the URL from an Internet Shortcut (.url) file.
Specifying the .URL Icon
One of the neater features of the .URL file format is that you can change the shortcut's associated icon. By default the .URL will carry the icon of the default browser. If you want to change the icon, you only have to add two additional fields to the .URL file, as in:

[InternetShortcut]
URL=http://delphi.about.com
IconIndex=0
IconFile=C:\MyFolder\MyDelphiProgram.exe

The IconIndex and IconFile fields let you specify the icon for the .URL shortcut. The IconFile could point to your application's exe file (IconIndex is the index of the icon as a resource inside the exe).
Internet Shortcut ... to open a regular document or an application
Being called an Internet Shortcut, an .URL file format does not permit you to use it for something else - such as a standard application shortcut.

Note that the URL field must be specified in the protocol://server/page format. For example, you could create an Internet Shortcut icon on the Desktop, that points to your program's exe file. You only need to specify the "file:///" for the protocol. When you double click on such an .URL file, your application will be executed. Here's an example of such an "Internet Shortcut":

[InternetShortcut]
URL=file:///c:\MyApps\MySuperDelphiProgram.exe
IconIndex=0
IconFile=C:\MyFolder\MyDelphiProgram.exe

Here's a procedure that places an Internet Shortcut on the Desktop, the shortcut points to the *current* application. You can use this code to create a shortcut to your program:

uses IniFiles, ShlObj;
...
function GetDesktopPath: string;
//get the location of the Desktop folder
var
DesktopPidl: PItemIDList;
DesktopPath: array [0..MAX_PATH] of Char;
begin
SHGetSpecialFolderLocation(0, CSIDL_DESKTOP, DesktopPidl);
SHGetPathFromIDList(DesktopPidl, DesktopPath);
Result := IncludeTrailingPathDelimiter(DesktopPath);
end; (*GetDesktopPath*)

procedure CreateSelfShortcut;
const
FileProtocol = 'file:///';
var
ShortcutTitle : string;
begin
ShortcutTitle := Application.Title + '.URL';

with TIniFile.Create(GetDesktopPath + ShortcutTitle) do
try
WriteString(
'InternetShortcut',
'URL',
FileProtocol + Application.ExeName);
WriteString(
'InternetShortcut',
'IconIndex',
'0');
WriteString(
'InternetShortcut',
'IconFile',
Application.ExeName);
finally
Free;
end;
end; (*CreateSelfShortcut*)

Note: simply call "CreateSelfShortcut" to create a shortcut to your program on the Desktop.
When to use
I'm using those handy .URL files with my every project. When you create a setup for your applications, include an .URL shortcut inside the Start menu - let the users have the most convenient way to visit your web site for updates or examples or help files.

Related Articles

* How to extract the URL from an Internet Shortcut (.url) file
* How to Create a Windows Shortcut (.LNK) File from Delphi Code
* Windows Files and Folders
* URL - Definition of the Term URL from the Blogging Glossary.
* What is the index.html page - default.htm index.htm - Default Web pages

No comments: