How to auto-run a script from code

Discussion forum for G-Force users

Moderators: BTT, andy55, b.dwall, juxtiphi

Post Reply
User avatar
andy55
Site Admin
Posts: 553
Joined: Sat May 01, 2004 4:38 pm
Contact:

How to auto-run a script from code

Post by andy55 »

By request, this is example code that looks for a script with a specific name when a new track starts playing. To try it yourself, use a decent text editor and open the file <EngineDir>/Library/SSEngine.py. Search for "def OnTrackChange" and you'll find the top half of the code below. Put in the second part of the below snippet (below the #######'s) and restart G-Force, etc. Keep in mind that in Python, indentation spacing matters, so watch out for missing or stay spaces.

Note that this demo is useless when running in Standalone mode since the standalone vis never gets any info about when a track starts playing (and is only available in iTunes, JRMC, WMP, etc).

If you're new to SoundSpectrum visual scripting, check out G-Force/Scripts/Example Script.py and you can follow along as it runs using the patch below.

Code: Select all

    def OnTrackChange( self, inStartTime = -1 ):

        if inStartTime < 0:
            self.TrackChangeTime = self.mTimeIndex
        else:
            self.TrackChangeTime = inStartTime

        self.StopTrackOverlay()

        # Make new tracks cause a general visual changeup
        if TrackInfoIsAvailable():
            if self.mTimeIndex > self.TRACK_CHANGE_INITIAL_IGNORE:
                self.InsertVisEdit( self.mTrackStartVisEditParams )
            self.TrackChangePending = True
            self.AlbumCoverArtNeeded = True

            ###############################################################
            ###  Patch to run scripts with the name of songs that play  ###
            ###############################################################
            script_name = GetTrackText_Private( "#TITLE#-#ARTIST#.py" )
            script_pathname = "/Scripts/" + script_name
            if GetFileSize(script_pathname) > 0:
                print("Running: '%s'" % script_pathname)
                RunFile(script_pathname, self.CreateUserScript().namespace)
            else:
                print("Didn't find '%s' running other script" %  script_pathname)
                script_pathname = "/Scripts/Example Script.py"
                RunFile(script_pathname, self.CreateUserScript().namespace)


Post Reply