--iTunes LCDproc sends your currently playing iTunes song to an LCDproc server. --Place iTunes LCDproc in ~/Library/iTunes/Scripts to make it available to iTunes. --If the Scripts folder does not exist, make it. --Copyright (c) 2008 Eric Pooch --set these to where your LCDproc server is running set server_addr to "127.0.0.1" set server_port to "13666" global new_track set new_track to " " global newline set newline to ASCII character 10 global error_cnt set error_cnt to 0 global expect_cmd_path set expect_cmd_path to POSIX path of (path to temporary items folder) & "tmp_expect_itunes_lcdproc" set expect_cmd_file to POSIX file expect_cmd_path global expect_input_path global expect_input_file set expect_input_path to POSIX path of (path to temporary items folder) & "tmp_output_itunes_lcdproc" set expect_input_file to POSIX file expect_input_path set expect_script to " #!/usr/bin/expect -f # Sample telnet to LCDproc automation ## call with lcdproc_sess \"/path/to/input_file\" set input_file [lindex $argv 0] set screen_id \"itunes_direct\" spawn telnet " & server_addr & " " & server_port & " set telnet_id $spawn_id set timeout 4 expect \"Connected\" { set timeout 10 send \"hello\\n\" expect \"connect\" expect -re \"lcd wid (.*) hgt (.*) cellwid\" set screen_wid $expect_out(1,string) set screen_hgt $expect_out(2,string) send \"client_set -name $screen_id \\n\" send \"screen_add $screen_id \\n\" expect \"success\" send \"widget_add $screen_id title1 title\\n\" expect \"success\" send \"widget_add $screen_id scroller2 scroller\\n\" expect \"success\" if {[file exists $input_file]} { spawn tail -f -n1 $input_file set tail_id $spawn_id while {[file exists $input_file]} { expect -re \"<<(.*)><(.*)>>\" { set screen_title $expect_out(1,string) set screen_text $expect_out(2,string) set spawn_id $telnet_id send \"widget_set $screen_id title1 {$screen_title}\\n\" send \"widget_set $screen_id scroller2 1 2 $screen_wid 1 m 3 {$screen_text}\\n\" set spawn_id $tail_id } } }} exit " try set tmp_file_ref to open for access expect_cmd_file with write permission write expect_script to tmp_file_ref close access tmp_file_ref set tmp_file_ref to open for access expect_input_file with write permission set eof of tmp_file_ref to 0 write "<>" & newline to tmp_file_ref close access tmp_file_ref -- Start the connection and see if it worked. do shell script "/usr/bin/expect -f " & expect_cmd_path & " " & expect_input_path & " &> /dev/null & echo $!" set expect_pid to the result delay 5 do shell script "/bin/ps -p " & expect_pid & " | /usr/bin/wc -l" set shell_result to the result as number if shell_result ² 1 then --we weren't able to connect error "Unable to connect to LCDproc server at: " & server_addr & ":" & server_port & Â ". Set the correct IP address and port number in the script." number -915 end if on error error_message number error_number writeErrorToConsoleLog(error_message) set error_message to error_message & " Fatal error, click OK to quit script." display alert error_message as critical giving up after 10 quit end try on idle try --Quit when iTunes quits. tell application "System Events" set iTunes_active to ((application processes whose (name is equal to "iTunes")) count) end tell if (iTunes_active ² 0) then error "iTunes is not running." number -600 on error error_message number error_number set error_message to error_message & " Click OK to quit script." display alert error_message as critical giving up after 10 quit return end try try --Get the current song tell application "iTunes" set current_track to name of current track if current stream title is missing value then set current_artist to artist of current track else set current_artist to current stream title end if end tell if new_track is not equal to current_track then set new_track to current_track set current_track to (newline & "<<" & current_track & "><" & current_artist & ">>" & newline) -- Write to the file expect is checking with the new information set tmp_file_ref to open for access expect_input_file with write permission write current_track to tmp_file_ref starting at (get eof of tmp_file_ref) as Çclass utf8È close access tmp_file_ref set error_cnt to 0 end if on error error_message number error_number --Log the error message once only. if error_cnt ² 0 then set error_cnt to 1 writeErrorToConsoleLog(error_message) end if end try -- Check if there is a new song in 15 seconds return 15 end idle on quit try do shell script "rm " & quoted form of expect_cmd_path & " " & quoted form of expect_input_path on error error_message number error_number writeErrorToConsoleLog(error_message) end try continue quit end quit on writeErrorToConsoleLog(my_error_string) --Log to console.log with a nice format. do shell script "echo `date +\"%F %X.000 \"` " & "iTunes LCDproc[`echo $PPID`] " & quoted form of my_error_string & " > /dev/console &" end writeErrorToConsoleLog