2009-11-12 37 views
3

我想用posh腳本自動打開ISE中的最後一個opend文件,因此我嘗試保存這些文件的文件路徑,如下所示。如何打開ISE中最後打開的文件的起始

$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

當我關閉ISE

,exitingtest.log創建,並已 「現在退出...」, 但不創建psISElastOpenedFiles.txt。 ISE似乎在執行退出事件之前關閉所有打開的文件。

我應該使用Timer事件嗎?

回答

1

幾個月前我嘗試過這樣做,發現競賽條件阻止95%的時間工作。在處理powershell.exiting事件之前,ISE對象模型中的選項卡集合通常會被丟棄。啞,是的。可修復,沒有。

-Oisin

2

而不是保存退出,保存MRU信息時CurrentTabs和文件對象提高CollectionChanged事件。這是我正在使用的MRU ISE插件:

# 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