PDA

View Full Version : Control SlimServer _from_ iTunes



Craig, James (IT)
2004-10-04, 02:20
It seems there are some appleScript genii here.
Perhaps someone can help me with the appleScript to make my iTunesUpdate plugin work on the Mac?
My best guess at the appleScript to update the play count and play time for any track is this:

tell application "iTunes"
set theTrack to (the first track in library playlist 1 whose location is equal to "<filename & path here>")
set the playedCount of theTrack to (the playedCount of theTrack + 1)
set the playedDate of theTrack to "2004-Oct-04 10:17:40"
end tell

Is this going to work?

James

-----Original Message-----
From: discuss-bounces (AT) lists (DOT) slimdevices.com [mailto:discuss-bounces (AT) lists (DOT) slimdevices.com] On Behalf Of Bradley Mohr
Sent: 03 October 2004 01:55
To: Slim Devices Discussion
Subject: [slim] Control SlimServer _from_ iTunes

Here's an AppleScript I've been using for a while that adds the
currently selected tracks in iTunes to the current playlist or
alternately replaces the playlist with the selected tracks. It's
written for a single player, does little error checking, etc., but it
works for me! It's also available here:

<ftp://ftp.bradandkathy.com/SlimPlay.zip>.

-Brad


-- SlimPlay
-- by Brad Mohr, bmohr (AT) seanet (DOT) com
-- 22 January 2003
-- This script is absolutely free and may be used by anyone for any
purpose.

-- Known Limitations:
-- Currently only supports a single SLIMP3 or Squeezebox.
-- Very little error checking

-- edit these properties to match your setup.
property serverURL : "10.0.0.1" -- URL or IP address of the SlimServer
property serverPort : "9000" -- server port number, default is 9000
property slimpAddress : "10.0.0.2" -- URL or IP address of the SliMP3

tell application "iTunes"
set these_tracks to the selection of browser window 1
if these_tracks is {} then error "No tracks are selected in the front
window."

set should_play to false

display dialog "What would you like to do with these tracks?" buttons
{"Play", "Append"} default button 2
if the button returned of the result is "Play" then set should_play to
true

set is_first_file to true

repeat with a_track in these_tracks
set carbon_filepath to the location of a_track
set track_title to the name of a_track

-- encode_text doesn't handle unicode right, but the finder can
return a properly
-- escaped path for any file, so we'll let it do the heavy lifting
tell application "Finder" to set filepath to URL of carbon_filepath
set escaped_path to encode_text(filepath, true, false) of me
if should_play then
if is_first_file then
playlist_play_track(escaped_path) of me
set is_first_file to false
else
playlist_append_track(escaped_path) of me
end if
else
playlist_append_track(escaped_path) of me
end if
end repeat

if should_play then
begin_playing() of me
end if
end tell


-- encode_text
-- taken from some Apple sample code.
-- doesn't work right WRT unicode characters, so we only use this to do
a final massage after getting a URL from the Finder
-- (see above)
on encode_text(this_text, encode_URL_A, encode_URL_B)
set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
set the URL_B_chars to "().-_:"
set the acceptable_characters to the standard_characters
if encode_URL_A is false then
set the acceptable_characters to the acceptable_characters & the
URL_A_chars
if encode_URL_B is false then
set the acceptable_characters to the acceptable_characters & the
URL_B_chars
set the encoded_text to ""
repeat with this_char in this_text
if this_char is in the acceptable_characters then
set the encoded_text to (the encoded_text & this_char)
else
set the encoded_text to (the encoded_text & encode_char(this_char))
as string
end if
end repeat
return the encoded_text
end encode_text

-- this sub-routine is used to encode a character
-- sample code from Apple
-- (doesn't work right for utf8)
on encode_char(this_char)
set the ASCII_num to (the ASCII number this_char)
set the hex_list to
{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C",
"D", "E", "F"}
set x to item ((ASCII_num div 16) + 1) of the hex_list
set y to item ((ASCII_num mod 16) + 1) of the hex_list
return ("%" & x & y) as string
end encode_char


-- append the new track to the playlist
on playlist_append_track(escapedTrackPath)
-- we use curl here because the URL Access stuff chokes on long URLs
set curl_command to "/usr/bin/curl 'http://" & serverURL & ":" &
serverPort & "/status?p0=playlist&p1=append&p2=" & escapedTrackPath &
"'"
do shell script curl_command
end playlist_append_track


-- replace the existing playlist with the new track
on playlist_play_track(escapedTrackPath)
-- we use curl here because the URL Access stuff chokes on long URLs
set curl_command to "/usr/bin/curl 'http://" & serverURL & ":" &
serverPort & "/status?p0=playlist&p1=play&p2=" & escapedTrackPath & "'"
do shell script curl_command
end playlist_play_track


-- tell the default (or only) server to begin playing
on begin_playing()
-- we use curl here because the URL Access stuff chokes on long URLs
set curl_command to "/usr/bin/curl 'http://" & serverURL & ":" &
serverPort & "/status?p0=play'"
do shell script curl_command
end begin_playing


--
Brad Mohr
bmohr (AT) seanet (DOT) com

Anton F. van der Kraaij
2004-10-04, 03:52
Hi Everyone,

>Perhaps someone can help me with the appleScript to make my iTunesUpdate plugin work on the Mac?
>My best guess at the appleScript to update the play count and play time for any track is this:
>
>
I don't really see what you would like to do. And is it an existing
plugin? The currently playing song in slimserver must update the play
count in iTunes?

Regarding my submitted script, I hope somebody will come up with a
better way: I would like to catch the apple events that iTunes is
sending somehow and then "send" these events to the slimserver. Don't
know how to catch these apple events though, so help will be
appreciated! Such an improved script will be cleaner I think than the
current one, which works well only if the delay between iTunes play and
slimserver play is not too large. Otherwise, the slimserver may cut off
songs at the end, when noticing that iTunes is already playing another
song.

Greetings, Anton.

Oscar Marsh
2004-10-05, 05:44
On 4 Oct 2004, at 10:20 am, Craig, James (IT) wrote:

> tell application "iTunes"
> set theTrack to (the first track in library playlist 1 whose location
> is equal to "<filename & path here>")
> set the playedCount of theTrack to (the playedCount of theTrack + 1)
> set the playedDate of theTrack to "2004-Oct-04 10:17:40"
> end tell

I had a go and gave up. Too many descriptor type mismatches for me. I'm
beginning to really dislike AppleScript. By the way, you need to parse
POSIX paths to Mac-style paths using "POSIX file":
http://www.satimage.fr/software/en/file_paths.html

Oscar Marsh
--
jabber: ojmarsh (AT) amessage (DOT) at