how to get Open particular notepad close

Welcome Forums General PowerShell Q&A how to get Open particular notepad close

Viewing 5 reply threads
  • Author
    Posts
    • #146141
      Participant
      Topics: 4
      Replies: 4
      Points: 17
      Rank: Member

      Hi All,

       

      How to close currently open particular notepad

      even that notepad with save some name and close,

       

      Thanks,

      Nagaraj,

    • #146142
      Participant
      Topics: 1
      Replies: 1629
      Points: 3,061
      Helping Hand
      Rank: Community Hero

      As far as I know Notepad does not have an API you could use for that. You could try to use AutoHotkey or AutoIt to do something like this, but that does not belong here. 😉

    • #146145
      js
      Participant
      Topics: 27
      Replies: 716
      Points: 1,878
      Helping Hand
      Rank: Community Hero
      get-process notepad | where mainwindowtitle -eq 'test.ps1 - Notepad' | stop-process
      
    • #146151
      Participant
      Topics: 1
      Replies: 1629
      Points: 3,061
      Helping Hand
      Rank: Community Hero

      ...

      But that wouldn't save an open file with some name, or would it?

      • #146154
        js
        Participant
        Topics: 27
        Replies: 716
        Points: 1,878
        Helping Hand
        Rank: Community Hero

        Nope. I think notepad++ autosaves.

      • #146160
        Participant
        Topics: 1
        Replies: 1629
        Points: 3,061
        Helping Hand
        Rank: Community Hero

        OK, let's wait for Nagaraj's answer. 😉

      • #146468
        Participant
        Topics: 4
        Replies: 4
        Points: 17
        Rank: Member

        Thanks for all your replies

        get-process notepad | where mainwindowtitle -eq 'test.ps1 – Notepad' | stop-process  –> here where we need to define the unsaved notepad file name ?

        so such case I just want to close the particular notepad file (already saved one open status – with that saved notepad filename to close), how can I write that things, please suggest me

         

        Thanks,

        Nagaraj,

         

    • #146201
      Senior Moderator
      Topics: 8
      Replies: 1140
      Points: 3,921
      Helping Hand
      Rank: Community Hero

      Hmmm.. this would not be the case in upcoming Windows 10... Notepad has an autosave feature there.

    • #146229
      Participant
      Topics: 2
      Replies: 1000
      Points: 1,979
      Helping Hand
      Rank: Community Hero

      As for …

      How to close currently open particular notepad

      … what does this mean to you?

      All opened or the current one that has focus / in the foreground (which is almost impossible to do BTW – even if you check by StartTime).

      As noted by 'js', that is a way to get the isolated instance by that name. Yet, you can do a bit more.
      Use what 'js' gave, but you need to know what else you need to pull and there really is not much else useful for what you are after. The most relevant is the below.

      Get-Process -Name Notepad | 
      Select Name, MainWindowTitle, Responding, StartTime
      
      Name    MainWindowTitle        Responding StartTime
      ----    ---------------        ---------- ---------
      notepad Untitled - Notepad           True 3/22/2019 11:02:38 AM
      notepad Untitled - Notepad           True 3/22/2019 11:16:57 AM
      notepad TestFile.txt - Notepad       True 3/22/2019 11:35:21 AM
      

      So, you check for all titles that are either 'Untitled – Notepad' or 'named'.
      Of course, named means the file has been saved at least once.

      However, for those, 'Untitled – Notepad ones, you may want to check for content before closing it, as the user may not yet have had time to save it. Thus you need to save it for them with some name that you report back to them so that can find it again.

      So, anything with a name, save. Anything else should be checked and saved with say a timestampe and the close. The automation of this as pointed out by Olaf, use AutoIT, or you can use SendKeys. They later is a bit more quirky (timing, etc.), especially if any of these are minimize, and SendKeys chokes until you restore or maximize those windows.

      So here is how I've handled such use cases, but again, it can get really quirky. No guarantees at all from system to system you would target with it.

      ### Automating notepad checks
      
      Clear-Host
      
      Function Set-WindowStyle 
      {
          param
          (
              [Parameter()]
              [ValidateSet('FORCEMINIMIZE', 'HIDE', 'MAXIMIZE', 'MINIMIZE', 'RESTORE', 
                           'SHOW', 'SHOWDEFAULT', 'SHOWMAXIMIZED', 'SHOWMINIMIZED', 
                           'SHOWMINNOACTIVE', 'SHOWNA', 'SHOWNOACTIVATE', 'SHOWNORMAL')]
              $Style = 'SHOW',
              [Parameter()]
              $MainWindowHandle = (Get-Process -Id $pid).MainWindowHandle
          )
      
          $WindowStates = @{
              FORCEMINIMIZE   = 11; HIDE            = 0
              MAXIMIZE        = 3;  MINIMIZE        = 6
              RESTORE         = 9;  SHOW            = 5
              SHOWDEFAULT     = 10; SHOWMAXIMIZED   = 3
              SHOWMINIMIZED   = 2;  SHOWMINNOACTIVE = 7
              SHOWNA          = 8;  SHOWNOACTIVATE  = 4
              SHOWNORMAL      = 1
          }
          Write-Verbose ("Set Window Style {1} on handle {0}" -f $MainWindowHandle, $($WindowStates[$style]))
      
          $Win32ShowWindowAsync = Add-Type –memberDefinition @” 
          [DllImport("user32.dll")] 
          public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
      “@ -name “Win32ShowWindowAsync” -namespace Win32Functions –passThru
      
          $Win32ShowWindowAsync::ShowWindowAsync($MainWindowHandle, $WindowStates[$Style]) | Out-Null
      }
      
      
      
      [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic") | Out-Null
      [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms") | Out-Null
      
      
      Get-Process -Name Notepad | 
      ForEach {
          If($PSItem.MainWindowTitle -NotMatch 'Untitled')
          {
              "The process $($PSItem.MainWindowTitle) with start time of $($PSItem.StartTime) has been saved at least once."
              'Saving the file in its current state to the current working directory.'
              Set-WindowStyle RESTORE $PSItem.MainWindowHandle
              [Microsoft.VisualBasic.Interaction]::AppActivate($PSItem.ID)
              Start-Sleep -Seconds 2
              [System.Windows.Forms.SendKeys]::SendWait("%(fs)")
              Start-Sleep -Seconds 2
              [System.Windows.Forms.SendKeys]::SendWait("%{F4}")
              "`n"
          }
          Else
          {
              Write-Warning -Message "
              The process $($PSItem.MainWindowTitle) with start time of $($PSItem.StartTime) is not saved.
              Saving the file using the title and start time to the current working directory."
              Set-WindowStyle RESTORE $PSItem.MainWindowHandle
              [Microsoft.VisualBasic.Interaction]::AppActivate($PSItem.ID)
              Start-Sleep -Seconds 2
              [System.Windows.Forms.SendKeys]::SendWait("%(fa)")
              Start-Sleep -Seconds 2
              [System.Windows.Forms.SendKeys]::SendWait("$((($PSItem.MainWindowTitle) -split '-')[0])$(($PSItem.StartTime).ToString('ddMMyyyy@hhmmss'))")
              Start-Sleep -Seconds 3
              [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
              [System.Windows.Forms.SendKeys]::SendWait("%{F4}")
              "`n"
          }
      }
      
Viewing 5 reply threads
  • The topic ‘how to get Open particular notepad close’ is closed to new replies.