Skip to content

Instantly share code, notes, and snippets.

@zfarbp
Last active March 27, 2024 10:34
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zfarbp/581ce7e50a1b5740b4d31f007cac87fb to your computer and use it in GitHub Desktop.
Save zfarbp/581ce7e50a1b5740b4d31f007cac87fb to your computer and use it in GitHub Desktop.
Trigger Spotify with osascript

Trigger Spotify with osascript

/Applications/Spotify.app/Contents/Resources/Spotify.sdef

# read-only
osascript -e 'tell application "Spotify" to player state'                  # stopped,playing,paused
osascript -e 'tell application "Spotify" to current track'                 # The current playing track.
osascript -e 'tell application "Spotify" to artwork url of current track'  # Image data in TIFF format.
osascript -e 'tell application "Spotify" to artist of current track'       # The artist of the track.
osascript -e 'tell application "Spotify" to album of current track'        # The album of the track.
osascript -e 'tell application "Spotify" to disc number of current track'  # The disc number of the track.
osascript -e 'tell application "Spotify" to duration of current track'     # The length of the track in seconds.
osascript -e 'tell application "Spotify" to played count of current track' # The number of times this track has been played.
osascript -e 'tell application "Spotify" to track number of current track' # The index of the track in its album.
osascript -e 'tell application "Spotify" to starred of current track'      # Is the track starred?
osascript -e 'tell application "Spotify" to popularity of current track'   # How popular is this track? 0-100
osascript -e 'tell application "Spotify" to id of current track'           # The ID of the item.
osascript -e 'tell application "Spotify" to name of current track'         # The name of the track.
osascript -e 'tell application "Spotify" to artwork of current track'      # The track s album cover.
osascript -e 'tell application "Spotify" to album artist of current track' # That album artist of the track.
osascript -e 'tell application "Spotify" to spotify url of current track'  # The URL of the track.
osascript -e 'tell application "Spotify" to player position'               # Position of current track.

# read/write
osascript -e 'tell application "Spotify" to player position'   # The player s position within the currently playing track in seconds.
osascript -e 'tell application "Spotify" to set player position to 20'
osascript -e 'tell application "Spotify" to repeating enabled' # Is repeating enabled in the current playback context?
osascript -e 'tell application "Spotify" to set repeating enabled to true'
osascript -e 'tell application "Spotify" to repeating'         # Is repeating on or off?
osascript -e 'tell application "Spotify" to set repeating to true'
osascript -e 'tell application "Spotify" to shuffling enabled' # Is shuffling enabled in the current playback context?
osascript -e 'tell application "Spotify" to shuffling'         # Is shuffling on or off?
osascript -e 'tell application "Spotify" to sound volume'      # The sound output volume (0 = minimum, 100 = maximum)
osascript -e 'tell application "Spotify" to set sound volume to 50'

# commands
osascript -e 'tell application "Spotify" to next track'
osascript -e 'tell application "Spotify" to previous track'
osascript -e 'tell application "Spotify" to playpause'
osascript -e 'tell application "Spotify" to pause'
osascript -e 'tell application "Spotify" to play'
osascript -e 'tell application "Spotify" to play track "spotify:track:7IjFVDzHNxAAWoMwl2XRm5"'
osascript -e 'tell application "Spotify" to play track "spotify:playlist:3pCz4zMeSm7yIU7fslKih1"'

# read-only
osascript -e 'tell application "Spotify" to name'      # The name of the application.
osascript -e 'tell application "Spotify" to version'   # The version of the application.


osascript -e 'tell application "Spotify" to quit'
@unsignd
Copy link

unsignd commented Dec 20, 2022

The way in this code to get image data in TIFF format is not working, and I found a solution.
Use the command below instead.
osascript -e 'tell application "Spotify" to artwork url of current track'

I hope you have a nice day 👍

@zfarbp
Copy link
Author

zfarbp commented Jan 28, 2023

The way in this code to get image data in TIFF format is not working, and I found a solution. Use the command below instead. osascript -e 'tell application "Spotify" to artwork url of current track'

I hope you have a nice day 👍

Thanks!

Best,
Fabi

@MikeOxlong83
Copy link

How do I convert the seconds in the track duration to a normal Hh:Mm:Ss format? :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment