Invoke-UltraEdit.ps1: integrate your favorite text editor with the Powershell pipeline

Less talk, more code.  As long as that code has ample inline comments, of course!

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
<#
.Synopsis
Opens the desired file(s) in UltraEdit.
    
.Description
    The file(s) you specify on the command line or the pipeline will be opened in UltraEdit. We'll launch a new
    copy of the app, but only if necessary. The pipeline supports many varieties of objects via my Get-FullPath
    script.
    
    Put this in your $profile:
    PS> . "c:\path\to\invoke-ultraedit.ps1"
    PS> set-alias ue invoke-ultraedit
    
.Parameter File
    Path to the file being opened. Any object that contains such a path as a property named FullName, Path, etc.
    will do.
        
.Example
    PS> # open foo.txt in UltraEdit
    PS> ue foo.txt
    
.Example
    PS> # open any project files that contain a HintPath element
    PS> dir -r -fil *csproj | ss hintpath | ue
        
.ReturnValue

.Link
    http://richardberg.net/blog/?p=60
    http://richardberg.net/blog/?p=57
    
.Notes
NAME: Invoke-UltraEdit.ps1
AUTHOR: COATUECAP\rberg
LASTEDIT: 03/27/2009 17:24:02

#Requires -Version 2.0
#>
function Invoke-UltraEdit
{
    [CmdletBinding()]
    Param(
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)] 
        [psobject] $file
    )
 
    Begin
    {
        # this path isn't stored anywhere in the registry - you may need to adjust it
        $ue = join-path $programFilesX86 "IDM Computer Solutions\UltraEdit\Uedit32.exe"
         
        # If UltraEdit isn't running yet, launching it will hang the console until it exits
        # Same deal if there's an elevated UltraEdit process running but our shell is not elevated
        Function HaveToStartNewEditor
        {
           # If there is at least one process whose handle we have permission to grab, we're ok.
            $haveToStart = $true
            get-process (dir $ue).basename -ea SilentlyContinue | 
           % { 
                   if ($_.Handle -ne $null) 
                       { $haveToStart = $false }
         }
   
            return $haveToStart
        }
    }
  
    Process
    {
        while (HaveToStartNewEditor)
        { 
            invoke-item $ue
            sleep 2 
        }   

        # launch with option to use existing window
        & $ue /foi ($file | get-fullpath)
    }
}

As you can probably tell, you’ll need some other code for this to compilebe interpreted successfully.

Leave a Reply