Automation Challenge

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Automation Challenge

Post by elad770 » 02 Nov 2020, 10:07

I would like to reach out with another challenge I'm experiencing.

I work with Midi that transmits a Sysex message (or any message for that matter)

Here's the function I'm trying to do. If anyone successfully helps me to solve this problem I'm donating 200$ to support this community and 100$ for the member who posts the script, code, or what have you

- I have windows - Regular folders with thousands of files on my computer: notepads, music files, pdf files....etc
I would like to have the following function:

I would like to assign ANY file I want to a specific midi message. I want to be able to right-click on that file (e.g. notepad) and have the option to "assign" or "capture" the midi command
When clicking on that a popup window will appear saying: "listening" or "ready to capture" and then I will be pressing my midi controller to transmit the message. The message and the file are now "friends forever" :)
A log file or any file will be created somewhere to remember that registration and from this moment on, every time I will transmit the message, the file will open. Every time I will open the file manually the midi controller will engage (to have them sync and talk to each other). - It sounds complicated but it's more simple than you think. Please help with ideas and thoughts even if you don't know how to do it. The discussion is important!

Thank you
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Automation Challenge

Post by mikeyww » 02 Nov 2020, 10:27

Just conceptual thoughts here:

1. Could select the file in Windows Explorer. Would need the script to add a context-menu entry (should be doable) that then activates a routine.

2. Script checks selected file: if in this MIDI database ("log"), then use the corresponding MIDI message; otherwise, add it.

3. SendMIDI might help as a way to get the messages sent. I don't use MIDI and so am not sure whether SendMIDI can also receive a message that can then be saved.

https://github.com/gbevin/SendMIDI

4. "Every time I will open the file" is probably the tricky part, because files open in so many different programs. Identifying all of them is probably not feasible, but if the file will be opened using Explorer itself, then the event could be detected there, triggering the MIDI message. Another approach could be to use a shell hook to determine when specific types of files are accessed (or last access date changes), and then trigger the MIDI. Nonetheless, accessing a file does not mean that the file was opened by the user, so I'd go with the former approach.
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Re: Automation Challenge

Post by elad770 » 02 Nov 2020, 16:09

Sorry for the confusion.

When I said windows,I was referring to a regular windows platform, not internet explorer.

Lets assume that the files are mp3 files. Bunch of songs

They only open in 1 , only one program. Windows media player.

That is all. Can you guide me through mikey?
I will pay for your time, not a problem.

I saw the video you sent and understood 50% out of it.
I have no doubt in my mind that you understood it in its entirety.

I would highly appreciate your help
User avatar
boiler
Posts: 16705
Joined: 21 Dec 2014, 02:44

Re: Automation Challenge

Post by boiler » 02 Nov 2020, 16:21

The Windows Explorer he's referring to is also known as the File Explorer. It's the standard window for viewing folders and the files they contain. Not Internet Explorer, the web browser.
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Re: Automation Challenge

Post by elad770 » 02 Nov 2020, 16:54

Haha :lol:

Sorry for my ignorance.
I have the vision in my mind but not the words (the script)
To bring it out.

I hope someone will help. Thank you Boiler for bridging.
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Automation Challenge

Post by mikeyww » 02 Nov 2020, 18:54

Thanks for clarifying that, @boiler.

Each step looks achievable. I don't use MIDI, but I'm sure that there are some other audiophiles out there.
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Re: Automation Challenge

Post by elad770 » 02 Nov 2020, 19:02

Dear MIkey,

I'm not into Midi either, LOL.

Believe it or not, I'm in the Public Health field, currently finishing my second Masters. As far as you can imagine, LOL

If we were to assume that instead of midi, we are using a Key, like Num1 instead of the Sysex, would you be able to develop such a thing?
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Automation Challenge

Post by mikeyww » 02 Nov 2020, 19:11

I think so. I'll give it a look. Public health: great field, the need is large. Fix COVID while I'm working on this.
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Re: Automation Challenge

Post by elad770 » 02 Nov 2020, 20:05

Haha, I truly appreciate your efforts and willingness to help.

Covid? Well.... my interest in public health is the nutritional aspect.

CVD and other food born, chronic diseases have more impact on people's lives than Covid19. Yearly mortality rates are not even close. Not that I undermine Covid19 but statistically it is not the biggest public health concern. (Even if the focus is on that)

I really look forward to see your magic. I've been waiting to find a solution for a long time. 🙏🙏. God bless you

Elad
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Automation Challenge

Post by malcev » 02 Nov 2020, 20:40

Every time I will open the file manually the midi controller will engage...
It can be done with ReadDirectoryChanges api with FILE_NOTIFY_CHANGE_LAST_ACCESS filter, but You need to enable Last Access Update in registry.
But it will decrease Your PC performance.
Another option is to parse Change Journals (NTFS only).
https://docs.microsoft.com/en-us/windows/win32/fileio/change-journals
Another option (best one, but not easiest) is to write minifilter driver.
https://www.codeproject.com/Articles/43586/File-System-Filter-Driver-Tutorial
All about this approaches advantages and disadvantages You can read here:
https://blog.trailofbits.com/2020/03/16/real-time-file-monitoring-on-windows-with-osquery/
As for midi messages You can look here to understand how it works.
https://autohotkey.com/board/topic/54920-midi-inputoutput-combined-with-system-exclusive/
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Re: Automation Challenge

Post by elad770 » 02 Nov 2020, 23:46

malcev,

Thank you so much for joining in. I looked at all the links you've sent and I understood only to a certain extent.

I understand the main concepts, don't get me wrong but I won't lie to you: I simply do not know how to put everything together.

That's why out of despair I found this amazing forum and offered to simply pay for help.

I feel so blessed that Mikey is willing to help and if you can assist in any way to help to compile this script, I would be thankful!

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

Re: Automation Challenge

Post by mikeyww » 03 Nov 2020, 00:04

Code: Select all

/* mwMIDI ----------------------------------------------
By @mikeyww on 02 November 2020 • Version 01
This AutoHotkey script uses an INI file to associate specific files (paths) with text strings
 that the user defines.
When Windows Explorer (File Explorer) is displayed, the user can use the following hotkeys.
F3: Assign the selected file to a string. Assigning to DEL will delete the assignment.
F4: Retrieve the assigned string, and open the file.
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=82789
--------------------------------------------------------
*/
ini = E:\data\temp2\mwMIDI.ini ; Path to your INI file

#IfWinActive ahk_class CabinetWClass ahk_exe explorer.exe ; Hotkeys for Windows Explorer
F3:: ; Map the file to a string
file := Explorer_GetSelection().1 ; Get the path of file selected in Windows Explorer
SplitPath, file, fn ; Get the file name
IniRead, midi, %ini%, maps, %file% ; Find the assigned string that matches this file
midi := midi ~= "ERROR|^$" ? "NONE" : midi, newMidi := "" ; See if an error occurred
InputBox, newMidi, %fn%, Current mapping: %midi%`n`nEnter new mapping.,, 300, 125 ; Ask user for string assignment
If !newMidi ; User entered nothing
 Return
Switch newMidi {
 Case "DEL":
  IniDelete, %ini%, maps, %file% ; Delete the assignment
  MsgBox, 64, Deleted, % "Mapped: " file "`n`n        To: [DELETED]"
 Default:
  IniWrite, %newMidi%, %ini%, maps, %file% ; Save the assignment
  If ErrorLevel
   MsgBox, 48, Error, An error occurred during the mapping.
  Else MsgBox, 64, Changed, % "Mapped: " file "`n`n        To: " newMidi
}
Return

F4:: ; Retrieve the assigned string, and open the file
file := Explorer_GetSelection().1 ; Get the path of file selected in Windows Explorer
ToolTip, % "`n  Listening  `n" A_Space, 20, 20
SetTimer, TipOff, 1800 ; Tooltip will be displayed for this duration
Run, %file%
IniRead, midi, %ini%, maps, %file% ; Find the assigned string that matches this file
If !(midi ~= "ERROR|^$") ; An assigned string was found
 MsgBox, 64, MIDI, %midi% ; Transmit your message here
Return
#IfWinActive

TipOff:
ToolTip
Return

Explorer_GetSelection() {
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 array := []
 WinGetClass, winClass, % "ahk_id " (hWnd := WinExist("A"))
 If !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
  Return
 shellWindows := ComObjCreate("Shell.Application").Windows
 If !(winClass ~= "Progman|WorkerW") {
  For window in shellWindows
   If (hWnd = window.HWND) && (shellFolderView := window.Document)
    Break
 } Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
 For item in shellFolderView.SelectedItems
  result .= (result = "" ? "" : "`n") . item.Path, array.Push(item.Path)
 If !result
  result := shellFolderView.Folder.Self.Path
 Return array
}
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Re: Automation Challenge

Post by elad770 » 03 Nov 2020, 00:26

WOW, WOW ,WOW

And I thought I'm going to sleep tonight! LOL

Any special instructions?

Do I just run it?

2 main questions:

What is INI file?

How do I set up the midi channel that we are ought to listen to?
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Automation Challenge

Post by mikeyww » 03 Nov 2020, 00:35

Yes, you can run it, open Windows (File) Explorer, select a file, and press F3. F4 to run the file.

I don't know about MIDI, so you would need to amend the code. The idea of this script is that you could use it to assign files to MIDI strings and then execute them when the file is opened with F4. The SendMIDI documentation probably contains some or all of the needed details.

There could be an issue if the MIDI string is a binary string. Nonetheless, it is possible to map files to other files (e.g., binary files), so instead of storing the MIDI string in the INI file itself, the INI file would contain a pointer to the MIDI file (e.g., by name). Simple enough.

An INI file is an "initialization" file that is a plain text configuration file used by many programs. It contains simple assignments of one thing to another thing. This is typically used to define and manage sets of variables that are reused by the program. The INI file is convenient because it separates the program from its configuration settings. This enables the user to modify a program's configuration without having to do a lot of other tricks with the programming. In the case of this script, the file path itself is the "variable name", and the MIDI string is the value. The script just manages those assignments.

Files that are moved will then have a different path, so the mapping will be "lost" (different) until redefined. There are ways around that, such as using a file hash sum instead of a file path. That would work but would also make the file difficult to understand when viewed (if there were a need to do that). It would also mean that the INI file cannot be searched for a file path (if there were a need to do that). The advantage would be that file assignments could be preserved regardless of the file's location or path, or even its name.
Last edited by mikeyww on 03 Nov 2020, 00:44, edited 1 time in total.
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Re: Automation Challenge

Post by elad770 » 03 Nov 2020, 00:40

E:\data\temp2\mwMIDI.ini ; Path to your INI file

What is the INI file and do I need to change the file path to somewhere?

Is there a place in the script where i place the midi channel and port?

If anybody else can comment on the midi part?
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Re: Automation Challenge

Post by elad770 » 03 Nov 2020, 00:44

Is this where I place the Midi channel and port information?

midi := midi ~= "ERROR|^$" ? "NONE" : midi, newMidi := "" ;
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Automation Challenge

Post by mikeyww » 03 Nov 2020, 00:46

See some answers in my modified post above.

Judging from the SendMIDI documentation, this looks fairly easy-- adding the MIDI strings-- but I don't have MIDI and so have no way to test it. Others here might have that capability.

The easiest approach is probably to create a separate routine or function for sending your MIDI string. You can then just call that routine or function from the F4 hotkey routine, where it says, "Transmit your message here".

You can put the INI file wherever you want. In a typical scenario, you would have the script and the INI file together in a dedicated directory (folder). You could then change the path line to ini = %A_ScriptDir%\mwMIDI.ini ; Path to your INI file.

An alternative approach to an INI file is just storing a separate file (instead of just one string in the INI file) that has the same file name but a different extension. That file might contain the entire MIDI string information, for example. The script could then just look for that file directly, and execute it. It's a simple and straightforward approach that would work as long as you don't have different files with the same name (in different directories). Even then, there are ways to distinguish such files.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Automation Challenge

Post by malcev » 03 Nov 2020, 06:11

elad770, why dont You want to use ready programs for Your tasks, like this one?
https://midikey2key.de/
Why do You need this "Every time I will open the file manually the midi controller will engage (to have them sync and talk to each other)"?
What do You mean by "talk to each other"?
If You need automate WMP only, may be it will be easier to use MediaChange event.
https://docs.microsoft.com/en-us/windows/win32/wmp/player-player-mediachange
You have to tell more about Your workflow and what do You want.
User avatar
mikeyww
Posts: 26437
Joined: 09 Sep 2014, 18:38

Re: Automation Challenge

Post by mikeyww » 03 Nov 2020, 06:40

Wonderful ideas and program reference from @malcev! Looks like it could suit the need exceptionally well! :bravo: :dance: :beard:
elad770
Posts: 82
Joined: 30 Oct 2020, 16:21

Re: Automation Challenge

Post by elad770 » 03 Nov 2020, 09:38

Very good point and I have used the programs you've mentioned.

The only problem is that midi2key requires me to:

- Enter the application (which is a 3rd party)
- create an individual log file for every file manually
- Assign the Sysex to that file

This takes about many clicks and roughly 2min for every file to set up

I need a FAST, automated process - Click the file, assign, and BOOM! it's done and then to forget about it.

What Mikey designed is most definitely the way to go for me but unfortunately, I was not able to make it work. I need the midi part to be explained and to actually assign the right channel and port
I don't know how to place it in the script. But in terms of function it looks really good
Post Reply

Return to “Ask for Help (v1)”