Use AHK to play audio for set periods of time Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TheGene
Posts: 15
Joined: 09 Sep 2015, 10:16

Use AHK to play audio for set periods of time

Post by TheGene » 14 Nov 2022, 10:13

Hi folks...Hope someone can assist me - I use mpv as an audiobook player on my pc, it works perfectly as when I use "shift+q" it saves my play position and exits the app. So next time I start it, it resumes play where I stopped.
So, I thought, if I use autohotkey, I could preset times, like 10 mins, 20 mins 30 mins etc. but I cannot get autohotkey to (focus) on the mpv that's open so when it sends the "shift+q" it saves and closes the program.

I hope that explains what I need. Here's the code I have so far, it's all good except the timer part doesn't work, need some help please?

Code: Select all

#SingleInstance, Force


; GUI Layout
; ............
; .....................
GUI, +AlwaysOnTop

Gui, Font, cWhite

Gui, Add, Text,, Play Books with Timers

GUI, Color, Black

GUI, Add, Button, w200 h30 gOpen_current, Open current book
GUI, Add, Button, w200 h30 gTimer_10m, Timer set to read 10 minutes
GUI, Add, Button, w200 h30 gTimer_20m, Timer set to read 20 minutes
GUI, Add, Button, w200 h30 gTimer_30m, Timer set to read 30 minutes

GUI, Show,x800 y50 w500 h500, The Gui
return


; Labels
; ............
; .....................
GuiClose:
ExitApp
return

Open_current:
Run, cmd
Sleep 500
Send {Text} cd C:\Users\Public\Downloads\mkv\
Sleep 1000
Send {Enter}
Send {Text} mpv "C:\Users\Perrin Aybara\Music\Audiobooks\Simon Haynes - Hal Spacejock Omnibus One Hal Spacejock Books 1-3, Plus Visit\Hal Spacejock Omnibus One Hal Spacejock Books 1-3, Plus Visit.mp3"
Send {Enter}
return

; set to 6000 atm for testing, will use correct value once it works
Timer_10m:
Sleep 6000 
Send +q
Sleep 1000
Send {Enter}
return

^x::ExitApp

picklepissjar
Posts: 20
Joined: 22 Oct 2022, 10:03

Re: Use AHK to play audio for set periods of time

Post by picklepissjar » 14 Nov 2022, 17:18

If my understanding is correct you want to focus the command prompt and send shift + q?
You can use the WinActivate command to accomplish this.
Since the title of the command prompt changes, you'd use something like "ahk_exe cmd.exe" to refer to the cmd prompt (Wintitle).
That info btw being accessible from Window Spy. (Right click on the AHK notifcation tray icon)
Also you might want to look into using SetTimer for the 10,20,30 minute waits

TheGene
Posts: 15
Joined: 09 Sep 2015, 10:16

Re: Use AHK to play audio for set periods of time

Post by TheGene » 15 Nov 2022, 01:14

Thanks very much for your reply picklepissjar, Sorry NO, I want nothing with cmd at all?

I need AHK to interact with the open mpv window so that the "shift+q" is sent to the mpv window. This may be offscreen at the time as I carry on working on the PC while listening o a book.

And if at all possible if someone has a solution, please try and help me with the code (or an example), just saying use this or that command is hard, it took me a day to just get the current code working? I'm truly no coder, although I do try my best.

picklepissjar
Posts: 20
Joined: 22 Oct 2022, 10:03

Re: Use AHK to play audio for set periods of time

Post by picklepissjar » 15 Nov 2022, 07:53

Using the Window Spy you can get info on the mpv window.
You would then want to put some of that info into the WinActivate command (Scroll to the bottom of the docs for more examples).
An example, since mpv probably has a changing title (not sure I don't use mpv) would be:

Code: Select all

WinActivate, ahk_exe cmd.exe	; would focus cmd
Replace, ahk_exe cmd.exe
Then just put it before the send.
Along with a Sleep

Code: Select all

Timer_10m:
Sleep 6000 
				; <------- goes here
Send +q
Hope this helps :)

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

Re: Use AHK to play audio for set periods of time

Post by mikeyww » 15 Nov 2022, 07:54

Code: Select all

mpv = ahk_exe mpv.exe
#If WinExist(mpv)
F3::
WinActivate
Send +q
Return
#If

TheGene
Posts: 15
Joined: 09 Sep 2015, 10:16

Re: Use AHK to play audio for set periods of time

Post by TheGene » 16 Nov 2022, 08:40

Thanks everyone for suggestions - I did more reading and experimenting and so on according to your feedback and this is what I now have - it WORKS 100% and I'm very chuffed believe you me!

Code: Select all

#SingleInstance, Force


; GUI Layout
; ............
; .....................
GUI, +AlwaysOnTop

Gui, Font, cWhite

; Gui, Add, Text,, Audiobook reader with Timer

GUI, Color, Red

GUI, Add, Button, w200 h30 gOpen_current, Open current book
GUI, Add, Button, w200 h30 gTimer_10m, Timer set to read 10 minutes
GUI, Add, Button, w200 h30 gTimer_20m, Timer set to read 20 minutes
GUI, Add, Button, w200 h30 gTimer_30m, Timer set to read 30 minutes

GUI, Show,x800 y50 w220 h150, Audiobook Reader with Timer
return


; Labels
; ............
; .....................
GuiClose:
ExitApp
return

Open_current:
Run, cmd
Sleep 500
Send {Text} cd C:\Users\Public\Downloads\mkv\
WinGet, cmd_id, ID, A
Sleep 1000
Send {Enter}
Send {Text} mpv "C:\Users\Perrin Aybara\Music\Audiobooks\Sarah Prineas - The Magic Thief - 01 - The Magic Thief\The Magic Thief 01.mp3"
Send {Enter}

WinGet, active_id, ID, A
WinMinimize, ahk_id %cmd_id%
return

Timer_10m:
Sleep 600000
    WinActivate, ahk_id %active_id%
Send +q
Sleep 1000
Send {Enter}
WinClose, ahk_id %cmd_id%

return

Timer_20m:
Sleep 1200000
    WinActivate, ahk_id %active_id%
Send +q
Sleep 1000
Send {Enter}
WinClose, ahk_id %cmd_id%

return

Timer_30m:
Sleep 1800000
    WinActivate, ahk_id %active_id%
Send +q
Sleep 1000
Send {Enter}
WinClose, ahk_id %cmd_id%

return


^x::ExitApp

; .....................



Ideally what I'd like to do next is see if I can set it up to give me a choice between the books I have saved in the music/audiobooks folder, but that is just a wishlist for now. It's working like the above, if you can look and see maybe you can improve on my code, I'd be VERY grateful, always nice to see what the experts do as opposed to us novices ....

Thanks again

picklepissjar
Posts: 20
Joined: 22 Oct 2022, 10:03

Re: Use AHK to play audio for set periods of time  Topic is solved

Post by picklepissjar » 16 Nov 2022, 11:38

Code: Select all

FileSelectFolder, OutputVar, , 3
if OutputVar =
{
	MsgBox, You didn't select a folder.
	Return
}
else
{
	files =
	Loop %OutputVar%\*.mp3
	{
		files = %files%`n%A_LoopFileName%
	}
	MsgBox % files
	Return
}
To get you started :D
Right now it just gets all the mp3's in a folder and lists them

LoopFile
Edit: Forgot to mention FileSelectFile which might be better now that I think of it :P
Last edited by picklepissjar on 17 Nov 2022, 08:46, edited 1 time in total.

TheGene
Posts: 15
Joined: 09 Sep 2015, 10:16

Re: Use AHK to play audio for set periods of time

Post by TheGene » 17 Nov 2022, 06:53

Great, thanks very much everyone for the help, I'll repost this at a later stage when I've got it down pat, in case someone else may want to use it.

Thanks again, stay safe....

Post Reply

Return to “Ask for Help (v1)”