0

I need help with Toast Notifications. I've written a script that creates and sends a Toast notification with custom buttons and protocols. The issue is that it launches apps in background. How would I go about launching them in foreground or focus?

REGISTRY KEYS FOR CUSTOM PROTOCOLS

$1 = @(
        [pscustomobject]@{name = 'Time'; Link = 'C:\Users\XXX\Desktop\Personal\Scripts\Time_sleep.BAT'}, 
        [pscustomobject]@{name = 'Zenek'; Link = 'C:\Users\XXX\Desktop\Personal\Scripts\email.bat'}, 
        [pscustomobject]@{name = 'NetEx'; Link = 'C:\Users\XXX\Desktop\Personal\Scripts\ne.bat'}, 
        [pscustomobject]@{name = 'Busy'; Link = 'C:\Users\XXX\Desktop\Personal\Scripts\busy.bat'}, 
        [pscustomobject]@{name = 'Shorts'; Link = 'C:\Users\XXX\Desktop\Personal\Scripts\Shortcuts.bat'}
    )
     $1 | % {
$RegPath = "HKCU:\Software\Classes\$($_.name)\"
New-Item -Path "$RegPath" -Force |Out-Null
New-ItemProperty -Path "$RegPath" -Name "(Default)" -Value "URL:$($_.name) Protocol" -PropertyType "String"|Out-Null
New-ItemProperty -Path "$RegPath" -Name "URL Protocol" -Value "" -PropertyType "String"|Out-Null
New-Item -Path "$RegPath\shell\open\command" -Force|Out-Null
New-ItemProperty -Path "$RegPath\shell\open\command" -Name "(Default)" -Value $_.Link -PropertyType "String"|Out-Null
    }
                                                               

EXAMPLE BAT FILE CONTENT 'SHORTCUTS.BAT'

@echo off
color b
echo List of shortcuts
echo BUSY      CTRL + ALT + C
echo MAIL      CTRL + ALT + L
echo NETEXT    CTRL + ALT + N
echo HIBERNATE CTRL + ALT + H
echo TIME      CTRL + ALT + T
echo ASSOC     CTRL + ALT + P
echo DRIVES    CTRL + ALT + D
pause

SCRIPT

function Show-Toast{
[cmdletbinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position = 0)]
[string]$Title,
[Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position = 1)]
[string]$message
)

[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]|Out-Null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime]|Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]|out-null

$guid = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
$ToastTitle = ""
$Signature = ""
$CustomHello = "Hello {0}.{1}" -f ($env:USERNAME[0].ToString()).ToUpper(), (($env:USERNAME.ToString()).Substring(1)).ToUpper()
$BannerImage = 'C:\Users\XXX\Desktop\Personal\Scripts\Toast\lake.jpg'

$template = @"
<toast scenario="reminder">>
    <visual>
        <binding template = "ToastGeneric">
         <text>$CustomHello</text>
            <text>$ToastTitle</text>
            <text placement="attribution">$Signature</text>
        
        <group>
                <subgroup>
                    <text hint-style="title" hint-wrap="true" >$Title</text>
                </subgroup>
            </group>
            <group>
                <subgroup>
                    <text hint-style="base" hint-wrap="true" >$message</text>
                </subgroup>
            </group>
            <group>
                <subgroup>
                    <text hint-style="captionSubtle" hint-wrap="true" >$Title</text>
                </subgroup>
            </group>
            <group>
                <subgroup>
                    <text hint-style="body" hint-wrap="true" >$message</text>
                </subgroup>
            </group>
            <image placement="hero" src="$BannerImage"/>

        </binding>
    </visual>
        <actions>
            <action arguments="Zenek:" content="Zenek" activationType="protocol" />
            <action arguments="NetEx:" content="NetEx" activationType="protocol"/>
            <action arguments="Busy:" content="Busy" activationType="protocol"/>
            <action arguments="Time:" content="Time" activationType="protocol"/>
            <action arguments="shorts:" content="Shorts" activationType="protocol"/>
        
            </actions>
</toast>
"@

$xml = [Windows.Data.Xml.Dom.XmlDocument]::new()
$xml.LoadXml($template)
$toast =  [Windows.UI.Notifications.ToastNotification]::new($xml)
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($guid).Show($toast)
}
 [pscustomobject]@{
 Title = "Test"
 message = "Another"
 }| Show-Toast 

How Shortcuts.bat is launched: enter image description here

versus how I want it to be launched (as an active/selected/focused window):

enter image description here

Nawad-sama
  • 151
  • 2
  • 12
  • Does this answer your question? [How to bring focus to window by process name?](https://stackoverflow.com/questions/42566799/how-to-bring-focus-to-window-by-process-name) – Scepticalist Jan 02 '23 at 10:13
  • Partially, I know I can run VBS file that will run Batch file, and that `.AppActivate` can be used. Now I have other issues. I cannot run vbs via wscript/cscript from their default location,nothing happens, however if I move the `.exe` file i can run `.vbs` successfully. Other issue is that I have very liitle knowledge of vbs and cannot determine how to use `.AppActivate` with my `.bat` file. Any thoughts? – Nawad-sama Jan 02 '23 at 11:40
  • Figured out `.AppActivate` issue. Addted `TITLE` to `.bat` and targeted that via `.AppActivate`, had to add a 1 second delay before activation. I guess the command window was loading to slow and `.AppActivate` was missing it's target. – Nawad-sama Jan 02 '23 at 11:58

0 Answers0