My script is making it impossible to quit an app it works with

Discussion in 'Mac Programming' started by Mephistopelus, Oct 16, 2012.

  1. Mephistopelus macrumors newbie

    Joined:
    Oct 12, 2012
    #1
    A script made for muting Spotify ads is polling Spotify every 0.5 seconds. By doing it, it makes Spotify impossible to quit (it instantly relaunches).

    Could anybody please help me solve this issue?
     
  2. Mac_Max macrumors 6502

    Joined:
    Mar 8, 2004
    #2
    I have three solutions, two helpful, one not.

    The helpful:

    Try polling every few seconds instead of every half second. That way the application can still quit.

    Create a 'shutdown Spotify' script that quits the first script, then tells Spotify to quit.

    The unhelpful:
    Throw them $5/month for Spotify Unlimited and the ads go away.
     
  3. Mephistopelus thread starter macrumors newbie

    Joined:
    Oct 12, 2012
    #3
    Hi!

    1. This way, the ads won't be muted "instantly".

    2. Thanks for the idea! I still hope to find a way to fix the script in the first place, though.

    3. I would love to! But in my country, Spotify (and practically all of the other American and EU services) do not work. So they won't take my money.
     
  4. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4

    Attached Files:

  5. Mephistopelus thread starter macrumors newbie

    Joined:
    Oct 12, 2012
    #5
    Wow, thanks.

    Here it is:

    Code:
    repeat #Repeat forever
    	try #Try to do this thing, and on error... (look at "on error" at the bottom)
    		tell application "Spotify"
    			#Setting variables
    			set cur_vol to sound volume
    			set trackk to track number of current track
    			set namme to name of current track
    			set pop to popularity of current track
    			set dur to duration of current track
    			set pos to player position
    		end tell
    		
    		if pop is 0 and dur is less than 40 then #If popularity is 0 and duration of current track is less then 40
    			tell application "Spotify" #Do our thing
    				pause
    				set sound volume to 0
    				play
    			end tell
    			delay dur - pos + 1 #When an ad ends,
    			tell application "Spotify"
    				set sound volume to cur_vol #restore the volume to the level it was before
    			end tell
    		else
    			delay 0.5 #If nothing worked (it must be a music track, not an ad), try and run this script again in 0.5 seconds.
    		end if
    		
    	on error errorMessage #On any error 
    		delay 0.5 #Run this script again in 0.5 seconds
    	end try
    	
    end repeat
     
  6. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    Try this :

    Code:
    repeat
    	tell application "System Events"
    		set isRunning to (count of (every process whose bundle identifier is "com.spotify.client")) > 0
    	end tell
    	if isRunning then
    		-- do your stuff with Spotify
    	else
    		exit repeat
    	end if
    end repeat
     
  7. Mephistopelus thread starter macrumors newbie

    Joined:
    Oct 12, 2012
    #7
    That's it! Thank you so much!
     
  8. Mephistopelus, Oct 17, 2012
    Last edited: Oct 17, 2012

    Mephistopelus thread starter macrumors newbie

    Joined:
    Oct 12, 2012
    #8
    Hi!
    Do you by any chance know how to detect if Spotify is playing currently?
    It would be of great help to me!

    Update:
    I've found it!
    Sorry:)
     
  9. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #9
    Code:
    tell application "Spotify" to set playerState to player state

    Tip :Take a look at the Spotify dictionary in scripteditor to see
    how to use it with Applescript.
     
  10. Mephistopelus thread starter macrumors newbie

    Joined:
    Oct 12, 2012
    #10
    Wow, thank you a billion times!
    Didn't know about the dictionary :)
     

Share This Page