Page 1 of 1

Create internet (.url) shortcuts

Posted: Mon Dec 04, 2006 11:49 pm
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

Posted: Tue Dec 05, 2006 12:38 am
by rsts
very nice.

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

cheers

Posted: Tue Dec 05, 2006 1:07 pm
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:

Posted: Tue Dec 05, 2006 1:25 pm
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 :?:

Posted: Tue Dec 05, 2006 2:59 pm
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).

Posted: Tue Dec 05, 2006 3:23 pm
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.

Posted: Tue Dec 05, 2006 4:27 pm
by chromaX
dq.s = chr(34)
alternatively:

Code: Select all

#DQUOTE$

Posted: Tue Dec 05, 2006 6:13 pm
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

Posted: Tue Dec 05, 2006 6:23 pm
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:

Posted: Tue Dec 05, 2006 11:17 pm
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.

Posted: Wed Dec 06, 2006 4:01 pm
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???