numri Posted May 13, 2012 Posted May 13, 2012 (edited) Dear AutoIt forum, I'm currently working on a minor dashboard to be used with the streaming application Xsplit broadcaster. The applications main purpose is fetching a title from my currently playing track in Spotify and dump it down into a txt file which xsplit can later read. This action should be repeated every 10 seconds. Getting this far was easy with the following code. ; Fetch Title of Spotify window and remove "Spotify - " from the title Local $title = StringTrimLeft(WinGetTitle("Spotify"), 10) ; Replace the weird "-" from spotify with a proper dash to separate Artist - Track Local $final = StringReplace($title, " – ", " - ", 0, 0) ; Open File in overwrite mode - Place the spotify.txt in the same directory as this script. Local $file = FileOpen("spotify.txt", 2) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Write current track to file FileWriteLine($file, "| " & $final & " |") ; Close file FileClose($file) Sleep(10000) However I want to add a GUI to the program, with an input window that you can dump custom data into another textfile which xsplit will read and display on screen, but how can I combine these 2? I only want the spotify track to be checked every 10 seconds for CPU usage reasons, but I can't very well have a 10 second sleep inside the GUI loop now can I? I was thinking after I write to the file I set a "timestamp" Then check against this timestamp next time loop is ran and see if 10 secs has passed, but i'm not sure how to execute that.. and how will it affect the cpu usage? Streaming is very CPU heavy so this application has to be as minimalistic as possible. Thank you in advance. PS. If this is a commonly asked question I truly am sorry! I tried alot of different search queries but couldn't really figure out how to phrase my question. *edit* perhaps this should have gone into the GUI Forum? Edited May 13, 2012 by numri
VixinG Posted May 14, 2012 Posted May 14, 2012 (edited) Welcome to the forums numri If the action should be repeated every 10 seconds, but you don't want to use the Sleep() function you can set this script as a function: Func Fetch() Local $title = StringTrimLeft(WinGetTitle("Spotify"), 10) Local $final = StringReplace($title, " – ", " - ", 0, 0) ; Open File in overwrite mode - Place the spotify.txt in the same directory as this script. Local $file = FileOpen("spotify.txt", 2) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf FileWriteLine($file, "| " & $final & " |") FileClose($file) EndFunc And register this function with: AdlibRegister('Fetch',10000) ;every 10 sec So it will "Fetch" every 10 sec in the 'background'. Sleep() is stopping the script, but this doesn't, so you can do other things in your GUI, while AdlibRegister will be running the Fetch() function every 10 sec for you. Edited May 14, 2012 by VixinG [indent=3][/indent]
numri Posted May 14, 2012 Author Posted May 14, 2012 Thank you Vixin, I'm a long time lurker first time poster I'll try this immediatly! I had a bit of a revelation shortly after making this thread and started looking at timers, but reading some threads it seemed like they had some issues. Where do i stick the AdlibRegister? inside the GUI Loop? Thanks a bunch!
VixinG Posted May 14, 2012 Posted May 14, 2012 Put it only once, for example one line under the gui creation. Don't loop it. If you want to stop this, create a button or something with AdlibUnregister function. Something like this: #include <GUIConstants.au3> #include <WindowsConstants.au3> $GUI = GUICreate("Your GUI Example",600,500,@DesktopWidth/2-300,@DesktopHeight/2-250) AdlibRegister("Fetch",10000) $stopbutton = GUICtrlCreateButton('Stop Fetching!',10,10,150,30) GUISetState() While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE Exit Case $msg = $stopbutton AdlibUnRegister("Fetch") ;(...) EndSelect WEnd Func Fetch() Local $title = StringTrimLeft(WinGetTitle("Spotify"), 10) Local $final = StringReplace($title, " – ", " - ", 0, 0) Open File in overwrite mode - Place the spotify.txt in the same directory as this script. Local $file = FileOpen("spotify.txt", 2) If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf FileWriteLine($file, "| " & $final & " |") FileClose($file) EndFunc [indent=3][/indent]
numri Posted May 14, 2012 Author Posted May 14, 2012 Makes perfect sense, works like a charm. Thanks alot, have a nice evening!
VixinG Posted May 14, 2012 Posted May 14, 2012 I'm glad I was able to help. Nice evening too mate [indent=3][/indent]
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now