Skip to content

Instantly share code, notes, and snippets.

@mseymour
Created August 19, 2013 21:50
Show Gist options
  • Save mseymour/6274600 to your computer and use it in GitHub Desktop.
Save mseymour/6274600 to your computer and use it in GitHub Desktop.
A Textual plugin script that gets the currently playing song from Cog, and scrapes the UI for the current position to calculate time remaining.
-- Copyright © 2013 Mark Seymour (mark.seymour.ns@gmail.com)
on textualcmd()
tell application "System Events"
tell application "System Events" to set isRunning to exists (processes where name is "Cog")
set curpos to value of static text of group 2 of tool bar 1 of window of application process "Cog"
end tell
set listening to "/me np: "
tell application "Cog"
set ce to properties of currententry
if (artist of ce ≠ missing value) then set listening to listening & (artist of ce) & " - "
if (title of ce ≠ missing value) then set listening to listening & (title of ce) & " "
if (album of ce ≠ missing value) then set listening to listening & "[" & (album of ce) & "] "
set listening to listening & "-" & my float2time(my elapsed2remaining(curpos as text, length of ce)) & " / " & my float2time(length of ce)
end tell
return listening
end textualcmd
on float2time(duration)
set mins to duration div minutes
set secs to (duration mod minutes) as integer
return ((mins as text) & ":" & (secs as text))
end float2time
on elapsed2remaining(elapsed, duration)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ":"
set esecs to ((first text item of elapsed as integer) * 60) + (last text item of elapsed as integer)
set AppleScript's text item delimiters to tid
return (duration as integer) - esecs
end elapsed2remaining
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment