📜 ⬆️ ⬇️

Automation of music management in the office, any song on order via ICQ


I am an office employee. My workplace is an office in which besides me 6 more people are sitting. I have long been used to the fact that the background is always playing music, with the exception of those workflows in which concentration is needed. Listening to music in headphones is somehow uncomfortable, and the head starts to hurt soon. Therefore, I always listened to her in such a way that it was impossible for my “neighbors” not to hear her. They scolded me ... but they scolded me not because I was disturbing them with the music itself, but because I was listening to the wrong music. Then I had a desire to please everyone, so that everyone could, without rising from his seat, turn on any song, even the one that is not on the computer.

Scheme

At first glance, automation should include the following features: enabling any track from the online network, changing the track, adjusting the volume, being able to pause playback and, accordingly, remove the pause from the pause, and also be able to turn on the track located locally on the computer. I am not really familiar with programming as such, except for reading the AutoIT manual (I always wanted to be able to automate any processes in Windows systems). A scheme immediately appeared in my head: AutoIT + ICQ + Foobar + A plugin for Foobar that allows you to search and listen to music online from the social network VKontakte (its window is shown in the screenshot). The point is simple: my colleagues send messages to a certain asya, the AutoIT script “picks up” them and, in accordance with the message, performs manipulations with Foobar.

Implementation

Since during AutoIT, all script manipulations are visible on the screen: opening windows, selecting a track, and so on, I initially planned to use a virtual machine for it, but turning around, I saw the idle computer of the recently retired employee and decided that the “media center” would be he.
image
First of all, I registered a new ICQ account for the bot. Coming up with a nickname for him did not take long - "Remote". Then I installed QIP 8095 on the selected computer - I chose this client because the messages in it are stored as txt files. I set it up in the following way: I turned off saving the history of service messages (that someone from the contact list entered the network or went out, etc.), also turned off all sounds so that they would not distract from the work of colleagues. Then I installed Foobar with the necessary plug-in and configured it to work with hot keys. And at the end I brought the bot and myself into the contact list of all the employees sitting with me in the office.

After completing all the preparatory work began to write the script itself. The principle of its operation is simple: every 2 seconds it must check the folder in which QIP stores the history of correspondence, the presence of a txt file, if the file is located, it reads the message from it, deletes the history file itself and, in accordance with what was written in the message makes manipulations with Foobar'om.
')
What happened in the end

Script:

Func Terminate ( ) ; Script off function as it works in an infinite loop
Exit 0
Endfunc
HotKeySet ( "{ESC}" , "Terminate" ) ; Binds off function to ESC key

FileChangeDir ( "C: Program FilesQIPUsersXXXXXXXXXXHistory" ) ; Changes the working directory

while 1 ; Starts endless script loop
$ Search = FileFindFirstFile ( "* .txt" ) ; It searches for txt files in the working directory and returns the search value to the $ Search variable
if $ Search = - 1 then ; Checks the presence of files on the contents of the variable
Sleep ( 1000 ) ; If no files are found, wait a second and start the cycle again.
else ; If txt file is found, then ...
$ File = FileFindNextFile ( $ Search ) ; Enter file name into $ File
$ Log = FileRead ( $ File ) ; Filing file contents into $ log variable
$ Log = StringSplit ( $ Log , @LF ) ; Since in QIP one message consists of three lines (marking the message as incoming or outgoing, author’s name / date / time of sending the message and the message itself), it makes an array of variables in which each element is equal to one line
$ Mess = $ log [ 3 ] ; The message itself enters the $ Mess variable
FileDelete ( $ File ) ; Deletes history file
MsgBox ( "0" , "" , $ Mess , 1 ) ; For a second, it displays a message with a command that the script will now execute - this is implemented for debugging
WinActivate ( '[REGEXPCLASS :(? I) {97E27FAA-C0B3-4b8e-A693-ED7881E99FC1}]' ) ; It makes the Foobar window active, implemented by referring to the class, since the title of the player changes depending on the composition being played.
WinWaitActive ( '[REGEXPCLASS :(? I) {97E27FAA-C0B3-4b8e-A693-ED7881E99FC1]]' ) ; Waiting for Foobar window to become active.

$ Check = StringInStr ( $ Mess , "local" ) ; Checks the entry of the word "local" in the message text
if $ Check = 1 then ; If "local" is at the beginning of the message, then ...
$ Mess = StringTrimLeft ( $ Mess , 6 ) ; "Cuts off" the first 6 characters in the message (the word local followed by a space)
Send ( "^ f" ) ; Send CTRL + F keystroke
WinWaitActive ( '[REGEXPCLASS :(? I) {483DF8E3-09E3-40d2-BEB8-67284CE3559F}]]' ) ; We are waiting for the track search window to open.
Send ( $ mess ) ; Enters the request message content
Sleep ( 500 ) ; Waiting half a second
Send ( "{TAB}" ) ; Sends TAB keystroke
Sleep ( 500 ) ; Waiting for
Send ( "{TAB}" ) ; Sends keystroke TAB (makes an active element of the window in which the search result is displayed)
Sleep ( 500 ) ; Waiting for
Send ( "{DOWN}" ) ; Sends the DOWN keystroke (the first track found by the search is highlighted)
Sleep ( 500 ) ; Waiting for
Send ( "{ENTER}" ) ; Includes found track
Sleep ( 500 ) ; Waiting for
WinClose ( '[REGEXPCLASS :(? I) {483DF8E3-09E3-40d2-BEB8-67284CE3559F}]]' ) ; Close the search box
continueloop ; Returns to the beginning of the loopless while loop
endif ; If the word "local" is not at the beginning of the message, then the script continues reading from here ...

$ Stop = StringRegExp ( $ Mess , "stop" ) ; ... looking for the entry of the word "stop" in the message
$ VolUp = StringRegExp ( $ Mess , "louder" ) ; Looks for the entry of the word "louder" in the message
$ VolDown = StringRegExp ( $ Mess , "quieter" ) ; Looks for the entry of the word "quieter" in the message
$ VolMax = StringRegExp ( $ Mess , "loud" ) ; Looks for the entry of the word "loud" in the message
$ VolMin = StringRegExp ( $ Mess , "quiet" ) ; Looks for the word "quiet" in the message

if $ Stop = 1 then ; If the word "stop" is sent, then ..
Send ( "! X" ) ; ... send ALT + X, which pauses / continues playback
continueloop ; Returns to the beginning of the loopless while loop
endif

if $ VolUp = 1 then ; If the word "louder" is sent, then ...
Send ( "! {UP}" ) ; ...
Send ( "! {UP}" ) ; ...
Send ( "! {UP}" ) ; ...
Send ( "! {UP}" ) ; ...
Send ( "! {UP}" ) ; Press the ALT + UP key combination 5 times, which slightly increases the volume
continueloop ; Returns to the beginning of the loopless while loop
endif

if $ VolDown = 1 then ; If the word "louder" is sent, then ...
Send ( "! {DOWN}" ) ; ...
Send ( "! {DOWN}" ) ; ...
Send ( "! {DOWN}" ) ; ...
Send ( "! {DOWN}" ) ; ...
Send ( "! {DOWN}" ) ; Press the ALT + DOWN key combination 5 times, which slightly decreases the volume
continueloop ; Returns to the beginning of the loopless while loop
endif

if $ VolMax = 1 then ; If the word "loud" is sent, then ...
Send ( "! C" ) ; ... send the shortcut key ALT + C, which turns on the maximum volume
continueloop ; Returns to the beginning of the loopless while loop
endif

if $ VolMin = 1 then ; If the word "silent" is sent, then ...
Send ( "! Z" ) ; ... will send the key combination ALT + Z, which turns on the minimum volume (does not turn it off at all)
continueloop ; Returns to the beginning of the loopless while loop
endif

; If not one of the standing ups did not work, then ...

Send ( "^ q" ) ; Sends the CTRL + Q key combination, which is responsible for opening the Foobar plugin window, which searches for music on VKontakte
WinWaitActive ( "vk.com audio Search" ) ; Waiting until the plugin window is active.
ControlCommand ( "vk.com audio Search" , "" , "ComboBox1" , "SetCurrentSelection" , 6 ) ; From the drop-down list "What services to search for" selects vk.com (VKontakte)
Sleep ( 500 ) ; Waiting half a second
ControlFocus ( "vk.com audio Search" , "" , "Edit1" ) ; Makes the query input line active.
Sleep ( 500 ) ; Waiting for
Send ( $ mess ) ; Sends the contents of the message from QIP to the input line
Sleep ( 5000 ) ; Wait 5 seconds while the plugin is looking for a track

while 1 ; I do not know how else to implement what will happen next, except with the use of an infinite loop
$ i = ControlListView ( "vk.com audio Search" , "" , "SysListView321" , "GetItemCount" ) ; Pushes the number of tracks found into the variable $ i
if $ i = 0 then ; If no tracks are found (the plugin sometimes does not find results from the first time)
ControlClick ( "vk.com audio Search" , "" , "Button1" ) ; Clicks on the "Stop Search" button
Sleep ( 500 ) ; Waiting for
ControlClick ( "vk.com audio Search" , "" , "Button1" ) ; Click on the "Search" button
Sleep ( 5000 ) ; Waiting for
exitloop ; Exits from the inner infinite while loop (from the second time it always finds)
else
exitloop ; If you did not find it the first time, then, most likely, the name of the track was entered incorrectly and the script also leaves the internal while loop
endif
wend

ControlListView ( "vk.com audio Search" , "" , "SysListView321" , "Select" , 1 ) ; Selects the second track from the search result (the first one is often for some reason 00:00 and does not play)
ControlClick ( "vk.com audio Search" , "" , "Button3" ) ; Adds a second track to the playlist.
WinClose ( "vk.com audio Search" ) ; Closes the plugin window
WinWaitActive ( '[REGEXPCLASS :(? I) {97E27FAA-C0B3-4b8e-A693-ED7881E99FC1]]' ) ; Waiting for Foobar window to become active.
Sleep ( 500 ) ; Waiting half a second
Send ( "{END}" ) ; Sends
Sleep ( 500 ) ; Waiting for
Send ( "{ENTER}" ) ; Sends keystroke ENTER, which includes the track
endif
wend ; Returns to the beginning of the loopless loop


How it works

Before you start, you need to add a local music library to the Foobar playlist. If someone writes simply: “metallica fade to black”, then the script “finds” this song on VKontakte and includes it. If someone wrote: “local joe satriani crowd chant”, then the script finds the track in its playlist, and then includes it. Commands are also available: “quiet”, “loud”, “quieter”, “louder” and “stop”.

Conclusion

I did this for several hours and I know that the script is far from ideal, there are many flaws in it, for example: you should somehow reduce the code (you can probably replay the command capture in a more correct way) or if a person with the word “local” requests a track not in the playlist, then it should be searched online and so on. I'm not a programmer at all, so if it's not difficult, write about code construction errors, I will be grateful.

Source: https://habr.com/ru/post/145550/


All Articles