2

(Active - the file whose window is in the foreground.)

I am working on a problem which is to extract the text in the current active pdf (.pdf), word (.doc, .docx), or notepad (.txt) file in real time. I have tried JNA, and got the active file name (e.g., a.pdf) and exe name, but not the file path (e.g., C:/Desktop/.../a.pdf.) I need the full file path here, because I can read the corresponding file and extract the text in it once I know its path. (I do not need the exe path, e.g., C:/Program Files/adobe.exe.)

I have checked the doc of JNA, but not found a method which can provide the path of current active file (not a Java application).

Alternatively, I think maybe some package can provide the full path of the current file/icon in focus (e.g., I click a.pdf before I open it and my mouse will focus on a.pdf.) I tried JNI, but still not found such a method.

Could you please provide some suggestions about how to get the path of the current active file (not a Java application) using Java or how to get the path of the current file/icon in mouse focus?

Thanks a lot!

Alvin
  • 21
  • 1
  • 2
  • please, define what you mean by "active file". What does "active file" mean? – DallaRosa May 30 '12 at 02:49
  • I mean the file whose window is in the foreground. – Alvin May 30 '12 at 02:51
  • Let me see if I got it right. You want to get the path of the active application open. even if that application is not your own. Plus, you want to get the path of the file opened by that application even though that application is not your own. Is that correct? – DallaRosa May 30 '12 at 02:53
  • I think that is completely application-specific (could be more than one file, or no file at all, right?) and not something the OS could tell you in a generic way. Does it only need to work for Acrobat on Windows? – Thilo May 30 '12 at 02:54
  • Thilo: Yeah, that's why I'm asking him to confirm if that's really what he want. because I mean, depending on the appplication could there be no open file and btw, it's not like these apps are providing an API for you to get that kind of information. It looks to me much like you have to kind of exploit some vulnerability in the application to extract that kind of information. (correct me if I'm wrong) – DallaRosa May 30 '12 at 02:57
  • Plus it's gonna be OS dependent too. – DallaRosa May 30 '12 at 02:57
  • Yes, the applications/files are not my Java applications/files. I think I only need to get the path of the file (e.g., a.pdf) opened by an application. I can get the path of the active application (e.g., acrobe.exe) already. – Alvin May 30 '12 at 02:58
  • I think it should be OS dependent. Therefore, I only need to work on pdf, doc/docx, and txt files. – Alvin May 30 '12 at 02:59
  • I think to return the full path of an icon in mouse focus can be easier. Could you please provide some suggestions? – Alvin May 30 '12 at 03:00
  • The full path of an icon is also dependent on a bunch of stuff. depending on the image, it could be encoded in the executable itself and then it wouldn't have a path of it's own more than it would have a position in the memory. Are you going to work only with windows? is that specific to just one application? – DallaRosa May 30 '12 at 03:06
  • @DallaRosa: Yes, I only need to work with Windows and get the full path of pdf/doc/docx/txt files once you open them. – Alvin May 30 '12 at 03:10

1 Answers1

1

On unix, you can get the files opened by a process using the lsof command. I searched a bit for something similar on Windows and came up with this thread:

How can I determine whether a specific file is open in Windows?

the equivalent of lsof -p pid

is combined output from sysinternals handle and listdlls, ie

handle -p pid

listdlls -p pid

you can find out pid with sysinternals pslist

if you're not used to the unix terminology, "pid" means the process id, the number that identify each process. find out the pid for acrobat and then get the handles for the process and you should be able to get all the files opened by acrobat.

DISCLAIMER: I'm a Mac/Linux user so I haven't tried the commands above!

Community
  • 1
  • 1
DallaRosa
  • 5,737
  • 2
  • 35
  • 53
  • let me know if this helped. If I find any new info I'll let you know :) – DallaRosa May 30 '12 at 03:29
  • @Garrett Hall: Hi Garrett, I see your answer to the question "Getting active window information in Java", which is great and helpful. But I have some questions about the code: 1. Is GetModuleBaseNameW() a method in an interface? I cannot find that method in the JNA document. 2. I also cannot find method GetWindowTextW() in the JNA document. I can only find GetWindowText(). 3. Almost all the methods in the User32 interface do not work. Could you please explain why? (The JNA document is incorrect?) 4. Could you please help me take a look at the problem in this post? Thanks a lot! – Alvin May 30 '12 at 21:18