How to display current active users? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

How to display current active users?

Post by Krd » 04 Jun 2023, 13:26

Hey folks!

If a script named 'MyScript.ahk' is shared via OneDrive (Sync), allowing multiple users to simultaneously run the script, how can I display a list of all users currently executing 'MyScript.ahk'?

Currently, the method I am using only shows each user their own username and not the usernames of others running the script.

Code: Select all

MyGui := Gui()
MyGui.Add("DropDownList", , [A_UserName])
MyGui.Show
Some magical for loop or any other methods to make this work?

Thanks in advance :)

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: How to display current active users?

Post by mikeyww » 04 Jun 2023, 13:28

So you want to built a LAN chat program. You could store the user names in an INI file, deleting them upon exit. It isn't foolproof but can provide an idea. There are ways to add status updates based on idle times, and so on.

Explained: IniWrite

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to display current active users?

Post by Krd » 05 Jun 2023, 01:05

Thanks for the reply.

I thought there might be an easier method. Does what I'm asking for count as complicated as making a chat program?

My goal was simply to display usernames for showcase purposes, nothing more.

If that's the case, then I'm done with this for the next 10 years as the pro doesn't even know what INI is yet. :)

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: How to display current active users?

Post by mikeyww » 05 Jun 2023, 05:46

I was only guessing about the chat. :)

Actually, INI is easy, or you could just use a plain text file that stores the user names.

1. When the script runs, add the user name to the file.
2. When the script exits, delete the user name from the file.
3. Use the file to populate your display.

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to display current active users?

Post by Krd » 05 Jun 2023, 11:49

Too complicated to write, read and delete all the time.

Can't a map do this?

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: How to display current active users?

Post by mikeyww » 05 Jun 2023, 12:02

Each script runs in its own local memory. Sharing information among scripts on different computers-- regarding whether they are running a certain program-- would require sending a message, writing to a file, etc. In my opinion, sending a message is more complicated than writing to a file, but other perspectives may vary. I cannot think of anything easier or less complicated than reading a file, but others here might know something simpler.

RussF
Posts: 1237
Joined: 05 Aug 2021, 06:36

Re: How to display current active users?

Post by RussF » 05 Jun 2023, 13:42

It can get even more complicated very quickly. What happens if the user doesn't exit the program cleanly? If it's not a GUI -based script, the users are not likely to remember to properly exit the script before powering down for the day; or if the system crashes and they are forced to reboot, but don't need to use the program for the rest of the day. In both those cases, the log file will show them logged in when in fact, they are not.

You could write a sort of "heartbeat" routine where each running copy of the script would periodically (let's say once per minute) write a record to the file (.ini would probably be better here) with a timestamp attached to the user name. The "control" program that monitors this, would then compare the timestamp to the current time and if it is older than a minute, it would know that it was invalid and delete it from the file.

The log file would have to reside somewhere that all users have read-write access to - I'm assuming that is the OneDrive. What happens when the user who knows what they are doing decides to copy the script to their local PC and run it without being logged into the OneDrive?

These are some (but by no means all) of the possible scenarios that must be accounted for in advance. I guess it all depends on how important or critical this information is as to whether it is worth doing.

Russ

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to display current active users?

Post by Krd » 06 Jun 2023, 01:45

Hmm, thought this was easy feed for pros. :)

I just want something that works when script is running.
The script is synced and no one can run it by downloading it. :)

Why do I always tend to ask for complicated scripts? :crazy:

With INI I had to delete every key upon reload so I gave it up.

I let this be open for a while if some pros got the time to spoon feed me. Because I will never be able to do it on my own.

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: How to display current active users?  Topic is solved

Post by mikeyww » 06 Jun 2023, 06:54

Code: Select all

; This script displays a list of networked users of this script.
#Requires AutoHotkey v2.0
usersDir      := 'W:\AHKusers'  ; Directory where user files will be stored
updateFreqSec := 5              ; Update frequency
fileName      := A_ComputerName ; One file is created for each user
fileContents  := A_UserName
fontSize      := 10
; --------------------------------------
users := Gui(, 'Users'), users.SetFont('s' fontSize)
LB    := users.AddListBox('w230 r5')
ProcessSetPriority 'B'
SetTimer updateUsers, 1000 * updateFreqSec
updateUsers(), users.Show()

F3::users.Show

updateUsers() {
 DirCreate usersDir
 fileObj := FileOpen(usersDir '\' fileName, 'w')
 fileObj.Write(fileContents), fileObj.Close()
 LB.Delete
 Loop Files usersDir '\*.'
  If DateDiff(A_Now, A_LoopFileTimeModified, 'S') <= updateFreqSec
   LB.Add([A_LoopFileName]) ; Current user
}

RussF
Posts: 1237
Joined: 05 Aug 2021, 06:36

Re: How to display current active users?

Post by RussF » 06 Jun 2023, 07:25

Sweet! :clap:

Russ

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to display current active users?

Post by Krd » 07 Jun 2023, 02:12

Mikeyww, I apologize for making you to spoon-feed me. 😊

It displays the computer name. Why?

I am unable to view any files in the directory where user files are supposed to be stored.
Is it possible that Microsoft is blocking access to SharePoint files?
Is it a txt. or ahk. file?

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: How to display current active users?

Post by mikeyww » 07 Jun 2023, 06:11

With 356 posts, you are a pro. The script displays the computer name because it displays file names, and line 5 of the script defines the file name to be the computer name. Feel free to change it. The extension will be whatever you indicate it to be, on that line. The computer name itself is a text word that typically has no extension. The script currently uses this.

All of the scripts would need to use a shared directory that is accessible to all of them. You can modify the access rights, or use a different directory that is accessible to all. You can use a NAS, a drive that is already mapped, etc. You can discover that by accessing the directory from the computers. If you are trying to access a directory, Web site, server, etc. that generally requires a separate password or other authorization to access it, then the script will likely not have access to it unless you adjust the script or that resource accordingly. I have a LAN with two computers using a common drive that is shared via a standard Windows drive mapping. Scripts on additional computers using the same drive would also work.

If you have difficulty with the file modification times, you can add a tolerance time (e.g., 2 seconds) to the DateDiff, but I think that this is unlikely to be a problem for LAN computers that synchronize their times with a time server.

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to display current active users?

Post by Krd » 07 Jun 2023, 07:59

Thanks. It worked as provided now.

However, I am unable to display it all the time since it will be a part of another script.

Is it possible to transform this GUI into a function so that users can call it instead?

Alternatively, would it be feasible to have all of this as a nested function, excluding the variables that need to be global? I'm struggling to make it work in this manner.

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: How to display current active users?

Post by mikeyww » 07 Jun 2023, 10:09

Yes, this can be put into a function. I will leave that part to you, as there will be various decisions about how many functions to use, and what the parameters should be. You could have an "initUsers" function that puts most of the stuff at the top of the script into a function via parameters or constants.

Enjoy!

Krd
Posts: 405
Joined: 10 Mar 2020, 02:46

Re: How to display current active users?

Post by Krd » 08 Jun 2023, 01:34

I will try and see how it ends when this is part of a script with 10,000+ lines of code.
Even with 5,000 posts, I will still be a noob, haha. All of those posts are just asking for help rather than offering assistance. :D
I would be a star if I had the skills that some of you guys here possess. :D

Again, thank you so much, and I appreciate you taking the time to help both noobs like me and pros! :D

User avatar
mikeyww
Posts: 26588
Joined: 09 Sep 2014, 18:38

Re: How to display current active users?

Post by mikeyww » 08 Jun 2023, 05:21

You are welcome.

A few tips if you want to turn this into a function library:
1. Get your library working before you integrate it into your longer script.
2. Use mostly function parameters and local variables instead of global variables.

Post Reply

Return to “Ask for Help (v2)”