Jump to content

Recommended Posts

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
Posted
  Quote

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.

 

 

Posted

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)

 

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.

Posted (edited)

maybe WordPad use some kind of MRU ?

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 10/29/2015 at 9:59 AM, mLipok said:

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

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

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
Posted

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')

 

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted

@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!

 

Posted

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.

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:

  Quote

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
Posted

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?

 

 

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
Posted

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.

Posted (edited)
  Quote
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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...