Reading specific part of text from a profile file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CubanB
Posts: 8
Joined: 20 Aug 2022, 07:27

Reading specific part of text from a profile file

Post by CubanB » 12 Oct 2022, 03:51

This sort of thing is very new to me, so I figured I'd ask for help and see if this could be possible.

It's not a text file, it's an OpenRGB profile, and I'm trying to capture the device name. When I read it as text using FileRead, it only shows the header "OPENRGB_PROFILE". I've tried reading it as raw or hex but so far it comes up with "0" or blank. I've never done anything like this before, so it's very tough.

When I open the file in notepad++, it's line 2 that has the data I am looking for. When I open the file in notepad.exe, there's a character on each side of the device name. But these characters aren't constant from profile to profile, and the idea is to extract the device name of different devices/profiles. The only thing that stays constant is that the first letter of the device is in the same place (when viewing the hex). So maybe reading the file with an offset, and then reading up until the point where it detects more than one space, and then trimming off the extra spaces could work?

This is a screenshot of the part that I am trying to extract.

Image

The goal is to have it show up in a MsgBox (for testing), and then write the variable to a settings ini file. So reading the text, storing it as a variable and then writing it use IniRead. The second part is easy but the first part seems super complicated.

I've included two sample profiles from two different devices, if anyone knows of a way to make this work. I figured it'd be worth a try..
Extract_text_from_profiles.zip
(2.46 KiB) Downloaded 23 times

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

Re: Reading specific part of text from a profile file  Topic is solved

Post by mikeyww » 12 Oct 2022, 05:29

Code: Select all

dir         = %A_ScriptDir%
filePath    = %dir%\Corsair_Profile.orp
; filePath    = %dir%\Logitech_Profile.orp
start       = 30
len         = 33
file       := FileOpen(filePath, "r"), file.Seek(start)
deviceName := file.Read(len)         , file.Close()
MsgBox, 64, Device name, %deviceName%
image221012-0632-001.png
Output
image221012-0632-001.png (9.19 KiB) Viewed 546 times

CubanB
Posts: 8
Joined: 20 Aug 2022, 07:27

Re: Reading specific part of text from a profile file

Post by CubanB » 12 Oct 2022, 22:36

Wow, Mikeyww that works great on both profiles! The code seems so simple but does exactly what I was hoping for. I never saw anything like this in my searches, I need to learn more about FileOpen because it seems so simple and effective. There are some compatible devices with OpenRGB that have really long device names, I don't have one to test, but I created the profile to simulate it and this code worked for that too. The value of "len" just needed increasing to 70, and it worked with no downsides. I was worried that it might include some other stuff for the shorter named devices, but it didn't. Is there a reason for why that didn't happen?

Do you know what this kind of profile file is classified as, in terms of encoding? Because there doesn't seem to be any ansi or utf-8 conversions required but I was having trouble reading it with other methods mentioned in the searches I was doing? How it reading the file in such a simple and effective way?

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

Re: Reading specific part of text from a profile file

Post by mikeyww » 13 Oct 2022, 06:39

I do not know the answers, but discovered the solution by trial and error. This might be a proprietary or unique binary file format. It's possible that the real field length is 70 or something like that, or that there are field delimiters and invisible characters, or both. A Web search for OpenRGB might lead to the answer or a description of the file format.

As far as I know, FileRead works with text, but file objects can work with any file.

garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Reading specific part of text from a profile file

Post by garry » 13 Oct 2022, 07:36

@mikeyww thank you for solution
tried this to read , it's not correct , but see ASCII characters

Code: Select all

F1:=a_scriptdir . "\Logitech_Profile.orp"
F2:=a_scriptdir . "\NewText.txt"
ifexist,%f2%
   filedelete,%f2%
FileRead,aa,%f1%
Loop, % VarSetCapacity(aa) 
   { 
   byte := NumGet(aa, A_Index - 1, "Char" )
   if (byte="13")          ;- new line
     Textx .= Chr(byte)
   if (byte="10")             ;- new line
     Textx .= Chr(byte)
   if (byte="1")     ;- maybe add 2 spaces
     {
     byte:="32"
     Textx .= Chr(byte)
	 Textx .= Chr(byte)
     }
   if (byte="9")     ;- TAB
     Textx .= Chr(byte)
   Textx .= 31 < byte && byte < 127 ? Chr(byte) : "" 
   } 
fileappend,%textx%,%f2%,utf-8
try
 run,%f2%
exitapp
esc::exitapp

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

Re: Reading specific part of text from a profile file

Post by mikeyww » 13 Oct 2022, 09:50

Thanks, garry. I guess I should adjust my statement to, "FileRead works is usually used to work with text". :)

CubanB
Posts: 8
Joined: 20 Aug 2022, 07:27

Re: Reading specific part of text from a profile file

Post by CubanB » 14 Oct 2022, 11:07

mikeyww wrote:
13 Oct 2022, 06:39
I do not know the answers, but discovered the solution by trial and error. This might be a proprietary or unique binary file format. It's possible that the real field length is 70 or something like that, or that there are field delimiters and invisible characters, or both. A Web search for OpenRGB might lead to the answer or a description of the file format.

As far as I know, FileRead works with text, but file objects can work with any file.
No problems. OpenRGB is open source, the code is on Gitlab but understanding it is a bit beyond me at this point, I'm still trying to learn AHK.

Your solution worked really well, thanks again.

Post Reply

Return to “Ask for Help (v1)”