qwert

How to determine file that's open in WordPad?

24 posts in this topic

#1 ·  Posted (edited)

Would anyone know how a script can read the path and filename of the document that is open in a WordPad window?

What I thought would be forthright now seems to be difficult, maybe impossible.  I've found a (very) few posts from three years ago that are in the ballpark of "determining the active file of a window" ... but no clear method was identified.  Maybe I've used poor search terms, but nothing has turned up.

The only registry entries I can find are for the Last 10 files ... and the current file isn't listed.

Any help will be appreciated.

 

Edited by qwert

Share this post


Link to post
Share on other sites



January 2015.rtf - WordPad

True ... but it's without the file path.  For example, I need "C:\Current Documents\January 2015.rtf" and it's nowhere to be found.

 

 

Share this post


Link to post
Share on other sites

I found sometimes (not always) the current file will be saved to a default directory and not to the original directory that the current file is from.  
Saving the current file in WordPad before running this example guarantees the full path of the current file is returned from the example.

May be someone will come along with a better way to get the path and file name of the document that is open in a WordPad window.

; Read the path and filename of the document that is open in a WordPad window.

Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

;ConsoleWrite(ProcessClose(ProcessExists("WordPad.exe")) & @LF) ; Close hidden WordPad

If WinExists("WordPad") Then
    ConsoleWrite("From Title: " & StringRegExpReplace(WinGetTitle("WordPad", ""), " - WordPad", "") & @LF)

    WinActivate(" - WordPad", "")
    WinWaitActive(" - WordPad", "")
    Send("!f") ; Open File menu
    Send("!a") ; Open SaveAs window
    WinWaitActive("Save As", "")
    Local $sRet = ControlGetText("Save As", "", "ToolbarWindow323") & "\" & ControlGetText("Save As", "", "Edit1") ; Gets Full Path & File Name.
    ConsoleWrite("Current WordPad File: " & StringRegExpReplace($sRet, "^(Address:\h)*", "") & @LF)
    ControlClick("Save As", "", "Button3") ; Close SaveAs window
EndIf


; ============ Recent File List ================
Local $sVar, $sRecentList, $iCount = 1
Do
    $sVar = RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Wordpad\Recent File List", "File" & $iCount)
    If @error Then
        ExitLoop
    Else
        $sRecentList &= $sVar & @LF
        $iCount += 1
    EndIf
Until 0
ConsoleWrite(@LF & "Recent WordPad Files:" & @LF & $sRecentList & @LF)

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

It might also be possible to get on OLE interface or object associated with wordpad, but of course a COM witchdoctor would only know or sure.

Take a look in registry, and find HKEY_CLASSES_ROOT\CLSID\{73FDDC80-AEA9-101A-98A7-00AA00374959}

It has a progid of Wordpad.Document.1

Unfortunately 

$ObjWordpad = ObjGet("", "Wordpad.Document.1")

If IsObj($ObjWordpad) Then
    MsgBox(0,0,0)
EndIf

Does not appear to work, nor does its ObjCreate counterpart.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

maybe WordPad use some kind of MRU ?

 

Edited by mLipok

Wondering who uses AutoIT and what it can be used for ?


ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2017-03-27
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Edit: This was for MS Word my mistake. 

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

maybe WordPad use some kind of MRU ?

 

try this concept:

#include <WinAPIFiles.au3>
_Example()
Func _Example()
    Local $sFilePath = RegRead ( "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Applets\Wordpad\Recent File List", "File1" )
    If Not @error then 
        MsgBox(0, '', _WinAPI_FileInUse ( $sFilePath ))
    EndIf
EndFunc

 


Wondering who uses AutoIT and what it can be used for ?


ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2017-03-27
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

This is my Concept.

Its working, but limited as I have no idea how to get an object for the current Document and such.  I am fairly new to this.  

#Include <Word.au3>
Local $sDocument = @ScriptDir & "\TestDoc.docx"
Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, $sDocument, Default, Default, True)
MsgBox(0, "", $oDoc.Path & "\" & $oDoc.Name)
_Word_Quit($oWord)

 

Edit This Works Also:

#Include <Word.au3>
Local $sDocument = @ScriptDir & "\TestDoc.docx"
Local $oWord = _Word_Create()
Local $oDoc = _Word_DocOpen($oWord, $sDocument, Default, Default, True)
MsgBox(0, "", $oDoc.FullName)
_Word_Quit($oWord)

 

Edit Again: I found what I was looking for by taking apart Waters UDF and seeing how it works, but I also actually read the first post and noticed the small detail WordPad not MS Word :(  At anyrate incase anybody wants to know how to do it for Word this is working great for me.

$oAppl = ObjGet("", "Word.Application")
$oDocument = $oAppl.ActiveDocument
MsgBox(0, "", $oDocument.FullName)

 

Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites

From the help file:

#AutoIt3Wrapper_UseX64=y ;adjust this according to your os architecture
#RequireAdmin

#include <Array.au3>
#include <WinAPI.au3>
#include <WinAPIProc.au3>

Local $aAdjust, $aList = 0

; Enable "SeDebugPrivilege" privilege for obtain full access rights to another processes
Local $hToken = _WinAPI_OpenProcessToken(BitOR($TOKEN_ADJUST_PRIVILEGES, $TOKEN_QUERY))

_WinAPI_AdjustTokenPrivileges($hToken, $SE_DEBUG_NAME, $SE_PRIVILEGE_ENABLED, $aAdjust)

; Retrieve command-line arguments for all processes the system
If Not (@error Or @extended) Then
    $aList = ProcessList("wordpad.exe")
    For $i = 1 To $aList[0][0]
        $aList[$i][1] = _WinAPI_GetProcessCommandLine($aList[$i][1])
    Next
EndIf

; Enable SeDebugPrivilege privilege by default
_WinAPI_AdjustTokenPrivileges($hToken, $aAdjust, 0, $aAdjust)
_WinAPI_CloseHandle($hToken)

_ArrayDisplay($aList, '_WinAPI_GetProcessCommandLine')

 

2 people like this

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

@UEZ: Excellent find!  Not as simple as what I'd hoped ... but it does the job.  Below is the result I got with two occurrences of WordPad open.  The first entry is for a New document that hasn't yet be saved.  The second is for an opened document.

Filepaths.thumb.PNG.21377be8c96f5bc1a718

One caveat from the help file:  "This function uses undocumented API functions and may stop working properly in future versions of Windows."

Thanks very much for your response!

 

Share this post


Link to post
Share on other sites

I was messing with Process Explorer the other day, and if you open a file for the first time you can see it in cmd, but if you open a file from inside via file open dialog it did not show up for me.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Indeed, a file opened from inside WordPad doesn't show up.  I had used a double-click operation to open the one in my earlier example,  Here's the result of a 3rd file that was just opened:

Alas.thumb.PNG.0083f2870e4f889b13722a515

So I guess I'm back to square one on a direct method.

On a related note, however, I uncovered this rather obscure feature of windows that provides a copy/paste method in some circumstances:

To copy the full path for an individual file, hold down the Shift key as you right-click the file, and then choose Copy As Path.

Copy_as_path.thumb.png.f9f64676c7437e1c4

... which does work and might form the basis of some other method.

Thanks for all the responses.

 

 

Edited by qwert

Share this post


Link to post
Share on other sites

Here's something that almost works:

; Read the path and filename of the document that is open in a WordPad window.
#include <WinAPIFiles.au3>

Opt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

If WinExists("WordPad") Then
    WinActivate(" - WordPad", "")
    WinWaitActive(" - WordPad", "")
    ConsoleWrite("From Title: " & StringRegExpReplace(WinGetTitle("WordPad", ""), " - WordPad", "") & @LF)
EndIf

$dir = _WinAPI_GetCurrentDirectory ( )
ConsoleWrite("From Dir: " & $dir & @LF)

Exit

The problem is: it reports the current directory for my script ... not for the active window.  Would anyone have a suggestion of how to redirect the GetCurrentDirectory operation?

 

 

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

The example I gave isolates the proper file name.  If the GetCurrentDirectory could be made to operate on the active window, that would give the proper path. 

I'm sorry if that wasn't clear.

 

Edited by qwert

Share this post


Link to post
Share on other sites

I still don't see how that will help you at all.

Here is a link which contains C++ code to get the Current Directory of process.

I believe translating it to AutoIt for this purpose is a waste of someone's time, so it will not be mine.

http://stackoverflow.com/questions/14018280/how-to-get-a-process-working-dir-on-windows

Do keep us informed though.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

This is a really heavy artillery. Are there no simpler way to do this? – Yury
Not to my best knowledge. –  Anton

Thanks for the link.  I think Yury and Anton have it right, though.  But maybe another method will turn up.

 

 

 

Edited by qwert

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now