Find out files opened by an application Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Find out files opened by an application

Post by wetware05 » 04 Dec 2022, 13:16

Hi.

If you have several instances of notepad.exe, each one with a different file, of course! How to find out the file names of all open instances? (single instance would be WinGetTitle). Can you find out the path of the file? If I could do it, would it be valid for other applications such as video players?

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Find out files opened by an application

Post by swagfag » 04 Dec 2022, 13:22

Code: Select all

#Requires AutoHotkey v2.0-
for hwnd in WinGetList('ahk_exe notepad.exe')
	MsgBox WinGetTitle(hwnd)
+ whatever extra garbage v1 boilerplate u would need to process WinGet, List's output

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 04 Dec 2022, 13:28

swagfag wrote:
04 Dec 2022, 13:22

Code: Select all

#Requires AutoHotkey v2.0-
for hwnd in WinGetList('ahk_exe notepad.exe')
	MsgBox WinGetTitle(hwnd)
+ whatever extra garbage v1 boilerplate u would need to process WinGet, List's output
Thank you swagfag for such a quick response.

Is it possible to have autohotkey version 1.1 and 2.0 at the same time?

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Find out files opened by an application

Post by Smile_ » 04 Dec 2022, 13:29

v1

Code: Select all

WinGet, Hwnds, List, ahk_exe notepad.exe
Loop, % Hwnds {
    WinGetTitle, ThisTitle, % "ahk_id " Hwnds%A_Index%
    Msgbox, % ThisTitle
}

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 04 Dec 2022, 13:42

Smile_ wrote:
04 Dec 2022, 13:29
v1

Code: Select all

WinGet, Hwnds, List, ahk_exe notepad.exe
Loop, % Hwnds {
    WinGetTitle, ThisTitle, % "ahk_id " Hwnds%A_Index%
    Msgbox, % ThisTitle
}
Thanks Smile_, it works :thumbup: , but it gives me more information than it should. It gives me the name of three files, when I only have 2 instances. I have tried with a video player and the same thing happens. Where does it get the rest of the names? Does it call the recent files?

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Find out files opened by an application

Post by Smile_ » 04 Dec 2022, 13:52

I can't say for sure, but it may happen to have multiple processes have the same name (but not same PID) with different origins.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 04 Dec 2022, 14:03

Smile_ wrote:
04 Dec 2022, 13:52
I can't say for sure, but it may happen to have multiple processes have the same name (but not same PID) with different origins.
I think I understand where the problem is, it seems to "remember" that recently there were three instances... is that possible? I had 4 video players open, I closed two and when I ran the script it listed 4. Two of them without names, since they no longer existed. I have added Hwnds="" to delete the variable, but it doesn't make sense either, since the script exits when giving the message.

Code: Select all

Hwnds=""
WinGet, Hwnds, List, ahk_exe PotPlayerMini64.exe
MsgBox, %Hwnds% ;Added line to find out how many processes the script "believes" are open: it says 4, when there are only two.
Loop, % Hwnds {
    WinGetTitle, ThisTitle, % "ahk_id " Hwnds%A_Index%
    Msgbox, % ThisTitle
}

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Find out files opened by an application

Post by Smile_ » 04 Dec 2022, 14:09

Can you check your process list from the taskmanager?, maybe there is background processes.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 04 Dec 2022, 14:35

Hi

It does not give errors when extracting the ahk_class. This is the final script to generate a small list of the files open in the various open instances of PotPlayer. How to remove " - PotPlayer" from the text. I know how to do it by cutting the last 12 characters (because that text stays at the end of the string), but I imagine that there will be a shorter process.

Code: Select all

Iniciate := 0
loop, PlayerList_*.txt
Iniciate := % Floor(A_Index)

^F8:: ;Ctrl-F8
Iniciate++
WinGet, Hwnds, List, ahk_class PotPlayer64
Loop, % Hwnds {
    WinGetTitle, ThisTitle, % "ahk_id " Hwnds%A_Index%
    PlayerList:= "PlayerList_" Iniciate ".txt"
    FileAppend, %ThisTitle% `n, %PlayerList%
}
Ah! Is there a way to extract the full path of the file? It's not important, but it would be nice if it were saved in the list.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application  Topic is solved

Post by wetware05 » 04 Dec 2022, 15:00

Hi.

This is how I have solved it, based on my knowledge:

Code: Select all

Iniciate := 0
loop, PlayerList_*.txt
Iniciate := % Floor(A_Index)

^F8::
Iniciate++
WinGet, Hwnds, List, ahk_class PotPlayer64
Loop, % Hwnds {
    WinGetTitle, ThisTitle, % "ahk_id " Hwnds%A_Index%
    LenText:= StrLen(ThisTitle)-12
    StringLeft, OutputVar, ThisTitle, %LenText%
    ThisTitle:= SubStr(ThisTitle, 1, %LenText%)
    PlayerList:= "PlayerList_" Iniciate ".txt"
    FileAppend, %OutputVar% `n, %PlayerList%
}
(This script has the disadvantage that minimized windows are not processed, so you have to open them).

Rectified. I had left out an extra line, which was a test.
Last edited by wetware05 on 08 Dec 2022, 11:26, edited 3 times in total.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 05 Dec 2022, 14:40

Hi.

I share the script that I have elaborated from the previous one, where a command line utility (es.exe), belonging to the "EveryThing" program (it is a very fast and efficient file searcher and indexer —free—, searches in all hard drives and external drives), looking for the location of all potplayer instances (it will surely be valid for other applications or players..., adapt it to each circumstance), and generates a list of the files and their location (long path) , to be able to open them later. (WinGetTitle only generates the title of the file, the script gives the title to the "everything" program and this provides the path of the file. Curiously, it will tell us if it is duplicated).

You have to download the program "everything", from https://www.voidtools.com/downloads/, and run it a first time so that it generates a database of all the files. The one I have is only 95Mb and it has 11 drives (internal and external) the database is small! Then you have to download "Command-line Interface", which is below, and unzip the .zip file in the same directory where the program was installed (I recommend downloading the portable version and unzip it in a non-system directory) .
(Change the script paths according to each personal configuration. Everything.exe has to be running, the script takes care of checking if the application is open).

Code: Select all

DetectHiddenWindows, On
Iniciate := 0
loop, PlayerList_*.m3u
Iniciate := % Floor(A_Index)

if not WinExist("Everything")
{
Run, d:\Utilidades\Everything\Everything.exe -startup
}

^!F8::
Iniciate++
WinGet, Hwnds, List, ahk_class PotPlayer64
Loop, % Hwnds {
    WinGetTitle, ThisTitle, % "ahk_id " Hwnds%A_Index%
    OutputVar:= StrReplace(ThisTitle, " - PotPlayer")
    RunEverything:= "d:\Utilidades\Everything\es.exe """ OutputVar """ > File_Temp.txt"
    FileAppend, %RunEverything%, Everything.bat
    RunWait, Everything.bat ,,hide
    PlayerList:= "PlayerList_" Iniciate ".m3u"
    FileRead, OutputEnd, File_Temp.txt
    FileAppend, %OutputEnd%`n, %PlayerList%
    FileDelete, Everything.bat
    FileDelete, File_Temp.txt
}

SoundPlay, *64
MsgBox, Ended process!
Return
Suggestions to improve the script are accepted.

-------------------------------------------
Modified on 06/12. It didn't work well if the file name contained spaces. Added quotes to the names. Fixed up.

Modified on 08/12. Some improvements. Minimized windows are registered, the process of trimming the long title to the short title has been improved. Notifies you when the process has been completed.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 08 Dec 2022, 11:05

Hi :salute:

I have modified the previous script so that it only serves to check if the name of a file -copied to the clipboard- exists in the storage units and returns its location in a message and pasting it to the clipboard.

An example is if we must download a certain file or image, etc. from a web page, or we already have it on the computer.

I have modified the script above again, so that it works in the most optimal way, which consists in that "Everything" starts with the computer, as a service, but its interface does not start. In this way, its execution as administrator is avoided and its presence becomes less "annoying" every time its interface is opened. You have to configure the general options as shown in the graphic.

Image

(The "EFU file association" option is optional. An EFU file can be generated "Everything" on removable drives so that the program searches for files existing there, without the need for such drives to be connected. Go to the following address to learn how to generate EFU files: https://www.voidtools.com/support/everything/file_lists/ The EFU files have to be in the same directory as the program, or else you have to go to the "configuration", "Indexes", "File lists" and add them. Example, from command line: Everything.exe -create-file-list "Temporal_L.efu" "l:\Temporal\").

Code: Select all

DetectHiddenWindows, Off

if not WinExist("Everything")
{
Run, d:\Utilidades\Everything\Everything.exe -startup
}

If !Clipboard
{
MsgBox, The clipboard is empty.
Return
}

RunEverything:= "d:\Utilidades\Everything\es.exe """ Clipboard """ > File_Temp.txt"
FileAppend, %RunEverything%, Everything.bat
RunWait, Everything.bat ,,hide
FileRead, OutputEnd, File_Temp.txt
FileDelete, Everything.bat
FileDelete, File_Temp.txt

SoundPlay, *64
If !OutputEnd
{
MsgBox, The file does not exist on the computer.
}
Else
{
MsgBox, % OutputEnd
clipboard:= % OutputEnd
}

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 09 Dec 2022, 17:52

Hi.

I get bored... :cry: I start thinking about the same theme, with variations. It's the same script as above, but if the file is found, the output message gives the option to go to the directory where the file is located (in explorer, I imagine the script can be adapted so that the directory is open in Total Commander or other browsers), otherwise copy the path to the clipboard.

(It gives an error if multiple files are found. I'll think about how to fix it if that happens.)

Code: Select all

DetectHiddenWindows, Off

if not WinExist("Everything")
{
Run, d:\Utilidades\Everything\Everything.exe -startup
}

If !Clipboard
{
MsgBox, The clipboard is empty.
Return
}

RunEverything:= "d:\Utilidades\Everything\es.exe """ Clipboard """ > File_Temp.txt"
FileAppend, %RunEverything%, Everything.bat
RunWait, Everything.bat ,,hide
FileRead, OutputEnd, File_Temp.txt
FileDelete, Everything.bat
FileDelete, File_Temp.txt

SoundPlay, *64
If !OutputEnd
{
MsgBox, The file does not exist on the computer.
Return
}
Else
{
MsgBox, 0x1024,, The file exists in %OutputEnd% `n(Otherwise the long path will be pasted to the clipboard) `n`nGo to directory?
	IfMsgBox Yes
	   {
	   StringGetPos, pos, OutputEnd, \, r1
	   length := pos+1
	   StringLeft, OutText, OutputEnd, %length%
	   Run, explorer %OutText%
	   return
	   }
}
clipboard:= % OutputEnd

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 13 Dec 2022, 10:29

The structure of my scripts are a bit baroque, sorry.

From the previous script I have modified two things. 1. Holding down the control key clears the clipboard, forcing... 2. to bring up a small GUI where you can enter the file to search for.

I have made this utility just for entertainment and for my own use. It is useful when it is a very specific name, like the name of a movie or song (image...), but beyond that, it has its limits.

There are several programs and utilities that make use of the "Everything" program https://www.voidtools.com/forum/viewtopic.php?f=10&t=6326, it seems strange to me that nothing has been developed in AutoHotkey, or maybe I haven't found it. In this direction there are several applications and utilities that make use of "Everything" https://www.voidtools.com. To highlight "EverythingToolbar" that replaces the Windows search engine and makes better searches (it makes use of the "Everything" database). "PowerToys Run" no longer makes use of "Everything", but rather the system's own search index, which is why it is not very complete. "Wox" is not a bad option either, but I think it has stopped developing it or at most updates the plugins.

For me the best and most elegant is Listary https://www.listary.com/download, but it is paid if you want the Pro version.

Code: Select all

GetKeyState, state, Ctrl
if (state = "D")
{
 ClipBoard:=""
}

DetectHiddenWindows, Off

if not WinExist("Everything")
{
Run, d:\Utilidades\Everything\Everything.exe -startup
}

If !Clipboard
{
TempClip:=Clipboard
xposUtil := Floor((A_ScreenWidth/2)-450)
yposUtil := Floor((A_ScreenHeight/2)-200)
Gui, Search:+HwndGuiID
Gui, Search: Font, S9 Bold, Verdana
Gui, Search: Add, Edit, x10 y16 w230 h20 vMySearch, Búsqueda
Gui, Search: Add, Button, x250 y16 w65 h20 GSearch, Buscar
Gui, Search: Show, x%xposUtil% y%yposUtil% h56 w330, GUI
Return
}
Else {
TempClip:=Clipboard
Gosub, Rutina
}

Rutina:
RunEverything:= "d:\Utilidades\Everything\es.exe """ TempClip """ > h:\File_Temp.txt"
FileAppend, %RunEverything%, h:\Everything.bat
RunWait, h:\Everything.bat ,,hide
FileRead, OutputEnd, h:\File_Temp.txt
FileDelete, h:\Everything.bat
FileDelete, h:\File_Temp.txt

SoundPlay, *64
If !OutputEnd
{
MsgBox, The file does not exist on the computer.
Clipboard:=""
ExitApp
}
Else
{
MsgBox, 0x1024,, The file exists in %OutputEnd% `n(Otherwise the long path will be pasted to the clipboard) `n`nGo to directory?
	IfMsgBox Yes
	   {
	   StringGetPos, pos, OutputEnd, \, r1
	   length := pos+1
	   StringLeft, OutText, OutputEnd, %length%
	   Run, explorer %OutText%
	   ExitApp
	   }
clipboard:= % OutputEnd
ExitApp
}

#If WinActive("ahk_id " GuiID)
Enter::
NumPadEnter::
Search:
Gui Search: Submit
TempClip:=MySearch
Gui Search: Destroy
Gosub, Rutina
Return
They don't have a "trigger" keyboard shortcut, I have it inside the FastKeys scripts.
--------------------
Edited: Added a routine to accept the Enter key to validate and close the dialog, thanks to the help of boiler.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 13 Dec 2022, 17:38

hi.

The above scripts have the bug that if you search for "sample_sample.jpg" or "sample-sample2.jpg", you won't find the file "sample sample.jpg. I didn't notice this until today. In the "everything" interface, wildcards can be used, but not on the command line. The following script fixes this "glitch" by doing a search and if no match comes up, it tries again by changing the scripts to spaces. It's also not perfect, because it can If the name of the file is "samble sample-sample.jpg" and you won't find it anymore, you'll have to look for the possible variations, it's out of my knowledge to program something like that.

I'm going to suggest to the developer that wildcards can be used on the command line, it's the shortest way.

Code: Select all

GetKeyState, state, Ctrl
if (state = "D")
{
 ClipBoard:=""
}

DetectHiddenWindows, Off
Event=0

if not WinExist("Everything")
{
Run, d:\Utilidades\Everything\Everything.exe -startup
}

If !Clipboard
{
TempClip:=Clipboard
xposUtil := Floor((A_ScreenWidth/2)-450)
yposUtil := Floor((A_ScreenHeight/2)-200)
Gui, Search:+HwndGuiID
Gui, Search: Font, S9 Bold, Verdana
Gui, Search: Add, Edit, x10 y16 w230 h20 vMySearch, Búsqueda
Gui, Search: Add, Button, x250 y16 w65 h20 GSearch, Buscar
Gui, Search: Show, x%xposUtil% y%yposUtil% h56 w330, GUI
Return
}
Else {
TempClip:=Clipboard
Gosub, Rutina
}

Rutina:
Event++
If Event=2
 {
 TempClip:= RegexReplace(TempClip, "[\-\_]", "*")
 }
RunEverything:= "d:\Utilidades\Everything\es.exe """ TempClip """ > h:\File_Temp.txt"
FileAppend, %RunEverything%, h:\Everything.bat
RunWait, h:\Everything.bat ,,hide
FileRead, OutputEnd, h:\File_Temp.txt
If !OutputEnd and Event=1
 {
 FileDelete, h:\Everything.bat
 FileDelete, h:\File_Temp.txt
 Gosub, Rutina
 }
 else
 {
 FileDelete, h:\Everything.bat
 FileDelete, h:\File_Temp.txt
 }

SoundPlay, *64
If !OutputEnd
{
MsgBox, The file does not exist on the computer.
ExitApp
}
Else
{
MsgBox, 0x1024,, The file exists in %OutputEnd% `n(Otherwise the long path will be pasted to the clipboard) `n`nGo to directory?
	IfMsgBox Yes
	   {
	   StringGetPos, pos, OutputEnd, \, r1
	   length := pos+1
	   StringLeft, OutText, OutputEnd, %length%
	   Run, explorer %OutText%
	   ExitApp
	   }
Else
{
clipboard:= % OutputEnd
ExitApp
}
}

#If WinActive("ahk_id " GuiID)
Enter::
NumPadEnter::
Search:
Gui Search: Submit
TempClip:=MySearch
Gui Search: Destroy
Gosub, Rutina
Return
-----------------
Edited: Eh! The command line application does accept wildcards. What happens is that it is different than in the "Everything" interface. I've changed the script so that it replaces any type of dash with a wildcard. Now it does better searches. (If anyone thinks they need to add more types of signs, like periods or commas, please add them on line 36 TempClip:= RegexReplace(TempClip, "[\-\_]", "*")

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 15 Dec 2022, 16:25

Hi. :salute:

I think it is the definitive version of this simple script. I had to completely rebuild it, because the previous version was quite "clunky" with so much "Else" and jumping back and forth (it gave crashes when adding the new GUI).

The same initial process is maintained. If the "control" key is held down, the clipboard will be cleared and a dialog box will appear to do a custom search. Now the default search removes hyphens and replaces them with wildcards, to make the search more effective.

If any result is found, a GUI will appear where you can choose between several options. Now open as many instances of explorer as files found. It can also be executed, or the dual option of opening in the explorer and opening them (ideal if they are images, videos or audios). The property to paste to the clipboard at the end is optional. The file with the routes stays on the hard disk, in case you prefer to see it. It will be cleared with the next search.

Image

The interface of the "Everything" program is better to do a good search, this utility is a faster process to verify if you already have a certain file on the hard drive, through a keyboard shortcut (not incorporated, because I do the called from the program "FastKeys").

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory
;EnvSet, SD, %A_ScriptDir%

GetKeyState, state, Ctrl
if (state = "D")
{
 ClipBoard:=""
}

;Send ^c

DetectHiddenWindows, Off
Event=0

if not WinExist("Everything")
{
Run, d:\Utilidades\Everything\Everything.exe -startup
}

If !Clipboard
 {
 Gosub, SearchGUI
 }
Else
 {
 TempClip:= Clipboard
 Gosub, Rutine
 }
Return

SearchGUI:
TempClip:=Clipboard
xposUtil := Floor((A_ScreenWidth/2)-450)
yposUtil := Floor((A_ScreenHeight/2)-200)
Gui, Search:+HwndGuiID
Gui, Search: Font, S9 Bold, Verdana
Gui, Search: Add, Edit, x10 y16 w230 h20 vMySearch, Enter your Search
Gui, Search: Add, Button, x250 y16 w65 h20 GSearch, Search
Gui, Search: Show, x%xposUtil% y%yposUtil% h56 w330, GUI
Return

#If WinActive("ahk_id " GuiID)
Enter::
NumPadEnter::
Search:
Gui Search: Submit
TempClip:=MySearch
Gui Search: Destroy
Gosub, Rutine
Return

Rutine:
FileDelete, h:\File_Temp.txt
TempClip:= RegexReplace(TempClip, "[\-\_]", "*")
RunEverything:= "d:\Utilidades\Everything\es.exe """ TempClip """ > h:\File_Temp.txt"
FileAppend, %RunEverything%, h:\Everything.bat
RunWait, h:\Everything.bat ,,hide
FileRead, OutputEnd, h:\File_Temp.txt
FileDelete, h:\Everything.bat
;FileDelete, h:\File_Temp.txt

If !OutputEnd
 {
 Gosub, NotFound
 }
Else
 {
 Gosub, GuiControl
 }
Return
 
GuiControl:
Loop, Read,  h:\File_Temp.txt
NumLines := A_Index
SoundPlay, *64
Gui, Control: Add, GroupBox, x22 y60 w340 h140, Options
Gui, Control: Add, Radio, x32 y80 w300 h20 vDirectory Checked, Open the Container Directory
Gui, Control: Add, Radio, x32 y100 w300 h20 vOpenFile,  Open the file
Gui, Control: Add, Radio, x32 y120 w300 h20 vFileAndDir, Open the File and Open the Directory
Gui, Control: Add, Radio, x32 y140 w300 h20 vNothing, Do Nothing
Gui, Control: Add, CheckBox, x32 y170 w300 h20 vCopyClip, Copy to Clipboard
Gui, Control: Add, Button, x30 y210 w100 h30 gOptions, OK
Gui, Control: Add, Button, x252 y210 w100 h30 gCancel, Cancel
;Gui, Control: Font, S9 Bold, Verdana
Gui, Control: Add, Text, x22 y16 w340 h40 , %NumLines% file(s) exists in %OutputEnd%
Gui, Control: Show, x910 y416 h260 w384, GUI Control
Return

Options:
Gui, Control: Submit

If Directory
 {
 If (NumLines<>1)
 {
 Loop, %NumLines%
 {
 FileReadLine, VarOut, h:\File_Temp.txt, %A_Index%
 StringGetPos, pos, VarOut, \, r1
 length := pos+1
 StringLeft, OutText, VarOut, %length%
 Run, explorer %OutText%
 }
 }
Else
 {
 StringGetPos, pos, OutputEnd, \, r1
 length := pos+1
 StringLeft, OutText, OutputEnd, %length%
 Run, explorer %OutText%
 }
 If CopyClip=1
  {
  Clipboard:= OutputEnd
  }
 ExitApp
 }


If OpenFile
 {
 If (NumLines<>1)
 {
 Loop, %NumLines%
  {
  FileReadLine, VarOut, h:\File_Temp.txt, %A_Index%
  Run, Explorer %VarOut%
  }
 }
Else
 {
 Run, Explorer %OutputEnd%
 }
 If CopyClip=1
  {
  Clipboard:= OutputEnd
  }
 ExitApp
 }

  
If FileAndDir
 {
 If (NumLines<>1)
 {
 Loop, %NumLines%
  {
  FileReadLine, VarOut, h:\File_Temp.txt, %A_Index%
  Run, Explorer %VarOut%
  StringGetPos, pos, VarOut, \, r1
  length := pos+1
  StringLeft, OutText, VarOut, %length%
  Run, Explorer %OutText%
  }
  }
Else
 {
 Run, Explorer %OutputEnd%
 StringGetPos, pos, OutputEnd, \, r1
 length := pos+1
 StringLeft, OutText, OutputEnd, %length%
 Run, explorer %OutText%
 }
 If CopyClip=1
  {
  Clipboard:= OutputEnd
  }
 ExitApp
 }

If Nothing
 If CopyClip=1
  {
  Clipboard:= OutputEnd
  }
 ExitApp

NotFound:
SoundPlay, *64
MsgBox, The file does not exist on the computer.
ExitApp

Cancel:
ExitApp

User avatar
andymbody
Posts: 867
Joined: 02 Jul 2017, 23:47

Re: Find out files opened by an application

Post by andymbody » 16 Dec 2022, 00:00

I have not read all the details of your post, so this may not apply but... if you are not finding all the matches you expect, you may want to place the following command at top of your script...

SetTitleMatchMode, 2

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 16 Dec 2022, 04:23

andymbody wrote:
16 Dec 2022, 00:00
I have not read all the details of your post, so this may not apply but... if you are not finding all the matches you expect, you may want to place the following command at top of your script...

SetTitleMatchMode, 2
Thank you, andymbody.

They are searches using an application from the command line, which belongs to the developer of the "Eveything" program, an indexer and search engine widely used by the community and which is also free.

I went off topic. It's typical of me. And I know that it shouldn't be done, but I usually share the entire development, and so people who are new to AutoHotkey can see how a script gets more complicated and extended.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 17 Dec 2022, 13:14

Hi.

(Read the previous posts to be able to use this script)

I have added the ability to search for files through regular expressions (RegEx), as shown in the video. Since I don't know much about expressions I have used a simple example (through the interface of the program "Everything" I am learning to use them, since either no search comes out or the program puts in bold the part of the text that matches the expression). I have set by default, in the final GUI, that the script does not do anything, because it has already happened to me that when I set it to open the files found in the explorer, the system collapses as soon as there are many (I had to restart).

I have also added the ability to open the generated .txt file to give the option of saving it with another name, and because if there are many files, they are not seen in the small final GUI. Puts the number of files found and displays the first two lines. The file number already gives us a clue as to what we should do, whether to open it or not do anything, and open the text file or paste it to the clipboard.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory
;EnvSet, SD, %A_ScriptDir%

GetKeyState, state, Ctrl
if (state = "D")
{
 ClipBoard:=""
}

;Send ^c ;optional. If you just want to select the text and have the script copy it to the clipboard, remove the semicolon.

DetectHiddenWindows, Off
Event=0

if not WinExist("Everything")
{
Run, d:\Utilidades\Everything\Everything.exe -startup
}

If !Clipboard
 {
 Gosub, SearchGUI
 }
Else
 {
 TempClip:= Clipboard
 Gosub, Rutine
 }
Return

SearchGUI:
TempClip:=Clipboard
xposUtil := Floor((A_ScreenWidth/2)-450)
yposUtil := Floor((A_ScreenHeight/2)-200)
Gui, Search:+HwndGuiID
Gui, Search: Font, S9 Bold, Verdana
Gui, Search: Add, Edit, x10 y16 w230 h20 vMySearch, Enter your Search
Gui, Search: Add, Button, x250 y16 w65 h20 GSearch, Search
Gui, Search: Add, CheckBox, x10 y46 w300 h20 vRegEx, Use RegEx Search
Gui, Search: Show, x%xposUtil% y%yposUtil% h80 w330, GUI
;Gui, Search: Show, x%xposUtil% y%yposUtil% h56 w330, GUI
Return

#If WinActive("ahk_id " GuiID)
Enter::
NumPadEnter::
Search:
Gui Search: Submit
TempClip:=MySearch
Gui Search: Destroy
Gosub, Rutine
Return

Rutine:
FileDelete, h:\File_Temp.txt
If RegEx=1
 {
 RunEverything:= "d:\Utilidades\Everything\es.exe -r """ TempClip """ > h:\File_Temp.txt"
 }
Else
 {
TempClip:= RegexReplace(TempClip, "[\-\_\s+]", "*")
 RunEverything:= "d:\Utilidades\Everything\es.exe """ TempClip """ > h:\File_Temp.txt"
 }
FileAppend, %RunEverything%, h:\Everything.bat
RunWait, h:\Everything.bat ,,hide
FileRead, OutputEnd, h:\File_Temp.txt
FileDelete, h:\Everything.bat
;FileDelete, h:\File_Temp.txt

If !OutputEnd
 {
 Gosub, NotFound
 }
Else
 {
 Gosub, GuiControl
 }
Return
 
GuiControl:
Loop, Read,  h:\File_Temp.txt
NumLines := A_Index
SoundPlay, *64
Gui, Control: Add, GroupBox, x22 y60 w340 h140, Options
Gui, Control: Add, Radio, x32 y80 w300 h20 vDirectory, Open the Container Directory
Gui, Control: Add, Radio, x32 y100 w300 h20 vOpenFile,  Open the file
Gui, Control: Add, Radio, x32 y120 w300 h20 vFileAndDir, Open the File and Open the Directory
Gui, Control: Add, Radio, x32 y140 w300 h20 vNothing Checked, Do Nothing
Gui, Control: Add, CheckBox, x32 y170 w100 h20 vCopyClip, Copy to Clipboard
Gui, Control: Add, CheckBox, x150 y170 w100 h20 vTxtOpen, Open file List
Gui, Control: Add, Button, x30 y210 w100 h30 gCancel, Cancel
Gui, Control: Add, Button, x252 y210 w100 h30 gOptions, OK
;Gui, Control: Font, S9 Bold, Verdana
Gui, Control: Add, Text, x22 y16 w340 h40 , %NumLines% file(s) exists in %OutputEnd%
Gui, Control: Show, x910 y416 h260 w384, GUI Control
Return

Options:
Gui, Control: Submit

If Directory
 {
 If (NumLines<>1)
 {
 Loop, %NumLines%
 {
 FileReadLine, VarOut, h:\File_Temp.txt, %A_Index%
 StringGetPos, pos, VarOut, \, r1
 length := pos+1
 StringLeft, OutText, VarOut, %length%
 Run, explorer %OutText%
 }
 }
Else
 {
 StringGetPos, pos, OutputEnd, \, r1
 length := pos+1
 StringLeft, OutText, OutputEnd, %length%
 Run, explorer %OutText%
 }
 If CopyClip=1
  {
  Clipboard:= OutputEnd
  }
 If TxtOpen=1
  {
  Run, h:\File_Temp.txt
  }
 ExitApp
 }


If OpenFile
 {
 If (NumLines<>1)
 {
 Loop, %NumLines%
  {
  FileReadLine, VarOut, h:\File_Temp.txt, %A_Index%
  Run, Explorer %VarOut%
  }
 }
Else
 {
 Run, Explorer %OutputEnd%
 }
 If CopyClip=1
  {
  Clipboard:= OutputEnd
  }
 If TxtOpen=1
  {
  Run, h:\File_Temp.txt
  }
 ExitApp
 }
  
If FileAndDir
 {
 If (NumLines<>1)
 {
 Loop, %NumLines%
  {
  FileReadLine, VarOut, h:\File_Temp.txt, %A_Index%
  Run, Explorer %VarOut%
  StringGetPos, pos, VarOut, \, r1
  length := pos+1
  StringLeft, OutText, VarOut, %length%
  Run, Explorer %OutText%
  }
  }
Else
 {
 Run, Explorer %OutputEnd%
 StringGetPos, pos, OutputEnd, \, r1
 length := pos+1
 StringLeft, OutText, OutputEnd, %length%
 Run, explorer %OutText%
 }
 If CopyClip=1
  {
  Clipboard:= OutputEnd
  }
 If TxtOpen=1
  {
  Run, h:\File_Temp.txt
  }
 ExitApp
 }

If Nothing
 If CopyClip=1
  {
  Clipboard:= OutputEnd
  }
 If TxtOpen=1
  {
  Run, h:\File_Temp.txt
  }
 ExitApp

NotFound:
SoundPlay, *64
MsgBox, The file does not exist on the computer.
ExitApp

Cancel:
ExitApp


(The large icon menus below are generated by the "TrueLauncherBar" program, which is now free...http://www.truelaunchbar.com/announces/true-launch-bar-v8.0.html good to know these things! I bought it years ago. I prefer this option to filling the desktop with icons, also, being applications on the taskbar are accessed without having to resort to "go to desktop"). I forgot to comment something. TrueLauncher does not work in Windows 11, because it removed one of the properties of the previous systems, (I will surely recover it in Windows 12 or some patch). To make it work you have to install StartAllBack or Actual Window Manager, which return that "lost" feature to the taskbar.
-------------------------
Reissued on 12/18. I didn't realize that if you search for, say, "example example.jpg" the file "example-example.jpg" might exist and it wouldn't find it. I've changed the RegexReplace line so that it changes spaces to wildcards. I have also interchanged the "Ok" and "Cancel" buttons, since "Ok" have to go to the right as a rule.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Find out files opened by an application

Post by wetware05 » 18 Dec 2022, 16:09

Hi.

(I changed two things from the previous script, as I left written at the end).

I have encountered the problem that some list of music or videos (.m3u) are no longer valid, because it has been the case that I have deleted these files or have moved them from the directory or from the HD drive (it can also happen that the name has been changed too much, this is already unpredictable). This script, again using the "EveryThing" program's files database engine, checks if the files still exist and creates a new list with the valid paths (New_PlayerList.m3u). It also creates another file of those files that no longer exist (Files_not_Found.txt).

(As in the previous scripts: adapt the file paths according to the requirements of each one).

Code: Select all

DetectHiddenWindows, Off

if not WinExist("Everything")
{
Run, d:\Utilidades\Everything\Everything.exe -startup
}

NameTemp:= "h:\PlayList\PlayerList_15.m3u" ;Change the file name on this line
Loop, Read,  %NameTemp%
NumLines:= A_Index

Loop, %NumLines%
 {
 FileDelete, h:\Temp_m3u.txt
 FileReadLine, TempTitle, %NameTemp%, A_Index
 RegExMatch(TempTitle, "^.*\\(.*)\.(.*)$",m)
 NameFileTemp:= % m1 "." m2
 NameFile:= RegexReplace(NameFileTemp, "[\-\_\s+]", "*")
 RunEverything:= "d:\Utilidades\Everything\es.exe """ NameFile """ > h:\Temp_m3u.txt"
 FileAppend, %RunEverything%, h:\Everything.bat
 RunWait, h:\Everything.bat ,,hide
 FileRead, OutputEnd, h:\Temp_m3u.txt
 If !OutputEnd
  {
  SoundPlay, *64
  MsgBox, File not found %NameFileTemp%
  FileAppend, %NameFileTemp%`n, h:\Files_not_Found.txt, CP65001
  FileDelete, h:\Everything.bat
  }
 Else
  {
  FileAppend, %OutputEnd%, h:\New_PlayerList.m3u, CP65001
  FileDelete, h:\Everything.bat
  }
 }

SoundPlay, *64
FileDelete, h:\Temp_m3u.txt
MsgBox, Process Completed!!!
ExitApp
----------------

Reedited the script, I forgot the part where it checks if "Everything.exe" is running.

Post Reply

Return to “Ask for Help (v1)”