Posts: 35
Threads: 13
Joined: Oct 2017
I don't know which Python library is needed to read the path and file name of a text file open on screen and in focus!
The focused file needs to be stamped with the path and file name using Python coding. I am incorporating this code into an AutoKey script which type the filename in the onscreen document.
Posts: 12,033
Threads: 486
Joined: Sep 2016
Posts: 35
Threads: 13
Joined: Oct 2017
(Apr-07-2021, 09:26 AM)Larz60+ Wrote: Perhaps one of: https://pypi.org/search/?q=timestamp
Thanks for the link. I searched them but found no code sample that reads the path & name of an active onscreen file. This is so basic, and it has me stumped in Python on Linux. In Windows, this feature is part of text and code processors. Should this not be the system variable which I can parse?
Posts: 12,033
Threads: 486
Joined: Sep 2016
Please show what you have for code so far.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Files aren't onscreen, windows are. File data may be used to draw some of the information in that window, but
that doesn't mean there's a one-to-one relationship between files and windows.
Unfortunately, GUIs aren't standardized in a cross-platform way. I don't know any way to query a window and have the controlling app respond with the filename associated with that window.
Or are you talking about a window that's part of your application?
Posts: 35
Threads: 13
Joined: Oct 2017
(Apr-07-2021, 04:33 PM)bowlofred Wrote: Files aren't onscreen, windows are. File data may be used to draw some of the information in that window, but
that doesn't mean there's a one-to-one relationship between files and windows.
Unfortunately, GUIs aren't standardized in a cross-platform way. I don't know any way to query a window and have the controlling app respond with the filename associated with that window.
Or are you talking about a window that's part of your application?
I am talking about any text file that's open on screen in any text editor.
Here is an example inserted by a Windows text editor: 2021-04-07 - Z:\media\linux-data\google-drive\shared\linux-notes.txt
This is what I have so far.
# Description, kybd = win + INS insert current date, time and path and filename
import os, time, datetime
ts = time.time()
time_stamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M') + " "
# file_name =
keyboard.send_keys(time_stamp)
# keyboard.send_keys(time_stamp + "-" + file_name) Please note that "keyboard.send_keys" is the substitute for print()
If I knew how to access the system variables in Python, I am sure that one of them would have the active filename.
Posts: 1,583
Threads: 3
Joined: Mar 2020
If you mean the window "title", that is probably queryable. The distinction is that apps may not put the information you expect into the title. You only get what happens to be there.
I haven't used them (and can't test windows code for a bit), but this StackOverflow post has several suggestions.
The simplest from there would seem to be to install "pywin32" and then try
from win32gui import GetWindowText, GetForegroundWindow
print(GetWindowText(GetForegroundWindow()))
Posts: 35
Threads: 13
Joined: Oct 2017
(Apr-07-2021, 05:28 PM)bowlofred Wrote: If you mean the window "title", that is probably queryable. The distinction is that apps may not put the information you expect into the title. You only get what happens to be there.
I haven't used them (and can't test windows code for a bit), but this StackOverflow post has several suggestions.
The simplest from there would seem to be to install "pywin32" and then try
from win32gui import GetWindowText, GetForegroundWindow
print(GetWindowText(GetForegroundWindow()))
Thanks for your post. Unfortunately, I work in Linux. Although the "title" would work in Linux as well if I knew how to access it. I am just beginning to familiarize myself with Python through Autokey.
Posts: 1,583
Threads: 3
Joined: Mar 2020
I see, I thought you meant otherwise by "Windows text editor". There are several potential Linux solutions in that link. This one requires no additional modules (but uses xprop ).
import sys
import os
import subprocess
import re
def get_active_window_title():
root = subprocess.Popen(['xprop', '-root', '_NET_ACTIVE_WINDOW'], stdout=subprocess.PIPE)
stdout, stderr = root.communicate()
m = re.search(b'^_NET_ACTIVE_WINDOW.* ([\w]+)$', stdout)
if m != None:
window_id = m.group(1)
window = subprocess.Popen(['xprop', '-id', window_id, 'WM_NAME'], stdout=subprocess.PIPE)
stdout, stderr = window.communicate()
else:
return None
match = re.match(b"WM_NAME\(\w+\) = (?P<name>.+)$", stdout)
if match != None:
return match.group("name").strip(b'"')
return None
if __name__ == "__main__":
print(get_active_window_title())
Posts: 35
Threads: 13
Joined: Oct 2017
(Apr-07-2021, 05:28 PM)bowlofred Wrote: If you mean the window "title", that is probably queryable. The distinction is that apps may not put the information you expect into the title. You only get what happens to be there.
I haven't used them (and can't test windows code for a bit), but this StackOverflow post has several suggestions.
The simplest from there would seem to be to install "pywin32" and then try
from win32gui import GetWindowText, GetForegroundWindow
print(GetWindowText(GetForegroundWindow()))
Thanks for pointing this out. Window titles are the only possible source so far. I also know that in Windows OS it would be easier to implement, but I am working in Linux. I found a number of posts on StackOverflow related to the subject, but so far I was not successful in extracting anything. My Python knowledge is very limited.
|