Search code examples
applescriptitunesspotifypreview

Preview songs in Spotify with Applescript - NeedleDrop from Doug


We all know Doug's applescripts for iTunes. However, with the growth of streaming music, I don't use iTunes so much anymore, but rather Spotify.

I was trying to adapt the NeedleDrop script to preview songs. The script allows to set

a - A start from given time, f.e. start playing the songs at 30 seconds b - Play that song for a given time, f.e. 10 seconds.

The problem is the script starts the first song at the given time and plays for the given time, but then, the next songs play for a given time, but start at the beginning instead of the given value in time.

The script is under GNU license by Doug and the license is included. I'm posting both the script and the exported app from it. If anyone has an idea ?!

LINK: Download

Here is the script provided in the download file:

-- handler to get a number from user
to get_a_number(pmpt, addenda, defnum)
    set rez to (display dialog addenda & pmpt default answer defnum buttons {"Cancel ", "OK"}          default button 2 with title "Needle Drop")
    if button returned of rez starts with "cancel" then tell me to quit
    set myNumber to text returned of rez
    try
        if myNumber is "" then get_a_number(pmpt, "Enter only numbers..." & return & return, defnum)
        return (myNumber as integer)
    on error -- m number n
        get_a_number(pmpt, "Enter only numbers..." & return & return, defnum)
    end try
end get_a_number

global start_time -- seconds into each track to begin playing
global needle_drop_interval -- seconds to play each track

on run
-- get number of seconds between songs
    set needle_drop_interval to my get_a_number("Play each track for how many seconds?", "", "10")
-- get seconds into each track to play
    set start_time to my get_a_number("How many seconds into each track to start playing?", "", "10")

-- play first song in the playlist
    tell application "Spotify"
        activate
        set player position to start_time
        play
        delay needle_drop_interval
    end tell
end run

on idle
    tell application "Spotify"
        if player state is not playing then tell me to quit
        pause
        next track
        set player position to start_time
        play
    end tell
return needle_drop_interval
end idle

on quit
    try
        tell application "Spotify" to stop
    end try
    continue quit
    error number -128
end quit

Solution

  • Amazing !

    However, after some songs, the bug still happened sometimes, which seems to get totally fixed by adding a delay before the set player position.

    This could sound contradictory, but worked better for me.

    Thanks !

    on idle
        tell application "Spotify"
            if player state is not playing then tell me to quit
            next track
            pause
            delay 1
            set player position to start_time
            play
        end tell
        return needle_drop_interval
    end idle