Page 1 of 1 1
Topic Options
#193610 - 2009-04-28 02:00 PM Create a shortcut and replace it if it already exists
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
As asked in the shoutbox by bekickst:

 Originally Posted By: bekickst

Hi. I search for a method to create a shortcut on the user desktop or if exists replace it. Help ?
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#193611 - 2009-04-28 02:02 PM Re: Create a shortcut and replace it if it already exists [Re: Mart]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
The code below deletes the shortcut is it exists and creates a new one using the WshShortcut() UDF. The UDF is included in the code below.

Please give it a go and let us know the results.

 Code:
Break on

$userprofile = %userprofile% + "\Desktop"

If Exist($userprofile + "\SomeShortcut.lnk")
	Del $userprofile + "\SomeShortcut.lnk"
	$rc = wshShortcut("SomeShortcut", "C:\someapp.exe")
EndIf


;=-=-=-=-=-=-=-=-=-= DO NOT CHANGE ANYTHING BELOW THIS LINE.
;=-=-=-=-=-=-=-=-=-=THIS IS A UDF AND IT COMES READY FOR USE.
; wshShortCut(),   
;   
;Authors and Contributors:   
; Shawn, Radimus, Al_Po,   
; Richard Farthing, NTDOC   
;   
;Version:   
; 1.3.3   
;   
;Action:   
; Creates Shortcuts for files or Web pages   
;   
;Syntax:   
; wshShortCut($shortcutname,$targetpath,optional $arguments, optional $startdir, optional $iconpath, optional $style, optional $Description)   
;   
;Parameters:   
; SHORTCUTNAME 	Required. Name of Shortcut. If path is omitted, will be saved to desktop.   
; TARGETPATH 	Required. The path the target point to.  To ommit checking if path exists in the function, separate the target path 
;                         with ",1"   
; ARGUMENTS 	Optional. Arguments appended to TARGETPATH   
; STARTDIR 	Optional. Working Directory   
; ICONPATH 	Optional. Path to Icon Library. To specify an icon other than the first, separate the icon path with   
;                   	  ",#" where # represents the icon in the library.   
; STYLE 	Optional. 1 = default, 3 = maximized window, 7 = minimized window   
; DESCRIPTION 	Optional. Description or Comment about Shortcut   
; HOTKEY 	Optional. Keyboard Hotkey.  (Note:  Shortcut must be saved to desktop or startmenu for Hotkey to function) 
;   
;Remarks:  
; wshShortCut 1.3.3 
; - Added option to bypass TARGETPATH exist check on LNK shortcuts.  Add ,1 to the end of your Targetpath to bypass 
;   See Example below. 
; wshShortCut 1.3.2 
; - Included Option for HotKey(.hotkey)  
; wshShortCut 1.3.1  
; - Bug Fixes when creating URL shortcuts  
; wshShortCut 1.3  
; - Attempts to create directory structure to shortcut if it does not exist  
; wshShortcut 1.2:   
; - Support for NoVarsinStrings   
; - Checks for existence of TargetPath in .lnk files   
; - Included option for Description (.description)   
; wshShortcut 1.1:   
; - fixes a logic bug in wshShortCut 1.0 so you can use more than icons 0-9 in a icon library on URLS.   
; - Unless path is explicity stated in $ShortCutName, icons are created on the desktop   
; - If .lnk or .url is omitted, UDF tries to determine the shortcut type, but defaults to .lnk if it can't figure it out.   
; wshShortCut 1.0   
; http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Board=UBB12&Number=81769&Forum=UBB12&Words=wshShortcut&Match=Entire%20Phrase&Searchpage=1&Limit=25&Old=allposts&Main=81769&Search=true#Post81769   
;   
;Examples:   
; $=wshShortcut("KiXtart Web Page","http://www.kixtart.org")   
; $=wshShortcut("Notepad","%systemroot%\system32\notepad.exe")   
; $=wshshortcut("Server1","\\Server1,1") 
 

 

Function wshShortCut($shortcutname, $targetpath, optional $arguments, optional $startdir, optional $iconpath, optional $style, optional $description, optional $hotkey)
	Dim $shell, $desktop, $shortcut, $index, $iconinfo, $iconindex, $scdir
	$wshshortcut = 1
	$shell = CreateObject("wscript.shell")
	If $shell
		If UCase(Right($shortcutname, 4)) = ".URL" Or UCase(Right($shortcutname, 4)) = ".LNK"
			;do nothing 
		Else
			If UCase(Left($targetpath, 5)) = "HTTP:" Or UCase(Left($targetpath, 6)) = "HTTPS:" Or UCase(Left($targetpath, 4)) = "FTP:"
				$shortcutname = $shortcutname + ".url"
			Else
				$shortcutname = $shortcutname + ".lnk"
			EndIf
		EndIf
		If InStr($targetpath, ",")
			$targetpath = Split($targetpath, ",")[0]
		Else
			If InStr($shortcutname, ".lnk") And Not Exist($targetpath)
				Exit 2
			EndIf
		EndIf
		If InStr($shortcutname, "\") = 0
			$Desktop = $shell.SpecialFolders("Desktop")
			$shortcutname = $desktop + "\" + $shortcutname
		Else
			$scdir = SubStr($shortcutname, 1, InStrRev($shortcutname, "\"))
			If Not Exist($scdir)
				MD $scdir
				If @error
					Exit @error
				EndIf
			EndIf
		EndIf
		$shortcut = $shell.createshortcut($shortcutname)
		If $shortcut
			$shortcut.targetpath = $targetpath
			If $iconpath And InStrRev($shortcutname, ".lnk")
				$shortcut.iconlocation = $iconpath
			EndIf
			If $arguments
				$shortcut.arguments = $arguments
			EndIf
			If $startdir
				$shortcut.workingdirectory = $startdir
			EndIf
			If $style
				$shortcut.windowstyle = $style
			EndIf
			If $description And InStrRev($shortcutname, ".lnk")
				$shortcut.description = $description
			EndIf
			If $hotkey
				$shortcut.hotkey = $hotkey
			EndIf
			$shortcut.save
			If @error
				Exit @error
			EndIf
			If InStrRev($shortcutname, ".url") And $iconpath
				$index = InStrRev($iconpath, ",")
				If $index = 0
					$iconindex = 0
				Else
					$iconindex = Split($iconpath, ",")[1]
					$iconpath = Split($iconpath, ",")[0]
				EndIf
				$ = WriteProfileString($shortcutname, "InternetShortcut", "IconFile", $iconpath)
				$ = WriteProfileString($shortcutname, "InternetShortcut", "IconIndex", $iconindex)
			EndIf
			$shortcut = 0
			$wshshortcut = 0
		Else
			Exit @error
		EndIf 
	Else
		Exit @error
	EndIf 
EndFunction
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198745 - 2010-06-01 11:27 AM Re: Create a shortcut and replace it if it already exists [Re: Mart]
nocke62 Offline
Fresh Scripter

Registered: 2005-03-30
Posts: 12
Loc: Ratingen, Germany
Hello Mart,

I have a little problem your UDF.
The function is implemented in a login script.
When running the script on a PC with Windows 7 and the destination is the desktop, the icon will appear on the desktop.
After changing the destination to the path of the taskbar, the shortcut is created in the destination path, but will not be shown in the task bar.
The user running the script is admin on the local PC.

Can You give a hint, please, what mayby wrong?

Kind Regards,
Wolfgang

Top
#198746 - 2010-06-01 01:09 PM Re: Create a shortcut and replace it if it already exists [Re: nocke62]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
I don't think WshShortcut is able to do that.
If you want to pin something to the Taskbar in Windows 7 you could use one of the Pin UDF's.

UDF Library » PinTo() - Pin Shortcuts to the Start Menu or Taskbar
UDF Library » PinIt() - pin to start menu
UDF Library » Pin() - Pins an item to the Start Menu
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198747 - 2010-06-01 01:54 PM Re: Create a shortcut and replace it if it already exists [Re: Mart]
nocke62 Offline
Fresh Scripter

Registered: 2005-03-30
Posts: 12
Loc: Ratingen, Germany
Hello Mart,

yes you are right. But this was another try put an icon on the task bar.
With pinit() and pinto() I have the same problem.
Do you have knowlegde with this two functions?
Or should I post my questions to Glenn or Allen?

Regards
Wolfgang

Top
#198748 - 2010-06-01 02:41 PM Re: Create a shortcut and replace it if it already exists [Re: nocke62]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4672
Loc: The Netherlands
Hi Wolfgang,

Sorry but I never pinned anything to anything through a script.
You might be better off starting your own thread dedicated to your issues with pinning things to the taskbar in basic scripting and if you like insert a link to this thread.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 507 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.114 seconds in which 0.077 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org