How to open recently opened files in ISE at startup

I wanted to automatically open the latest opend files in ISE using the posh script, so I tried to save the file paths of these files as follows.

$action = { $psISE.CurrentPowerShellTab.Files | select -ExpandProperty FullPath | ? { Test-Path $_ } |
Set-Content -Encoding String -Path$PSHOME\psISElastOpenedFiles.txt
Set-Content -Encoding String -Value "Now exiting..." -Path c:\exitingtest.log
}
Register-EngineEvent -SourceIdentifier Exit -SupportEvent -Action $action

When I close ISE, exitingtest.log is created and has "Now exit ...", but psISElastOpenedFiles.txt is not created. it seems that ISE closes all opening files before the output event is executed.

Should I use the Timer event?

+3
source share
2 answers

Instead of saving on exit, save the MRU information when the CurrentTabs and Files objects raise the CollectionChanged event. This is the MRU ISE addon that I use:

# Add to profile
if (test-path $env:TMP\ise_mru.txt)
{
    $global:recentFiles = gc $env:TMP\ise_mru.txt | ?{$_}
}

else
{
    $global:recentFiles = @()
}

function Update-MRU($newfile)
{
    $global:recentFiles = @($newfile) + ($global:recentFiles -ne $newfile) | Select-Object -First 10

    $psISE.PowerShellTabs | %{
        $pstab = $_
        @($pstab.AddOnsMenu.Submenus) | ?{$_.DisplayName -eq 'MRU'} | %{$pstab.AddOnsMenu.Submenus.Remove($_)}
        $menu = $pstab.AddOnsMenu.Submenus.Add("MRU", $null, $null)
        $global:recentFiles | ?{$_} | %{
            $null = $menu.Submenus.Add($_, [ScriptBlock]::Create("psEdit '$_'"), $null)
        }
    }
    $global:recentFiles | Out-File $env:TMP\ise_mru.txt
}

$null = Register-ObjectEvent -InputObject $psISE.PowerShellTabs -EventName CollectionChanged -Action {
    if ($eventArgs.Action -ne 'Add')
    {
        return
    }

    Register-ObjectEvent -InputObject $eventArgs.NewItems[0].Files -EventName CollectionChanged -Action {
        if ($eventArgs.Action -ne 'Add')
        {
            return
        }
        Update-MRU ($eventArgs.NewItems | ?{-not $_.IsUntitled}| %{$_.FullPath})
    }
}

$null = Register-ObjectEvent -InputObject $psISE.CurrentPowerShellTab.Files -EventName CollectionChanged -Action {
    if ($eventArgs.Action -ne 'Add')
    {
        return
    }
    Update-MRU ($eventArgs.NewItems | ?{-not $_.IsUntitled}| %{$_.FullPath})

}

Update-MRU
+2
source

, 95% . ISE powershell.exiting. , . Fixable, no.

-Oisin

+1

Source: https://habr.com/ru/post/1722699/


All Articles