Jump to content

Recommended Posts

Im am deploying an update on several computers.
All the users have the old shortcut on their desktop, but have moved it the the screenpart they want.
Updating the program, I have to delete the old shortcut and create a new one,however, I want to put the new icon on the location of the old icon.

What I want to to, is:

  1. save the location for a specific file or icon on the desktop.
  2. move the new shortcut to the saved location

I have been Googling several days, but I do not seem to get the grasp how some others are moving. saving or restoring the desktop icons.

Could someone give me some pointers ?

 

 

Link to post
Share on other sites
Link to post
Share on other sites

An example of capturing and moving a shortcuts position:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <GuiListView.au3>
#include <Array.au3>

Global $g_sSearch = "HP Support Assistant"

;~ Show all shortcut positions
Global $aShortcutIcon = _GetShortcutPos()
    If Not @error Then _ArrayDisplay($aShortcutIcon)

;~ Example for searching and moving "HP Support Assistant" shortcut position
Global $aShortcutIcon = _GetShortcutPos($g_sSearch)
    If Not @error Then _ArrayDisplay($aShortcutIcon)

;~ Move the "HP Support Assistant" shortcut to a new position
_SetShortcutPos($g_sSearch, 500, 100)

MsgBox(4096, "Check New Position", "Check " & $g_sSearch & " position has moved")

;~ Move the "HP Support Assistant" shortcut back to it's original position
_SetShortcutPos("HP Support Assistant", $aShortcutIcon[0][1], $aShortcutIcon[0][2])

Func _SetShortcutPos($_sShortcutName = "", $_iXPos = 0, $_iYPos = 0)
    If $_sShortcutName = "" Then Return
    ;~ Desktop handle
    Local $hDesktopListview = ControlGetHandle("[CLASS:Progman]", "", "[CLASS:SysListView32;INSTANCE:1]")
        If @error Then Exit
    Local $iShortcutPos = _GUICtrlListView_FindInText($hDesktopListview, $_sShortcutName)
    _GUICtrlListView_SetItemPosition($hDesktopListview, $iShortcutPos, $_iXPos, $_iYPos)
EndFunc

Func _GetShortcutPos($_sShortcutName = "")
    Local $sShortcutName
    Local $bShortcut = $_sShortcutName = "" ? False : True
    ;~ Desktop handle
    Local $hDesktopListview = ControlGetHandle("[CLASS:Progman]", "", "[CLASS:SysListView32;INSTANCE:1]")
        If @error Then Return SetError(1, 0)
    ;~ Desktop shortcut count
    Local $iShortcutCount = _GUICtrlListView_GetItemCount($hDesktopListview)
    If $iShortcutCount > 0 Then
        Local $aShortcutPos[$iShortcutCount][3]
        For $i = 0 To UBound($aShortcutPos, 1) - 1
            If $bShortcut Then
                Dim $aShortcutPos[0][3]
                $sShortcutName = _GUICtrlListView_GetItemText($hDesktopListview, $i)
                If $sShortcutName = $_sShortcutName Then
                    Dim $aShortcutPos[1][3]
                    $aShortcutPos[0][0] = $sShortcutName
                    $aShortcutPos[0][1] = _GUICtrlListView_GetItemPositionX($hDesktopListview, $i)
                    $aShortcutPos[0][2] = _GUICtrlListView_GetItemPositionY($hDesktopListview, $i)
                    Return SetError((UBound($aShortcutPos) > 0 ? 0 : 1), 0, $aShortcutPos)
                EndIf
            Else
                $aShortcutPos[$i][0] = _GUICtrlListView_GetItemText($hDesktopListview, $i)
                $aShortcutPos[$i][1] = _GUICtrlListView_GetItemPositionX($hDesktopListview, $i)
                $aShortcutPos[$i][2] = _GUICtrlListView_GetItemPositionY($hDesktopListview, $i)
            EndIf
        Next
    Else
        Return SetError(1, 0)
    EndIf
    Return SetError((UBound($aShortcutPos) > 0 ? 0 : 1), 0, $aShortcutPos)
EndFunc

 

Link to post
Share on other sites

@SubzThanks for the script, it is realy amazing. Especially because I really do not understand whats is going on and yet it works😀

My script requires to be run as admin or system and the extra amazing part is that even running as admin, it find all the shortcuts.
I have however 1 tiny question.
The list of "shortcuts" names have no extension.

Is there a way to get the list of all files on thes desktop with their extension, so I can differentiate between files with the same name but different extension ?
 

Link to post
Share on other sites

There are four columns in the list view you can use:

  • Name: Shortcuts do not show the extension, other file types do for example filename.txt or filename.msi
  • File Size: example 1.31 KB
  • File Type: example Shortcut, TXT file, File Folder etc...
  • Created Date: xx/xx/xxxx x:xx AM/PM

The following would show you the file type of each desktop item:

Func _GetShortcutPos($_sShortcutName = "")
    Local $sShortcutName
    Local $bShortcut = $_sShortcutName = "" ? False : True
    ;~ Desktop handle
    Local $hDesktopListview = ControlGetHandle("[CLASS:Progman]", "", "[CLASS:SysListView32;INSTANCE:1]")
        If @error Then Return SetError(1, 0)
    ;~ Desktop shortcut count
    Local $iShortcutCount = _GUICtrlListView_GetItemCount($hDesktopListview)
    If $iShortcutCount > 0 Then
        Local $aShortcutPos[$iShortcutCount][4]
        For $i = 0 To UBound($aShortcutPos, 1) - 1
            If $bShortcut Then
                Dim $aShortcutPos[0][4]
                $sShortcutName = _GUICtrlListView_GetItemText($hDesktopListview, $i)
                If $sShortcutName = $_sShortcutName Then
                    Dim $aShortcutPos[1][4]
                    ;~ Desktop item name
                    $aShortcutPos[0][0] = $sShortcutName
                    ;~ Desktop item x position
                    $aShortcutPos[0][1] = _GUICtrlListView_GetItemPositionX($hDesktopListview, $i)
                    ;~ Desktop item y position
                    $aShortcutPos[0][2] = _GUICtrlListView_GetItemPositionY($hDesktopListview, $i)
                    ;~ Desktop item file type
                    $aShortcutPos[0][3] = _GUICtrlListView_GetItemText($hDesktopListview, $i, 2)
                    Return SetError((UBound($aShortcutPos) > 0 ? 0 : 1), 0, $aShortcutPos)
                EndIf
            Else
                $aShortcutPos[$i][0] = _GUICtrlListView_GetItemText($hDesktopListview, $i)
                $aShortcutPos[$i][1] = _GUICtrlListView_GetItemPositionX($hDesktopListview, $i)
                $aShortcutPos[$i][2] = _GUICtrlListView_GetItemPositionY($hDesktopListview, $i)
                $aShortcutPos[$i][3] = _GUICtrlListView_GetItemText($hDesktopListview, $i, 2)
            EndIf
        Next
    Else
        Return SetError(1, 0)
    EndIf
    Return SetError((UBound($aShortcutPos) > 0 ? 0 : 1), 0, $aShortcutPos)
EndFunc

 

Link to post
Share on other sites
Posted (edited)

@SubzThe extension is only shown when the Explorer "Hide Extension" is disabled.

I have added the following snippet to enabele "Show Extension"
The change is only activated after the screen has been refreshed by sending a F5 via Controlsend.

 

$RegKey="HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
RegWrite($RegKey,"HideFileExt","REG_DWORD",0)             ;0="how ext"     1="hide ext"
ControlSend('Program Manager', '', '', '{F5}')

The above only seems to works if it has been applied before running the script as an admin.
Adding it to the admin script however give me an error.
So I am relying on the third column you added and just look up the shortcut/file type.
 

Edited by Jemboy
Link to post
Share on other sites

Yes, using _FileListToArrayRec, will yield the shortcuts/files on the desktop, but it won't be matchable to the shortcut if there are more with the same name.

E.g. if i have "accounting.docx" and "accounting.xlsx" on my desktop, both will be found as "accounting".
The only possibility to match positions of the these files, seems to be using the type in Col3.

Resp. "Micrsoft Office Word-Document" and "Micrsoft Office Excel-Document".
The only drawback is that the file type language uses the installed Windows language (in my case Dutch), however all my computers have the Dutch Windows 10 installed, so that is not really a problem for me.

@SubzWith your help, I have completed my program and for next stage, I am going to deploy it within my organization. Thanks.:)

 

image.png.48132a84ddedd244c86f9ef550b1c763.png

Link to post
Share on other sites

For future users, beware. Moving an icon directly after creation seems to fail. The icon will not be found.
To overcome this problem, I do a sleep(2500) after the schortcut creation, though on my cuurrent computers 2000ms seems enough wait.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...