The Oven

Project World => Win10XPE HomePage => Topic started by: ErikW on October 14, 2020, 01:46:25 AM

Title: How to create pinable shortcuts to batch files and documents
Post by: ErikW on October 14, 2020, 01:46:25 AM
After a bit of frustration I figured out how to make shortcuts to batch files and documents that can be pinned to the task bar or start menu.  The solution wasn't obvious and the details also were difficult to get right.

Shortcuts that can be pinned to the task bar or start menu must have an executable file as a target.  In my case, I had a batch file to abort a program because the program had no "close" option and would only either restart or shut down the system on closing.  The problem was that if I created a shortcut with the batch file as the target, then it would never pin to the task bar or start menu, even though it was added in the correct [PinUtil] section of PeCmd.ini.

Here is what I used in the plugin that did not pin the shortcut to the task bar.

Code: [Select]
AddShortcut,StartMenu,%MenuFolder%,"%PE_Programs%\%ProgramFolder0%\KillHDM17.bat","Close Paragon HDM 17",,"%PE_Programs%\%ProgramFolder0%\HDM17Close.ico"
True,AddPin,TaskBar,Auto,"#$pStartMenu#$p\Programs\%MenuFolder%\Close Paragon HDM 17.lnk"

The #$pStartMenu#$p uses character escapes to pass the string %StartMenu% to PeCmd.ini to refer to the run time environment variable for the start menu location.  Without using escape characters, the %StartMenu% in the plugin would refer to a build time variable named "StartMenu".

Here is how PeCmd.ini looked when the shortcut did not pin to the task bar.

Code: [Select]
_SUB Shortcuts
LINK %Programs%\HD Tasks\Close Paragon HDM 17,X:\Program Files\Paragon Software\Hard Disk Manager 17 Advanced\KillHDM17.bat,,X:\Program Files\Paragon Software\Hard Disk Manager 17 Advanced\HDM17Close.ico
...

[PinUtil]
TaskBar6=%StartMenu%\Programs\HD Tasks\Close Paragon HDM 17.lnk
...

The target in the properties for the resulting shortcut looked like this.

Code: [Select]
"X:\Program Files\Paragon Software\Hard Disk Manager 17 Advanced\KillHDM17.bat"

Notice that the LINK command in PeCmd.ini added quotes around the target path, so the shortcut is OK and it launches the batch file.  It just cannot be pinned to the task bar because it doesn't refer to an executable file.  The same problem occurs if a ".lnk" refers to a document instead of the application that opens the document.  Pinning a ".bat" or ".doc" file doesn't work either, for the same reason.

The solution is to make the target of the shortcut refer to an executable file, and then use the arguments to specify the document (or batch file).  To do that with a batch file, the target executable has to be "cmd.exe" and the argument has to be the batch file path.  If there are other arguments needed, then add them after the batch file or document path, or wherever the target application expects them.  In my case there were no batch file arguments.

Here is how the target of the shortcut should look in order for it to be pinnable.

Code: [Select]
"%SystemRoot%\System32\cmd.exe" /c "X:\Program Files\Paragon Software\Hard Disk Manager 17 Advanced\KillHDM17.bat"

The LINK command will add the quotes around the target, but it does not add quotes for any of the arguments.  The quotes for the arguments have to be specified as part of the arguments.

Here are the correct statements to create the shortcut and pin it to the task bar.

Code: [Select]
AddShortcut,StartMenu,%MenuFolder%,"%SystemRoot%\System32\cmd.exe","Close Paragon HDM 17","/c #$q%PE_Programs%\%ProgramFolder0%\KillHDM17.bat#$q","%PE_Programs%\%ProgramFolder0%\HDM17Close.ico"
AddPin,TaskBar,Auto,"#$pStartMenu#$p\Programs\%MenuFolder%\Close Paragon HDM 17.lnk"

Notice that the AddPin statement didn't change at all.  It was never the cause of the problem. Changing it would not have done any good, even if it was changed to specify the batch file or a document directly.  The only thing that would be pinnable is a path to an executable file, but then the associated document, or in my case a batch file would not be opened.

The quotes around parameters for AddShortcut are stripped out before those parameters are passed into the macro.  So, how are the quotes added in the correct places for the arguments?  This is one of those cases to use the character escapes that include a character literal in a string without the character being interpreted as a "special" character. The #$q escape is used to insert a double quote in the argument string instead of it being a closing or opening quote for the macro parameter.  This works the same way as the #$p did for %StartMenu%.  If there are other arguments that need quotes, such as other file paths, then the quotes can be added in the same way using #$q.  Spaces can be included using #$s or you can use normal space characters with enclosing quotes around the entire arguments macro parameter like I did in the example above.

Even if you don't pin your shortcuts to the task bar or start menu in your plugin, you still may want to create pinable shortcuts so that the user can right-click on the shortcut and pin it at run time.  If you don't create a pinable shortcut, then the pin options do not appear in the right click menu.  That is an easy way to tell if the shortcut is pinable.
Title: Re: How to create pinable shortcuts to batch files and documents
Post by: Bigbadmoshe on October 15, 2020, 12:36:38 PM
Awesome detective work :).

Keep on finding these little enhancements i love them.
Title: Re: How to create pinable shortcuts to batch files and documents
Post by: APT on October 15, 2020, 01:08:31 PM
Hi

other escape codes used

Code: [Select]
#$c = Comma [,]
#$p = Percent [%]
#$q = DoubleQuote ["]
#$s = Space [ ]
#$t = Tab [ ]
#$x = NewLine
## = Sharp [#]
Title: Re: How to create pinable shortcuts to batch files and documents
Post by: PetePossum on October 21, 2020, 04:24:12 PM
Thanks for sharing.

Just wanted to add a few notes on what I did on my PE regarding taskbar pins. I know there are other and maybe more efficient ways to do this, but I was searching for simple way to safe and restore my Taskbar pins on my USB device, without modifying files or rebuilding my PE. So the script would catch all pinned items and restore them at reboot.

I re-used some code but cant give credit as I dont remember where I found it.

So here is what I did:

- created 2 cmd files, one for saving , one for restoring the pins at startup
- Added them as plugin with links in the start menu
- The restore scripts runs as part of the autoruns when starting PE

Backup Script:

rmdir /s /q "%~dp0Taskbar-Pinned-Items-Backup\TaskBar"
md "%~dp0Taskbar-Pinned-Items-Backup\TaskBar"
copy /y "%AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar" "%~dp0Taskbar-Pinned-Items-Backup\TaskBar"
reg export "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband" "%~dp0Taskbar-Pinned-Items-Backup\PinnedItems.reg" /y

Restore Script:

del "%APPDATA%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\*" /S /Q /F
copy /y "%~dp0Taskbar-Pinned-Items-Backup\TaskBar" "%AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar"
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Taskband" /f
reg import "%~dp0Taskbar-Pinned-Items-Backup\PinnedItems.reg"
Pecmd.exe KILL explorer.exe
start explorer.exe

I removed some lines here to keep the posting short.
Title: Re: How to create pinable shortcuts to batch files and documents
Post by: ErikW on November 01, 2020, 05:46:02 AM
Just wanted to add a few notes on what I did on my PE regarding taskbar pins. I know there are other and maybe more efficient ways to do this, but I was searching for simple way to safe and restore my Taskbar pins on my USB device, without modifying files or rebuilding my PE. So the script would catch all pinned items and restore them at reboot.

I appreciate the additional information about the pin configuration.  I like booting from a WIM file but the downside is that it doesn't save settings even if installed to a hard disk.  An overlay filesystem like Puppy Linux uses might be an even more convenient solution to saving settings.  The WIM can be immutable and the changes can be saved in another overlay file.