Progress bar and (not) Sleep

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
jackman
Posts: 4
Joined: 13 May 2024, 16:10

Progress bar and (not) Sleep

Post by jackman » 13 May 2024, 16:46

Hi guys,

I would be most grateful if you could help. I have looked on this forum in case my query is covered, so I have tried, failed, so asking for help please.

I am using the latest version of AuoHotKey (v.2).

Two things which I am asking for help please.

What I have done so far is install Jellfin which makes my PC a server for the films, music and TV shows I have and lists the contents very beautifully. I am using an old Windows 10 PC purely for this, and I have managed to use an old Nintendo Wii controller along with a Wii > USB gadget so the Wii controller is in effect a remote controller (saved me buying a Windows remote controller for bluetooth).

(1)

I have set up the controller as below, after identifying what each button does. I need the Jellyfin server program to run in the background before running the link (it starts automatically as part of Windows 10 startup), I could only find "Sleep" to delay bootup of the Microsoft Edge browser as couldn't find a way to only run it AFTER the Jellyfin server app has loaded, so used "Sleep" as a delay to give Windows enough time to boot up Jellyfin, however it doesn't always work and would be far better if the "run media.lnk" program ran AFTER it identified Jellyfin was running and in my system tray in the background. Is there a better way to do this than using "Sleep" please?

Code: Select all

LButton::Esc
RButton::Enter
LWin::!F4
WheelUp::LButton
PGDN::Shutdown 8

Sleep 10000

Run "C:\Users\Entertainment\Desktop\Run media.lnk"
(2)

So I don't need to plug in a keyboard and mouse to drag new films over to the media PC, I am using the code below, which works fine however I cannot make a progress bar work and as the files I am copying over can be gigabytes long it can take a few minutes to complete the process. I have used the "MsgBox" just as a way to acknowledge that the program is running however I really would like a progress bar that shows the progress of the files being copied and then closes before running the Microsoft Edge browser to boot up the media program. I tried the guide but couldn't make it work: https://www.autohotkey.com/docs/v2/lib/GuiControls.htm#Progress

Code: Select all

MsgBox "Copying files, will start media viewer when done"

FileCopy "e:\films\*.mp4", "d:\films"
FileCopy "e:\John films\*.mp4", "d:\John films"
FileCopy "e:\music\*.mp4", "d:\music"
SourceDir := "e:\tv shows"
TargetDir  := "d:\tv shows"
DirCopy SourceDir, TargetDir, 1

Run "C:\Users\Entertainment\Desktop\Run media.lnk"
[Mod edit: Replaced b-tags around the code with [code][/code] tags. Please use them yourself when posting code!]
User avatar
mikeyww
Posts: 27195
Joined: 09 Sep 2014, 18:38

Re: Progress bar and (not) Sleep

Post by mikeyww » 13 May 2024, 17:00

Welcome to this AutoHotkey forum!
  1. Perhaps: ProcessExist
  2. You could use Loop Files to copy each file. You could then adjust the progress bar to reflect the percentage of files copied. If you use Loop Files twice, you can count the files in the first loop, and copy the files in the second loop.

Code: Select all

#Requires AutoHotkey v2.0
mask := '*.*'

F3::processFiles(A_ScriptDir '\' mask)

processFiles(dirMask) {
 Static g    := Gui(, 'Progress')
      , prog := g.AddProgress('w300')
 prog.Value := 0
 g.Show 'y100'
 n := 0
 Loop Files dirMask
  n++
 Loop Files dirMask {
  prog.Value := 100 * A_Index / n
  MsgBox A_LoopFilePath, 'File #' A_Index ' of ' n
 }
 g.Hide
}
jackman
Posts: 4
Joined: 13 May 2024, 16:10

Re: Progress bar and (not) Sleep

Post by jackman » 13 May 2024, 17:32

Thank you for the welcome, appreciated. I have not programmed since using QBasic and a little Assembler Code back in 1983 so I struggle with coding now.

Thank you for your suggestions however I don't understand how I can incorporate the code for what I need so it works, is there any chance I could please ask you to merge my code with yours so I have a working script I can copy and use? I am finding the coding past my abilities!

Many thanks in anticipation.
User avatar
mikeyww
Posts: 27195
Joined: 09 Sep 2014, 18:38

Re: Progress bar and (not) Sleep

Post by mikeyww » 13 May 2024, 18:28

Code: Select all

#Requires AutoHotkey v2.0
proc := 'Notepad.exe'
SoundBeep 1500
If 'Yes' != MsgBox('Copy files?', 'Confirm', 'YNC Default2 Icon?')
 Return
ProcessWait proc
copyFiles('E:\films\*.mp4', 'D:\films')
Run A_Desktop '\Run media.lnk'

copyFiles(source, dest) {
 Static g    := Gui(, 'Progress')
      , prog := g.AddProgress('w300')
 n := prog.Value := 0
 g.Show 'y100'
 Loop Files source
  n++
 Loop Files source {
  prog.Value := 100 * A_Index / n
  If !FileExist(dest '\' A_LoopFileName)
   FileCopy A_LoopFilePath, dest
  ; Sleep 300
 }
 Sleep 800
 g.Hide
}
jackman
Posts: 4
Joined: 13 May 2024, 16:10

Re: Progress bar and (not) Sleep

Post by jackman » 14 May 2024, 18:46

I spent a lot of time today with the code.

ProcessExist didn't seem to make any difference and I couldn't get the progress bar to work or Loop to work. It doesn't help when there is a fair bit of code above that I am not familiar with so I can't modify it to make it work.

Instead I made the files copy if when the PC booted it detected the presence of a USB drive inserted however it would be nice before it does that automatically to have MsgBox showing text and then closing itself a couple of seconds later without needing to press a button to "ok" it; is there a way?
User avatar
mikeyww
Posts: 27195
Joined: 09 Sep 2014, 18:38

Re: Progress bar and (not) Sleep

Post by mikeyww » 14 May 2024, 18:55

You can use the MsgBox timeout option or use a GUI.

My script used ProcessWait.
jackman
Posts: 4
Joined: 13 May 2024, 16:10

Re: Progress bar and (not) Sleep

Post by jackman » 14 May 2024, 19:28

Thanks, used this code for MsgBox, seems to work so i've added it to my script - will final test it tomorrow:

Code: Select all

result := MsgBox("The USB files will be copied, then Media player will boot", "Information", "T5")
Jellyfin however loads on Windows startup to start its services and goes onto the taskbar when loaded; using ProcessWait doesn't seem to work as it might be running before it finishes loading properly, so the script thinks it is running before it is? Any thoughts? I tried:

Code: Select all

proc := 'Jellyfin.Windows.Tray.exe'
ProcessWait proc
User avatar
mikeyww
Posts: 27195
Joined: 09 Sep 2014, 18:38

Re: Progress bar and (not) Sleep

Post by mikeyww » 14 May 2024, 20:10

Yes, the process might run before any icon appears, so you can add sleep if needed.
Post Reply

Return to “Ask for Help (v2)”