Skip to content

Instantly share code, notes, and snippets.

@patterns
Last active October 9, 2016 08:33
Show Gist options
  • Save patterns/c67c4664cbd5da5a2bb647cbca26e846 to your computer and use it in GitHub Desktop.
Save patterns/c67c4664cbd5da5a2bb647cbca26e846 to your computer and use it in GitHub Desktop.
Write Spotify track info to file
-- Original script is on Spotify Applescript API page
-- Creates a notification with information about the currently playing track
-- Main flow
set theFile to (((path to desktop folder) as string) & "trackinfo.txt")
if appIsRunning("Spotify") then
set currentlyPlayingTrack to getCurrentlyPlayingTrack()
----displayTrackName(currentlyPlayingTrack)
writeTextToFile(currentlyPlayingTrack, theFile, true)
else
tell application "Finder"
delete file theFile
end tell
end if
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
-- Method to get the currently playing track
on getCurrentlyPlayingTrack()
tell application "Spotify"
set currentArtist to artist of current track as string
set currentTrack to name of current track as string
return currentArtist & " - " & currentTrack
end tell
end getCurrentlyPlayingTrack
-- Method to create a notification
on displayTrackName(trackName)
display notification "Currently playing " & trackName
-- A delay is set added make sure the notification is shown long enough before the script ends
delay 1
end displayTrackName
on writeTextToFile(theText, theFile, overwriteExistingContent)
try
-- Convert the file to a string
set theFile to theFile as string
-- Open the file for writing
set theOpenedFile to open for access file theFile with write permission
-- Clear the file if content should be overwritten
if overwriteExistingContent is true then set eof of theOpenedFile to 0
-- Write the new content to the file
write theText to theOpenedFile starting at eof
-- Close the file
close access theOpenedFile
-- Return a boolean indicating that writing was successful
return true
-- Handle a write error
on error
-- Close the file
try
close access file theFile
end try
-- Return a boolean indicating that writing failed
return false
end try
end writeTextToFile
@patterns
Copy link
Author

patterns commented Oct 8, 2016

Script Editor export script out to ~/Library/Services/spotifytrackinfo.app (subdirectory) and pair with com.spotify.trackinfo.plist attached:

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