Python Forum
Filename stamp script is needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filename stamp script is needed
#1
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.
Reply
#2
Perhaps one of: https://pypi.org/search/?q=timestamp
Reply
#3
(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?
Reply
#4
Please show what you have for code so far.
Reply
#5
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?
Reply
#6
(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.
Reply
#7
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()))
Reply
#8
(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.
Reply
#9
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())
Reply
#10
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to map by time stamp and name SriRajesh 0 1,110 Apr-09-2022, 12:49 PM
Last Post: SriRajesh
  extract zip from .msg files and saving according to date stamp of email natjo 3 2,883 Aug-13-2020, 09:35 PM
Last Post: bowlofred
  copy with new time stamp leveex 0 1,758 Mar-29-2020, 08:46 PM
Last Post: leveex
  Manipulating the filename of an output script mckinneycm 4 11,825 Jan-15-2020, 07:29 PM
Last Post: mckinneycm
  How to save latest time stamp in a file? redwood 12 7,053 Jul-11-2019, 11:03 AM
Last Post: redwood

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020