Skip to content

Commit

Permalink
backup to github
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikhl committed Aug 4, 2014
1 parent 1d69292 commit 52e20a2
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.scpt
*.scptd/
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This is a silly makefile to compile an applescript

RM=rm -vrf
APPLESCRIPT=songstat

default: $(APPLESCRIPT).scptd

%.scptd: %.applescript
osacompile -o $@ $?

%.scpt: %.applescript
osacompile -o $@ $?

clean:
@$(RM) *.scpt *.scptd
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,34 @@
songmeter
=========

Stupid script for displaying song info with GeekTool
Stupid script for displaying song info and progress with [GeekTool][gt].


To use
------

1. Run `make` in the source catalog to compile the applescript. i
2. Run one of the script to produce some output

* `osascript songstat.scptd`
* `bash songmeter.sh`

Note that iTunes or Spotify must be running _and_ playing a song for the
script to produce any output

3. Enjoy, or not.

TODO
----

* Learn applescript, clean up the applescript
* Make more effective
* Rewrite the bash script in a more sane language, like python or perl

**OR**

in an *insane* language, like applescript


[gt]: http://projects.tynsoe.org/en/geektool/

55 changes: 55 additions & 0 deletions colors.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#! /bin/bash
#
# Functions to print styled text
#


function ansicolor {

case $1 in
black)
num=0;;
red)
num=1;;
green)
num=2;;
yellow)
num=3;;
blue)
num=4;;
magenta)
num=5;;
cyan)
num=6;;
white)
num=7;;
*)
num="9";;
esac

echo $num
}

ansifg=30
ansibg=40

function bold { echo "\033[1m$@\033[0m"; }
function uline { echo "\033[4m$@\033[0m"; }
function blink { echo "\033[5m$@\033[0m"; }
function reverse { echo "\033[7m$@\033[0m"; }
function frame { echo "\033[51m$@\033[0m"; }
function circle { echo "\033[52m$@\033[0m"; }

function fgcolor { color=$1; shift; echo "\033[$((`ansicolor $color` + $ansifg))m$@\033[0m"; }
function bgcolor { color=$1; shift; echo "\033[$((`ansicolor $color` + $ansibg))m$@\033[0m"; }

#echo $(bold bold text)
#echo $(uline uline line)
#echo $(blink blink blink)
#echo $(reverse reverse asd)
#echo $(frame reverse asd)
#echo $(circle reverse asd)
#echo $(fgcolor gold text goes here)
#echo $(bgcolor blue text goes here)
#echo $(bgcolor red $(fgcolor yellow text goes here))

66 changes: 66 additions & 0 deletions songmeter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#!/bin/bash
# This script formats output from the AppleScript songstat.scpt
# so that it displays nicely in GeekTool.
# Output from songstat.scpt should be a string formatted as:
# <artist>|<song>|<percent completed>
#

# Find our own path
function resolve_symlink # filename
{
declare filename="$1" dir
while [ -h "${filename}" ]; do
dir="$( cd -P "$( dirname "${filename}" )" && pwd )"
filename="$(readlink "${filename}")"
[[ ${filename} != /* ]] && filename="${dir}/${filename}"
done
echo ${filename}
}

# Render progress string
function progress # int parts, int percent, str pre, str post, str space
{
declare -i parts="$1" percent="$2" count=0
declare pre="$3" mark="$4" post="$5" sp="$6"

while [ $count -lt 100 ]; do
count=$(expr $count + $parts)
[ $count -lt $percent ] && printf "%b%s" "$pre" "$sp" && continue
[ $count -lt $(expr $percent + $parts) ] && printf "%b%s" "$mark" "$sp" && continue
printf "%b%s" "$post" "$sp"
done
}

# Format output
function formatsong # data, char
{
#echo $data
declare IFS="|"
declare data=( $1 ) char="$2"

#echo ${data[0]} # Artist
#echo ${data[1]} # Song title
#echo ${data[2]} # Percent completed

declare a="$(fgcolor white $2)"
declare b="$(fgcolor red $2)"
declare c="$(fgcolor black $2)"

echo "${data[0]}"
echo "${data[1]}"
echo $(progress 5 "${data[2]}" "$a" "$b" "$c" "")
}

self_dir=$( dirname $( resolve_symlink ${BASH_SOURCE[0]} ) )

source ${self_dir}/colors.sh

data=`osascript ${self_dir}/songstat.scptd`

if [ -n "$data" ]
then
formatsong "$data" ""
unset data
fi

unset self_dir
96 changes: 96 additions & 0 deletions songstat.applescript
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
script PlayerStatus

property isPlaying : false
property currentSong : ""
property currentArtist : ""
property currentPos : 0
property currentLength : 1

-- reset object - subclasses should fetch data and call setStatus
on updateStatus()
setStatus({false, "", "", 0, 1})
end updateStatus

-- set script object (playing, song, artist, pos, len)
on setStatus(results)
set my isPlaying to item 1 of results
set my currentSong to item 2 of results
set my currentArtist to item 3 of results
set my currentPos to item 4 of results as number
set my currentLength to item 5 of results as number
end setStatus

-- Test if appName is running
on isRunning(appName)
tell application "System Events"
if (name of every process) contains appName then return true
end tell
-- if application appName is running then return true
return false
end isRunning

-- Get current track progress in %
on getProgress()
if not isPlaying then return 0
-- else
return (round ((my currentPos) / (my currentLength)) * 100)
end getProgress

-- return player info as string: "<artist>|<song>|<perentage>"
on getSerialized()
if not isPlaying then return ""
return my currentArtist & "|" & my currentSong & "|" & getProgress()
end getSerialized

end script

script iTunesStatus

property parent : PlayerStatus

-- update and return player state
on updateStatus()
if isRunning("iTunes") then
tell application "iTunes"
if player state is playing then
continue setStatus({true, get name of current track, get artist of current track, get player position, get duration of current track})
else
set my isPlaying to false
end if
end tell
else
continue updateStatus()
end if
end updateStatus

end script

script SpotifyStatus

property parent : PlayerStatus

-- update and return player state
on updateStatus()
if isRunning("Spotify") then
tell application "Spotify"
if player state is playing then
continue setStatus({true, get name of current track, get artist of current track, get player position, get duration of current track})
else
set my isPlaying to false
end if
end tell
else
continue updateStatus()
end if
end updateStatus

end script

set output to ""

repeat with status in {iTunesStatus, SpotifyStatus}
tell status to updateStatus()
if isPlaying of status then return getSerialized() of status as string
end repeat

return ""

0 comments on commit 52e20a2

Please sign in to comment.