Thanks to @malcev for helping me figure this out
Code: Select all
/**
* Removes a list of pinned items from the Taskbar
* This function mutates the passed array and adds a status property to each file. The status property is an object
* consisting of a success boolean property and a message property with a text representation of the failure.
* Tested with AHKv2-a108. Works on Windows 10 for sure. Maybe Windows 8 also.
* @param {Array} files: An array of objects each with a display and link property
* @param {Bool=true} removeTombstones: A boolean to delete the orphaned links from the Tombstones folder after unpinning
* @return: Returns true if all pins were removed successfully, otherwise returns false
*/
UnpinFromTaskbar(files, removeTombstones := true) {
static CLSID_IStartMenuPinnedList := "{A2A9545D-A0C2-42B4-9708-A0B2BADD77C8}"
, IID_IStartMenuPinnedList := "{4CD19ADA-25A5-4A32-B3B7-347BEE5BE36B}"
, IID_IShellItem := "{43826d1e-e718-42ee-bc55-a1e261c37bfe}"
, root := A_AppData . "\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\"
tbl := ComObjCreate(CLSID_IStartMenuPinnedList, IID_IStartMenuPinnedList)
; Since an IID was specified above, tbl is a pointer (an integer).
; The pointer may be wrapped so that it will be released automatically
; when the script no longer has a reference to the wrapper object.
; See here: https://lexikos.github.io/v2/docs/commands/ComCall.htm
tbl := ComObject(13, tbl)
pIID_IShellItem := BufferAlloc(16)
DllCall("Ole32.dll\IIDFromString", "str", IID_IShellItem, "ptr", pIID_IShellItem)
status := true
for i, file in files {
pitem := 0
DllCall("Shell32.dll\SHCreateItemFromParsingName", "WStr", root . file.link, "Ptr", 0, "Ptr", pIID_IShellItem, "Ptr*", pitem)
if (!pitem) {
if (status) {
status := false
}
file.status := { success: false, message: "Taskbar pin for " . file.display . " not found!" }
Continue
}
if (!ComCall(3, tbl, "ptr", pitem)) {
file.status := { success: true, message: file.display . " successfully unpinned from Taskbar" }
}
else {
file.status := { success: false, message: "Error unpinning " . file.display . " from Taskbar" }
}
}
; Delete orphaned links directory that gets created when the programs are unpinned
if (removeTombstones) {
DirDelete(root . "Tombstones", true)
}
return status
}