The following example creates a WshShell object and uses the
CreateShortcut method to create two shortcuts.
JScript
var WshShell = WScript.CreateObject("WScript.Shell");
strDesktop = WshShell.SpecialFolders("Desktop");
var oShellLink = WshShell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk");
oShellLink.TargetPath = WScript.ScriptFullName;
oShellLink.WindowStyle = 1;
oShellLink.Hotkey = "CTRL+SHIFT+F";
oShellLink.IconLocation = "notepad.exe, 0";
oShellLink.Description = "Shortcut Script";
oShellLink.WorkingDirectory = strDesktop;
oShellLink.Save();
var oUrlLink = WshShell.CreateShortcut(strDesktop + "\\Microsoft Web Site.url");
oUrlLink.TargetPath = "http://www.microsoft.com";
oUrlLink.Save();
VBScript
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop & "\Shortcut Script.lnk")
oShellLink.TargetPath = WScript.ScriptFullName
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+F"
oShellLink.IconLocation = "notepad.exe, 0"
oShellLink.Description = "Shortcut Script"
oShellLink.WorkingDirectory = strDesktop
oShellLink.Save
set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
oUrlLink.TargetPath = "http://www.microsoft.com"
oUrlLink.Save
The following code creates a shortcut programatically.
#COMPILE EXE
#DIM ALL
#INCLUDE "WSHOM.INC"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
' =====================================================================================
' Creates an instance of the Windows Script Host
' =====================================================================================
LOCAL pWsh AS IWshShell
pWsh = NEWCOM "WScript.Shell"
IF ISNOTHING(pWsh) THEN
MSGBOX "Error creating an instance of Windows Scripting Host"
EXIT FUNCTION
END IF
' =====================================================================================
' =====================================================================================
' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
' =====================================================================================
LOCAL pLnk AS IWshShortcut
pLnk = pWsh.CreateShortcut(UCODE$(EXE.Path$ & "Test.lnk"))
IF ISNOTHING(pLnk) THEN
MSGBOX "Error creating the shortcut"
EXIT FUNCTION
END IF
' =====================================================================================
' =====================================================================================
' Sets variuos properties and saves them to disk
' =====================================================================================
pLnk.Description = UCODE$("Hello world")
pLnk.WorkingDirectory = UCODE$(EXE.Path$)
pLnk.Arguments = UCODE$("/c")
pLnk.HotKey = UCODE$("Ctrl+Alt+e")
pLnk.IconLocation = UCODE$(EXE.Path$ & "PROGRAM.ICO,0")
pLnk.RelativePath = UCODE$(EXE.Path$)
pLnk.TargetPath = UCODE$(EXE.Path$ & "EX_WHLNK_CreateShortcut.EXE")
pLnk.WindowStyle = %WshNormalFocus
pLnk.Save
' =====================================================================================
MSGBOX "Shortcut created"
END FUNCTION
' ========================================================================================
The following code creates an url shortcut programatically.
#COMPILE EXE
#DIM ALL
#INCLUDE "WSHOM.INC"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
' =====================================================================================
' Creates an instance of the Windows Script Host
' =====================================================================================
LOCAL pWsh AS IWshShell
pWsh = NEWCOM "WScript.Shell"
IF ISNOTHING(pWsh) THEN
MSGBOX "Error creating an instance of Windows Scripting Host"
EXIT FUNCTION
END IF
' =====================================================================================
' =====================================================================================
' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
' =====================================================================================
LOCAL pLnk AS IWshURLShortcut
pLnk = pWsh.CreateShortcut(UCODE$(EXE.Path$ & "Test.lnk"))
IF ISNOTHING(pLnk) THEN
MSGBOX "Error creating the shortcut"
EXIT FUNCTION
END IF
' =====================================================================================
' =====================================================================================
' Sets the TargetPath property and saves it to disk
' =====================================================================================
pLnk.TargetPath = UCODE$(EXE.Path$ & "EX_WHLNK_CreateShortcut.EXE")
pLnk.Save
' =====================================================================================
MSGBOX "Url Shortcut created"
END FUNCTION
' ========================================================================================
Mr Roca:
I have tried both of the powerbasic versions and the second one always end with an error ???
"Instruction referenced memory that could not be read" and no link is created. The second example that sets several properties seems to work fine.
PB
Hey Paul,
Try this:
First:
' =====================================================================================
' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
' =====================================================================================
Local pLnk As IWshURLShortcut
pLnk = pWsh.CreateShortcut(UCode$(EXE.Path$ & "Test.url")) ' <<< change to .URL
If IsNothing(pLnk) Then
MsgBox "Error creating the shortcut"
Exit Function
End If
Second:
' =====================================================================================
' Sets the TargetPath property and saves it to disk
' =====================================================================================
'pLnk.TargetPath = UCODE$(EXE.Path$ & "EX_WHLNK_CreateShortcut.EXE") ' <<< change this
pLnk.TargetPath = UCode$("http://www.spirituality.com") ' <<< to a real URL string like this
pLnk.Save
' =====================================================================================
Third:
enjoy!
-John
Thanks, I thought "sure why did'nt I see that?" but it still does the same thing. The url ex. just does something wrong here. I can use the other one.
PB
Hi Paul,
try this code - it work on my PC:
#COMPILE EXE
#DIM ALL
#INCLUDE "WSHOM.INC"
#Include "shlobj.inc"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
' =====================================================================================
' Creates an instance of the Windows Script Host
' =====================================================================================
LOCAL pWsh AS IWshShell
pWsh = NEWCOM "WScript.Shell"
IF ISNOTHING(pWsh) THEN
MSGBOX "Error creating an instance of Windows Scripting Host"
EXIT FUNCTION
END IF
' =====================================================================================
' =====================================================================================
' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
' =====================================================================================
LOCAL pLnk AS IWshURLShortcut
Local strDesktop as string
Local sPath As Asciiz * %MAX_PATH
SHGetFolderPath(0, %CSIDL_DeskTop, 0, 0, sPath)
strDesktop = sPath
pLnk = pWsh.CreateShortcut(UCODE$(strDesktop & "\TestURL.url"))
IF ISNOTHING(pLnk) THEN
MSGBOX "Error creating the shortcut"
EXIT FUNCTION
END IF
' =====================================================================================
' =====================================================================================
' Sets the TargetPath property and saves it to disk
' =====================================================================================
' pLnk.TargetPath = UCODE$(EXE.Path$ & "EX_WHLNK_CreateShortcut.EXE")
pLnk.TargetPath = UCODE$("http://www.microsoft.com")
pLnk.Save
' =====================================================================================
MSGBOX "Url Shortcut created ????"
END FUNCTION
' ========================================================================================
OK, that last one did it for the url object. I don't know what is wrong with the others for me but this one works fine. Setting the shortcuts in code is already in my latest project. Not much of hint when the call does not work, though.
Did you notice that it takes 217,000 lines of code to make a shortcut to a url? Seventy-six files linked to make this one little shortcut (from log file). 48K program size so there must be a lot going on that I can't see.
Thanks everybody, I'm making shortcuts as if they were free . . .
Quote
Did you notice that it takes 217,000 lines of code to make a shortcut to a url? Seventy-six files linked to make this one little shortcut (from log file). 48K program size so there must be a lot going on that I can't see.
No, it does not:
PowerBASIC for Windows
PB/Win Version 9.05.
Copyright (c) 1996-2010 PowerBasic Inc.
Englewood, Florida USA
All Rights Reserved
Primary source: Untitled3.BAS {1671 total lines}
Target compilation: Untitled3.exe
Compile time: 0.1 seconds, at 2005200 lines/minute
1116 bytes compiled code, 8572 bytes RTLibrary,
188 bytes string literals, and 2060 bytes dgroup.
Executable stack size: 1048576 bytes.
Disk image: 13312 bytes Memory image: 10820 bytes.
Component Files:
----------------
Untitled3.BAS
C:\USERS\PEPE\WINAPI\WINAPI\WSHOM.INC
C:\USERS\PEPE\WINAPI\WINAPI\SCRRUN.INC
It is the use of "shlobj.inc" in John's example. As you can imagine, the Windows Shell is very big.
BTW neither 217,000 lines of code are used nor 76 files are linked. These are the number of lines and files parsed, which is not quite the same.
Oops! Not mine! (I only showed edits to the first posts...)
-jhm
Have a Happy Thanksgiving! :)
[ADDED LATER]
With my edits, I've got the code in message #3 to work, but I cannot get the code provided by Dubravko to write a file...
I'm totally new to objects, so I can't track down the problem, but it has me wondering: why would this source code work fine on one machine and not at all on another? Does the OS make that much of a difference? (I'm running on XP PRO SP3) Or the compiler? (I'm running PBwin 9.05)
-jhm
Here it also does not work - XP SP3. I do not really trust the WSH-Stuff for reliability.
It is realiable, as long as you have it installed, of course.
As shown in my posts earlier in this thread, I had been able to create both File and URL shortcuts back in 2010, when I was using PB Win 9.05 and the JR includes were version 117. The code I compiled was the code from messages 2 and 3 of this thread.
Now I'm trying to recompile that same code, using PBWin 10.04 and the JR includes are III/104, and I cannot get past this point:
Local pLnk As IWshShortcut
pLnk = pWsh.CreateShortcut(UCode$(Exe.Path$ & "Test of CreateFileShortcut.lnk"))
If IsNothing(pLnk) Then
MsgBox "Error creating the shortcut"
Exit Function
End If
The only difference between the two versions of WSHOM.inc is that the STRING params are now WSTRING.
So I tried to recompile using the older version of WSHOM.INC, and it worked...
Is there something I am doing wrong?
-John
Of course,
with
pLnk = pWsh.CreateShortcut(UCode$(Exe.Path$ & "Test of CreateFileShortcut.lnk"))
you are converting the strint o unicode twice.
Use:
pLnk = pWsh.CreateShortcut(Exe.Path$ & "Test of CreateFileShortcut.lnk")
...twice... doh!
I need more sleep...
Thanks, Jose,
-John