Skip to content

Instantly share code, notes, and snippets.

@britishtea
Last active September 28, 2015 06:27
Show Gist options
  • Save britishtea/1398141 to your computer and use it in GitHub Desktop.
Save britishtea/1398141 to your computer and use it in GitHub Desktop.
Modified /np for Textual
on textualcmd(ignore, destination)
if destination is equal to "" then
return "/debug Invalid destination channel."
error number -128
end if
-- iTunes
if isRunning("iTunes") then
tell application "iTunes"
if player state is playing then
set thetrack to current track
if current stream title is not missing value then
return "/sme " & destination & " is listening to " & name of thetrack & ": " & current stream title
else
return "/sme " & destination & " is listening to " & name of thetrack & " by " & artist of thetrack & " (" & my normalTime(player position) & "/" & time of thetrack & ")"
end if
error number -128
end if
end tell
end if
-- Spotify
if isRunning("Spotify") then
tell application "Spotify"
if player state is playing then
return "/sme " & destination & " is listening to " & name of current track & " by " & artist of current track & " (" & my normalTime(round (player position as integer)) & "/" & my normalTime(duration of current track) & ")"
error number -128
end if
end tell
end if
-- Last.fm
set api_key to "" -- Use own API key
set username to "" -- Use own nickname
set base_uri to ("http://ws.audioscrobbler.com/2.0/")
set query to ("method=user.getrecenttracks&user=" & username & "&api_key=" & api_key)
set file_name to POSIX path of ((path to temporary items as Unicode text) & "lastfm.xml")
do shell script "curl -G -d \"" & query & "\" " & base_uri & " > " & file_name
tell application "System Events"
set xml_data to contents of XML file file_name
tell XML element "lfm" of xml_data
if value of XML attribute "status" is "ok" then
tell XML element "recenttracks"
tell XML element "track"
try
if value of XML attribute "nowplaying" is "true" then
set artist to value of XML element "artist"
set track_name to value of XML element "name"
set album to value of XML element "album"
return "/sme " & destination & " is listening to " & track_name & " by " & artist
error number -128
end if
end try
end tell
end tell
end if
end tell
end tell
return "/sme " & destination & " is not playing anything right now"
end textualcmd
on isRunning(mediaPlayer)
tell application "System Events" to return exists (processes where name is mediaPlayer)
end isRunning
on normalTime(totalSeconds)
set theHours to (totalSeconds div hours)
set theRemainderSeconds to (totalSeconds mod hours)
set theMinutes to (theRemainderSeconds div minutes)
set theRemainderSeconds to theRemainderSeconds mod minutes
if theRemainderSeconds < 10 then
set theRemainderSeconds to "0" & theRemainderSeconds as text
end if
if theHours > 0 then
return theHours & ":" & theMinutes & ":" & theRemainderSeconds
else
return theMinutes & ":" & theRemainderSeconds
end if
end normalTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment