Create internet (.url) shortcuts

Share your advanced PureBasic knowledge/code with the community.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Create internet (.url) shortcuts

Post by utopiomania »

This procedure creates shortcuts to files or web resources, and they pretty much works the same way
as ordinary links created using COM do, only these are simple text files, so the coding is very
straightforward and simple.

The procedure creates non-existant folders listed in the path to the shortcut, and you can
specify an icon library/index for the shortcut as well.

The demo creates a shortcut to Notepad on your desktop (#CSIDL_DESKTOPDIRECTORY) with an icon.

Code: Select all

;-PB4, 20061204, utopiomania
;creates internet shortcuts to a file or web resource
;creates folders on the fly if neccessary

procedure createShortcut(file, res.s, url.s, icon.s, index)
  ;creates internet shortcuts. res: linked to, url: shortcut path
  ;icon: the path of the icon library file, .ico, .dll or .exe
  ;index: the icon index within the icon library file
  dq.s = chr(34)
  if file
    ;links to a file
    res = "file://" + res
  else
    ;links to a web resource
    res = "http://" + res
  endIf
  if lcase(right(url, 4)) <> ".url"
    url = url + ".url"
  endIf
  ;try to create folders from the top down
  for i = 1 to len(url)
    if mid(url, i, 1) = "\"
      ;create folder:
      createDirectory(left(url, i))   
    endIf
  next
  if createFile(0, url)
    writeStringN(0, "[InternetShortcut]")
    writeStringN(0, "URL = " + dq + res + dq)
    if file
      if len(icon)
        writeStringN(0, "IconFile = " + dq + icon + dq)
        writeStringN(0, "IconIndex = " + dq + str(index) + dq)
      endIf
    endIf
    closeFile(0)
    procedureReturn 1
  endIf
  procedureReturn 0
endProcedure

procedure.s getSpecialFolder(id)
  protected path.s, *ItemId.ITEMIDLIST
  
  *itemId = #Null 
  if SHGetSpecialFolderLocation_(0, id, @*ItemId) = #NOERROR 
    path = space(#MAX_PATH) 
    if SHGetPathFromIDList_(*itemId, @path)
      if right(path, 1) <> "\"
        path + "\"
      endIf
      procedureReturn path 
    endIf 
  endIf 
  procedureReturn "" 
endProcedure 

res.s = getSpecialFolder($24) + "notepad.exe"
url.s = getSpecialFolder($10) + "SHORTCUT"
;create folders:
;url.s = getSpecialFolder($10) + "FOLDER\SHORTCUT"

createShortcut(1, res, url, res, 0)
messageRequester("!", "A SHORTCUT to Notepad was created on your desktop", #PB_MessageRequester_Ok)
end
Last edited by utopiomania on Tue Dec 05, 2006 12:46 am, edited 1 time in total.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

very nice.

I just may have a use for this, thanks for sharing with us.

cheers
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Why not create such a file directly? You just have to save this file as a *.url file!

Code: Select all

Procedure InternetShortcut(url.s, file.s)
     If LCase(GetExtensionPart(file)) <> "url" : file+".url" : EndIf
     Protected file=CreateFile(#PB_Any, file)
     If file
         WriteStringN(file, "[InternetShortcut]")
         WriteStringN(file, "URL="+url)
         CloseFile(file)
     EndIf
     ProcedureReturn file
EndProcedure


InternetShurtcut("http://www.tomt.de.vu/", "C:\MyUrl File.url")
:wink:
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Why not create such a file directly? You just have to save this file as a *.url file!

I'm not shure what you mean. The function createShortcut() do indeed create the shortcut files directly :?:
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Unfortunately, I just read your code quickly. I'm not sure what your code does do in detail. :oops:

My code, however, creates a *.url file directly to the disk, filling the file with the right content. I don't use any API, etc. So this code also works on other OS (e. g. Linux).
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

It does exactly what you say, writes a simple textfile with an .url extension to disk using plain vanilla Purebasic functions :)

The API stuff and everything else in there is just for the demo, to get the correct path to the desktop and notepad and so on.

The procedure (createShortcut() ) itself is also able to create any parent folders to the shortcut on the fly, and to specify an
icon for the shortcut as well.
chromaX
User
User
Posts: 84
Joined: Mon Apr 17, 2006 9:57 pm
Location: Switzerland

Post by chromaX »

dq.s = chr(34)
alternatively:

Code: Select all

#DQUOTE$
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Code: Select all

[InternetShortcut]
URL=file:///E:/Downloads/Dont's of game design.txt
IconIndex=0
IconFile=C:\WINDOWS\system32\url.dll

More info at:

http://www.cyanwerks.com/file-format-url.html
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

utopiomania wrote:It does exactly what you say, writes a simple textfile with an .url extension to disk using plain vanilla Purebasic functions :)
Well, then everything is OK.

Nevertheless, my procedure is a bit smaller tha yours... :wink:
PB 4.30

Code: Select all

onErrorGoto(?Fred)
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Yes AND51, your procedure is a bit smaller, and it does a bit less too, so everything's cool. :)

And thanks chromax, #DQUOTE$ / dq = 400%, Jeez, I think I'll just go to bed now.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Post by ricardo »

AND51 wrote: I don't use any API, etc. So this code also works on other OS (e. g. Linux).
But is usefull in Linux to create a url shortcut file for Windows???
Post Reply