AHK to auto run a Web Source Script from outside Mp3tag



 



Here's something I wrote to automatically run a script outside of Mp3tag, in my case foobar2000.

Any CLI will work so long as AHK is supported.

It'll ask the user to close once the saved file operation is finished. So all you'd have to do is go through the List of search results window and then ensure the adjust tag info is ok. Then a yes, no dialog where all you need to do is press enter to close Mp3tag.

So 3 baby steps :baby:

You need to change some stuff for this to suit your needs so read the header of the file carefully.

Just the CLI command needs changing in the latest release



If you've any questions just ask. Or if you have any improvements too, then please let me know.

Icon for anyone who may need it tag-with-web-source-scripts-in-mp3tag

Script in action

Code to save to the .ahk file

; #---------------------------------------------------------------------------------------------------------#
; # Tag with Web Source Scripts in Mp3tag.ahk written by stevehero
; #---------------------------------------------------------------------------------------------------------#
; # Version 0.1 beta -  2021.02.16 - First release.
; # Version 0.1a beta - 2021.02.17 - Improvements and bug fixes.
; # Version 0.1b beta - 2021.02.18 - Improvements.
; # Version 0.2a beta - 2021.02.23 - Rewrote entire thing to use functions and timers for the various window
; #                                  changes. This helps increase the speed of the script as there's no hard
; #                                  coded Wait commands in there. Also leaves it easier to edit.
; #                                - CTRL+ESC key will close Mp3tag at any time throughout.
; #                                - Splash screen and MsgBox are centered to the Mp3tag window.
; #---------------------------------------------------------------------------------------------------------#
; # You can buy me a beer or an EP on beatport
; # https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=EZVJN8ZLEU8KC&source=url
; #---------------------------------------------------------------------------------------------------------#
; #
; #---------------------------------------------------------------------------------------------------------#
; # WHAT IT DOES WITH FOOBAR2000 OR ANY CLI:
; #---------------------------------------------------------------------------------------------------------#
; # when you use this it will fire the appropriate keyboard shortcut to use the
; # correct script.
; # As an example in Mp3tag to search by album artist and release the keyboard
; # shortcut is: Alt, S, S, R. so this will run that depending on the arguments
; # you give it.
; # See the section below 'SYNTAX'.
; #---------------------------------------------------------------------------------------------------------#
; # HOW TO SET-UP SETTING WITH FOO_RUN:
; #---------------------------------------------------------------------------------------------------------#
; # 1. First, download and install the plugin and AHK.
; #    https://www.foobar2000.org/components/view/foo_run
; #    https://www.autohotkey.com/
; # 2. Save all of this code to a file: Tag with Web Source Scripts in Mp3tag.ahk
; # 2. Go to: File>Preferences>Tools>Run Services.
; # 3. Click on Add.
; # 4. Give it a label such as:
; #    Tag with Beatport - Album Artist - Album
; # 5. In the Path field use:
; #    See the section below 'SYNTAX'.
; #---------------------------------------------------------------------------------------------------------#
; # HOW TO USE:
; #---------------------------------------------------------------------------------------------------------#
; # 1. Select a single track or the full album you want to tag in foobar2000. The
; #    way this works is it loads the directory of the track selected. Typically
; #    an album has its own directory anyway.
; # 2. Run it via the right-click context menu or alternatively you can add it to
; #    a toolbar.
; # 3. This will then run the script depending on the keyboard shortcuts used.
; #---------------------------------------------------------------------------------------------------------#
; # NOTE:
; #---------------------------------------------------------------------------------------------------------#
; # If you modify this make sure there's a keyboard shortcut to the script in
; # Mp3tag first otherwise it won't work.
; #---------------------------------------------------------------------------------------------------------#
; # SYNTAX:
; #---------------------------------------------------------------------------------------------------------#
; # Wrap any paths with " (Quotes).
; # "<AutoHotkey Path.exe>" "<Path to AHK Script>" "<Path of folder to process in Mp3tag>" "<Short cut keys>"
; # Here's my working example:
; # "W:\Apps '('Portable')'\AutoHotkey\AutoHotkeyU64.exe" "W:\Apps '('Portable')'\Music Apps\foobar2000\.autohotkey-routines\Tag with Web Source Scripts in Mp3tag.ahk" "%_path%" "{s}{s}{r}"
; #---------------------------------------------------------------------------------------------------------#; #---------------------------------------------------------------------------------------------------------#
; # MP3TAG THREAD:
; #---------------------------------------------------------------------------------------------------------#
; # https://community.mp3tag.de/t/ahk-to-auto-run-a-web-source-script-from-outside-mp3tag/52002
; #---------------------------------------------------------------------------------------------------------#

#SingleInstance,Force
SetTitleMatchMode,RegEx
#Persistent
SetTimer, CenterMsgBox, 10
SetTimer, CenterSplashScreen, 10
SetTimer, Close, 1500
SetTimer, IsReady, 100
SetTimer, IsSaved, 500

arg = %1%
global keystopress
keystopress = %2%
global msgBoxTitle

Run, "W:\Apps (Portable)\Music Apps\Mp3tag\Mp3tag.exe" "/fp:%arg%",,xPID
Sleep 200
SplitPath, arg,, dir
WinGetPos, mainUIX, mainUIY, mainUIW, mainUIH, .*Mp3tag.*
SplashTextOn, StrLen(dir)*7.25, 25, Please Wait`, Loading Files from... , %dir%
; msgbox  % "mainUIX: " mainUIX " | mainUIY: " mainUIY " | mainUIW: " mainUIW " | mainUIH: " mainUIH

; Increase or decrease time-out here if needed.
WinWait,.*Mp3tag.*,, 15
if ErrorLevel
{

  MsgBox, 16, , WinWait Timeout waiting for Mp3tag window
  exit

}

else

{

  SplashTextOff

 ^Esc::WinKill ahk_exe Mp3tag.exe

 ; Removed the sleep as a test for these.
 RunKeyboardShortcuts() {
   ; Select all the files and send the Alt key to access the menu and run the web source script.
   ; Sleep 50
   Send ^a
   ; Sleep 50
   Send {ALT}
   ; Here are the keyboard shortcuts for the Mp3tag script. Change the CLI in your program to suit.
   ; Sleep 50
   Send %keystopress%
   ; Automatically accept the search box.
   ; Sleep 50
   Send {ENTER}
 }
 return

 AskToCloseMp3tag() {
  msgBoxOptions := 4
  msgBoxOptions += 32
  msgBoxOptions += 4096
  msgBoxTitle = Close Mp3tag?
  msgBoxText = Do you want to close Mp3tag? (Press YES or NO)`n`nIMPORTANT`:`nYou will not be able to undo previous changes made.
  MsgBox, % msgBoxOptions, %msgBoxTitle% , %msgBoxText%
  IfMsgBox, Yes
  {

    if WinExist(".*Mp3tag.*") {
      WinActivate
      WinKill ahk_exe Mp3tag.exe
    }

    Exit

  }

  IfMsgBox, No
  {
    Exit
  }

  Return

}

CenterSplashScreen:
IfWinExist, Please Wait`, Loading Files from...
{
  WinGetPos, , , msg_W, msg_H, Please Wait`, Loading Files from...
  SetTimer, CenterSplashScreen, Off
  WinMove, Please Wait`, Loading Files from..., , (mainUIW/2+mainUIX-(msg_W/2)), (mainUIH/2+mainUIY-(msg_H/2))
}
return

CenterMsgBox:
IfWinExist, Close Mp3tag?
{
  WinGetPos, , , msg_W, msg_H, Close Mp3tag?
  SetTimer, CenterMsgBox, Off
  WinMove, Close Mp3tag?, , (mainUIW/2+mainUIX-(msg_W/2)), (mainUIH/2+mainUIY-(msg_H/2))
}
return

IsReady:
WinWaitActive ahk_exe Mp3tag.exe
StatusBarGetText, RetrievedText, 1, ahk_exe Mp3tag.exe
If (RegExMatch(RetrievedText, ".*Ready.*")) {
  ; MsgBox, %RetrievedText% is found.
  SetTimer, IsReady, Off
  RunKeyboardShortcuts()
}
Return

; Here's where the script will check if the files have been saved and give the user the option to close the Mp3tag.
; The default button here is Yes, so you can just hit enter.
IsSaved:
WinWaitActive ahk_exe Mp3tag.exe
StatusBarGetText, RetrievedText, 1, ahk_exe Mp3tag.exe
If (RegExMatch(RetrievedText, ".*Saved tag.*")) {
  ; MsgBox, %RetrievedText% is found.
  SetTimer, IsSaved, Off
  AskToCloseMp3tag()
}
Return

; Close the ahk script when Mp3tag is closed.
Close:
IfWinNotExist ahk_exe Mp3tag.exe
ExitApp
; Exit
Return

} ; End Mp3tag else statement.

Doing some AHK myself, I would recommend ( in ahk doc as well) using sendinput instead of send, especially when multi-windowing.

It works as is. Here's an SO thread that says it should only be used if Send doesn't work.