• Apfeltalk ändert einen Teil seiner Allgemeinen Geschäftsbedingungen (AGB), das Löschen von Useraccounts betreffend.
    Näheres könnt Ihr hier nachlesen: AGB-Änderung
  • Was gibt es Schöneres als den Mai draußen in der Natur mit allen Sinnen zu genießen? Lasst uns teilhaben an Euren Erlebnissen und macht mit beim Thema des Monats Da blüht uns was! ---> Klick

Applescript -> Telnet -> prozentkodierte Rückgabe erhalten und verarbeiten

VisioNOIR

Holländischer Prinz
Registriert
23.08.09
Beiträge
1.871
Hallo zusammen!

Für mein HTPC-Mac Mini, der u.A. via Logitech Media Server (LMS) als Musik-Server fungiert, hätte ich gerne die "Komfort-Funktion", dass mir auf Begehren gesagt wird (mit der Mac OS-eigenen Sprachausgabe), welches Lied gerade läuft. LMS bietet für solche Spielereien ein Command Line Interface via Telnet an.

Also habe ich gegoogled, jede Menge Dokus und HowTos gelesen und bin zu jenem Ergebnis gelangt:
Code:
tell application "Terminal"
	set artist to ""
	do script "telnet 192.168.178.99 9090"
	set wID to id of front window
	do script "00%3A00%3A00%3A00%3A00%3A01 artist ?" in front window
	delay 1.0
	set wordcount to count words of (contents of window id wID as text)
	repeat with counter from 41 to wordcount
		set artist to artist & (word counter of (contents of window id wID as text))
	end repeat
	do script "exit" in front window
	close front window
end tell
say artist

Das, was ich über Telnet erhalte, sieht ähnlich wie folgt aus:
Code:
Last login: Wed Jan 23 22:20:05 on ttys000
msmini:~ msadmin$ telnet 192.168.178.99 9090
Trying 192.168.178.99...
Connected to msmini.msnetwork.com.
Escape character is '^]'.
00%3A00%3A00%3A00%3A00%3A01 artist ?
00%3A00%3A00%3A00%3A00%3A01 artist Gifts%20From%20Enola

So weit, so gut.

In der Variable "artist" steht nach der Ausführung
Code:
Gifts20From20Enola

Und da ist das Problem.

Wie man sieht, ist die Rückgabe vom LMS-CLI prozentkodiert. Mit meiner (stümperhaften) Methode, den Artist rauszufiltern, werden aber genau jene %-Zeichen aus dem String entfernt, sodass jede Menge hexadezimale Werte erhalten bleiben - in diesem Fall "%20" bzw. nur noch "20" für das Leerzeichen.

"Was tun?", sprach Zeus - zuerst mal "20" durch " " mit folgender Routine ersetzen:
Code:
on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Funktioniert in diesem Fall ganz gut. Wenn ich jetzt aber die zwanzigste Symphonie von Beethoven höre, verschluckt mein Script die 20. Schlecht! ;)


Ich sehe zwar, dass das Problem in der repeat-Schleife vom ersten Quellcode liegt - ich habe aber gerade keine Ahnung, wie ich dieses Problem lösen kann. Mag mich wer auf den richtigen Weg schubsen?


Vielen Dank im Voraus!
Michael
 
Zuletzt bearbeitet:

Pill

Adams Parmäne
Registriert
07.07.12
Beiträge
1.310
Was passiert, wenn du das ausführst:

set artist to (do shell script "echo 00%3A00%3A00%3A00%3A00%3A01 artist ? | telnet 192.168.178.99 9090")

Am Besten wäre es wahrscheinlich ungewünschten Text gleich per sed o.ä. zu entfernen.
 
Zuletzt bearbeitet:

VisioNOIR

Holländischer Prinz
Registriert
23.08.09
Beiträge
1.871
Danke!

Ich erhalte nur die Rückmeldung
Code:
Connection closed by foreign host.
sh: 00%3A00%3A00%3A00%3A00%3A01: command not found

Momentan bin ich zu schlaftrunken, dass ich das Problem analysieren könnte.
In diesem Sinne: Gute Nacht!
 

Pill

Adams Parmäne
Registriert
07.07.12
Beiträge
1.310
War mein Fehler, das kann so nicht funktionieren ;) Ich habs nochmal umgeändert, evtl. muss noch delay/sleep eingebaut werden, damit's funktioniert.

Den Interpreten bekommst du z.B. so aus deinem Text:

grep -o 'artist [^?].*' | sed 's/%20/ /g;s/artist //g'
 
  • Like
Reaktionen: VisioNOIR

VisioNOIR

Holländischer Prinz
Registriert
23.08.09
Beiträge
1.871
Vielen Dank für's Schubsen! :)

Der Quellcode sieht jetzt wie folgt aus:
Code:
set artist to lms_send("artist ?", 1)
set artist to (grep_find_simple(artist, "artist [^?].*"))
set artist to (do shell script ("echo " & artist & " | sed 's/artist //g'"))
set artist to urlDecode(artist)

say artist


on lms_send(cmd, wait)
	tell application "Terminal"
		do script "telnet 192.168.178.99 9090"
		set wID to id of front window
		do script "00%3A00%3A00%3A00%3A00%3A01 " & cmd in front window
		delay wait
		set res to (contents of window id wID as text)
		do script "exit" in front window
		close front window
	end tell
	return res
end lms_send

on grep_find_simple(strg, pattern)
	set python_script to quoted form of ¬
		"import re, sys
(s, p, c) = [x.decode('utf_8') for x in sys.argv[1:4]]
options = re.UNICODE | re.MULTILINE | re.IGNORECASE * int( c )
print re.search(p, s, options).group(0).encode('utf_8')"
	set pattern to quoted form of pattern
	set strg to quoted form of strg
	set ignore_case to ("a" = "A") as integer
	set cmd to "python -c " & python_script & space & strg & space & pattern & space & ignore_case
	try
		set r to do shell script cmd
	on error the_error number the_number
		-- remove following comment marks if errors are needed
		error the_error number the_number
		set r to ""
	end try
	return r
end grep_find_simple

on urlDecode(str)
	local str
	try
		return (do shell script "/bin/echo " & quoted form of str & ¬
			" | perl -MURI::Escape -lne 'print uri_unescape($_)'")
	on error eMsg number eNum
		error "Can't urlDecode: " & eMsg number eNum
	end try
end urlDecode

Funktioniert hervorragend, auch mit Beethovens zwanzigster Symphonie. ;)


Edith ist das aufploppende Terminal-Fenster gegen den Strich gegangen, also wurde die lms_send-Funktion wie folgt geändert:
Code:
on lms_send(cmd, wait)
	do shell script "echo 00%3A00%3A00%3A00%3A00%3A01 " & cmd & " | nc -i " & wait & " 192.168.178.99 9090"
	return the result
end lms_send

Dauert zwar gefühlt doppelt so lange - aber diesen "Tod" bin ich gerne bereit für die Optik zu sterben. ;)
 
Zuletzt bearbeitet:

VisioNOIR

Holländischer Prinz
Registriert
23.08.09
Beiträge
1.871
Zwischenzeitlich ging Edith die Verzögerung "as designed" zwischen den Sprachausgaben vom Künstler und Titel ziemlich gegen den Strich, so dass ich da noch mal Hand anlegen musste.

LMS bietet auch eine http-JSON-Schnittstelle zur Kommunikation an. Diese habe ich wie folgt genutzt:

Code:
set Voice to "Vicki"
set VoiceVolume to 30
set CLI_Delay to 1.5

do shell script "curl -v -H -X POST -d '{\"id\":1,\"method\":\"slim.request\",\"params\":[\"00:00:00:00:00:01\",[\"status\",\"-\",1,\"tags:al\"]]}' http://192.168.178.99:9000/jsonrpc.js"
set jsonres to the result

set artistStart to offset of "artist" in jsonres
set artistEnd to offset of "album" in jsonres
set artist to (characters (artistStart + 9) thru (artistEnd - 4) of jsonres) as string

set titleStart to offset of "title" in jsonres
set titleEnd to offset of "artist" in jsonres
set title to (characters (titleStart + 8) thru (titleEnd - 4) of jsonres) as string

tell application "Remote Buddy"
	osdmessage display text "Squeezebox:NowPlaying…" for duration CLI_Delay * 5
end tell

set curVolume to output volume of (get volume settings)

set volume output volume VoiceVolume
say "now playing" using Voice
set volume output volume curVolume

set artistSay to replace_chars(artist, ".", " ")
set artistSay to replace_chars(artistSay, "&", "and")

tell application "Remote Buddy"
	osdmessage display text artist for duration CLI_Delay * 5
end tell


set volume output volume VoiceVolume
say artistSay
set volume output volume curVolume

tell application "Remote Buddy"
	osdmessage display text artist & " - " & title for duration 5.0
end tell

set titleSay to replace_chars(title, ".", " ")
set titleSay to replace_chars(titleSay, "&", "and")

set volume output volume VoiceVolume
say title using Voice
set volume output volume curVolume

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Nun läuft es zur vollsten Zufriedenheit - ohne irgendeinen Tod sterben zu müssen.



TAGS: Logitech Squeezebox JSON Applescript (nur für die Suchmaschinen-Crawler, da ich recht lange an diesem Stück Code gebastelt habe, weil ich verdammt wenig zur JSON-Kommunikation via %Suchmaschine% gefunden habe...)
 
Zuletzt bearbeitet:

VisioNOIR

Holländischer Prinz
Registriert
23.08.09
Beiträge
1.871
so far sieht der ganze Quatsch nun wie folgt aus:

Code:
global __configsFile
global __configsReadScript
global config

on _prepare()
	set __configsFile to "Macintosh HD:Applications:SqueezePlayScripts:_config:configs.txt"
	set __configsReadScript to "Macintosh HD:Applications:SqueezePlayScripts:_config:ReadConfigFile.scpt"
end _prepare

on writeConfig()
	set configFile to __configsFile
	set config to {volMute:0, volNormal:70, volStep:3, volSpeech:30}
	set config to config & {timeStepSmall:5, timeStepMid:10, timeStepLarge:30}
	set config to config & {enVoice:"Vicki", deVoice:"Steffi"}
	set config to config & {lmsIP:"192.168.178.99 ", lmsHTTPPort:"9000", lmsCLIPort:"9090", lmsJSON:"/jsonrpc.js"}
	set config to config & {miniSqueezeMAC:"00:00:00:00:00:01", miniSqueezeMACPercent:"00%3A00%3A00%3A00%3A00%3A01"}
	set config to config & {osdMsgShow:true, osdMsgDurShort:1, osdMsgDurMid:3, osdMsgDurLong:6}
	set config to config & {lng:"en"}
	set config to config & {enSeconds:"seconds", deSeconds:"Sekunden"}
	set config to config & {enNxtTrack:"Squeezebox:Next Track", deNxtTrack:"Squeezebox:Nächster Titel"}
	set config to config & {enPrvTrack:"Squeezebox:Previous Track", dePrvTrack:"Squeezebox:Vorheriger Titel"}
	set config to config & {enVolUp:"Squeezebox:VolumeUp", deVolUp:"Squeezebox:Lauter"}
	set config to config & {enVolDown:"Squeezebox:VolumeDown", deVolDown:"Squeezebox:Leiser"}
	set config to config & {enVolMute:"Squeezebox:Mute", deVolMute:"Squeezebox:Stumm"}
	set config to config & {enVolNorm:"Squeezebox:Volume normalized", deVolNorm:"Squeezebox:Lautstärke normalisiert"}
	set config to config & {enVolMuteToggle:"Squeezebox:Mute toggle", deVolMuteToggle:"Squeezebox:Stumm umschalten"}
	set config to config & {enPlay:"Squeezebox:Play", deVolMuteToggle:"Squeezebox:Abspielen"}
	set config to config & {enPause:"Squeezebox:Pause", dePause:"Squeezebox:Pause"}
	set config to config & {enPauseToggle:"Squeezebox:Pause toggle", dePauseToggle:"Squeezebox:Pause umschalten"}
	set config to config & {enStop:"Squeezebox:Stop", deStop:"Squeezebox:Stopp"}
	set config to config & {enTimeForward:"Squeezebox:Forward", deTimeForward:"Squeezebox:Vorwärts"}
	set config to config & {enTimeBackward:"Squeezebox:Backward", deTimeBackward:"Squeezebox:Rückwärts"}
	set config to config & {enRndAll:"Squeezebox:Random play", deRndAll:"Squeezebox:Zufällig abspielen"}
	set config to config & {enRndTripHop:"Squeezebox:Random play : Trip-Hop", deRndTripHop:"Squeezebox:Zufällig abspielen : Trip-Hop"}
	set config to config & {enRndIDM:"Squeezebox:Random play : IDM", deRndIDM:"Squeezebox:Zufällig abspielen : IDM"}
	set config to config & {enRndElectronica:"Squeezebox:Random play : Electronica", deRndElectronica:"Squeezebox:Zufällig abspielen : Electronica"}
	set config to config & {enRndGoa:"Squeezebox:Random play : Goa", deRndGoa:"Squeezebox:Zufällig abspielen : Goa"}
	set config to config & {enRndIndie:"Squeezebox:Random play : Indie", deRndIndie:"Squeezebox:Zufällig abspielen : Indie"}
	set config to config & {enRndClassical:"Squeezebox:Random play : Classical", deRndClassical:"Squeezebox:Zufällig abspielen : Klassik"}
	set config to config & {enRndPostRock:"Squeezebox:Random play : Post Rock", deRndPostRock:"Squeezebox:Zufällig abspielen : Post Rock"}
	set config to config & {enRadio_RadioRSG:"Squeezebox:InternetRadio : Radio RSG", deRadio_RadioRSG:"Squeezebox:Internetradio : Radio RSG"}
	set config to config & {enRadio_1Live:"Squeezebox:InternetRadio : 1Live", deRadio_1Live:"Squeezebox:Internetradio : 1Live"}
	set config to config & {enRadio_WDR2:"Squeezebox:InternetRadio : WDR2 - Bergisches Land", deRadio_WDR2:"Squeezebox:Internetradio : WDR2 - Bergisches Land"}
	set config to config & {enNowPlaying:"Squeezebox:NowPlaying", deNowPlaying:"Squeezebox:Wird abgespielt"}
	set config to config & {enSayNowPlaying:"now playing", deSayNowPlaying:"wird abgespielt"}
	
	set fRef to (open for access file configFile with write permission)
	try
		set eof fRef to 0
		write config to fRef
	end try
	close access fRef
	return config as record
end writeConfig

on readConfig()
	_prepare()
	set configFile to __configsReadScript
	set config to run script (configFile as alias)
end readConfig

on sendCLICommand(cmd)
	do shell script "echo " & miniSqueezeMACPercent of config & " " & cmd & " | nc " & lmsIP of config & lmsCLIPort of config
end sendCLICommand

on getJSON()
	set http to "http://" & lmsIP of config & ":" & lmsHTTPPort of config & lmsJSON of config
	set playerIP to miniSqueezeMAC of config
	set json_cmd to "status"
	set json_par1 to "-"
	set json_par2 to "1"
	set json_par3 to "tags:al"
	do shell script "curl -v -H -X POST -d '{\"id\":1,\"method\":\"slim.request\",\"params\":[\"" & playerIP & "\",[\"" & json_cmd & "\",\"" & json_par1 & "\"," & json_par2 & ",\"" & json_par3 & "\"]]}' " & http
	return the result
end getJSON

on filterJSON(key, json)
	set s to 0
	set e to 0
	set res to ""
	if key = "artist" then
		set s to offset of "artist\"" in json
		set e to offset of "album\"" in json
	end if
	if key = "title" then
		set s to offset of "title\"" in json
		set e to offset of "artist\"" in json
	end if
	if key = "volume" then
		set s to offset of "volume\"" in json
		set e to offset of "player_name\"" in json
	end if
	set res to (characters (s + ((length of key) + 3)) thru (e - 4) of json) as string
	if key = "artist" then set res to normalizeArtist(res)
	if key = "volume" then
		if length of res = 3 then set res to character 2 of res
	end if
	return res
end filterJSON

on _normalizeArtist(theArtist)
	set res to theArtist
	set commaOffset to offset of "," in theArtist
	if commaOffset > 0 then
		set res to (characters (commaOffset + 2) thru ((commaOffset + (length of theArtist)) - commaOffset) of theArtist) as string
		set res to res & " " & (characters 1 thru (commaOffset - 1) of theArtist) as string
	end if
	return res
end _normalizeArtist

on normalizeArtist(theArtist)
	set res to theArtist
	set commaOffset to offset of "," in theArtist
	if commaOffset > 0 then
		set savedDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {" "}
		set delimitedList to every text item of theArtist
		set res to item 2 of delimitedList as string
		set res to res & " " & item 1 of delimitedList as string
		
		repeat with counter from 3 to length of delimitedList
			set res to res & " " & item counter of delimitedList as string
		end repeat
		
		set AppleScript's text item delimiters to {","}
		set delimitedList to every text item of res
		set res to ""
		repeat with counter from 1 to length of delimitedList
			set res to res & " " & item counter of delimitedList as string
		end repeat
		set AppleScript's text item delimiters to savedDelimiters
	end if
	return res
end normalizeArtist

on getText_(key)
	-- http://latenightsw.com/freeware/RecordTools/index.html
	set str to ""
	--set lngKey to get user property "lng" in config
	--set lngKey to lngKey & key
	--return get user property lngKey in config
	return str
end getText_

on getText__(key)
	set str to ""
	--set myScript to load script "Macintosh HD:Applications:SqueezePlayScripts:_ReturnText.scpt" as alias
	--tell myScript to returnText(key)
	--set str to the result
	return str
end getText__

on getText(key)
	set str to "%DUMMY%"
	if key = "TimeForward" then set str to enTimeForward of config
	if key = "TimeBackward" then set str to enTimeBackward of config
	if key = "Seconds" then set str to enSeconds of config
	if key = "NxtTrack" then set str to enNxtTrack of config
	if key = "PrvTrack" then set str to enPrvTrack of config
	if key = "NxtTrack" then set str to enNxtTrack of config
	if key = "VolUp" then set str to enVolUp of config
	if key = "VolDown" then set str to enVolDown of config
	if key = "VolMute" then set str to enVolMute of config
	if key = "VolNorm" then set str to enVolNorm of config
	if key = "VolMuteToggle" then set str to enVolMuteToggle of configen
	if key = "Play" then set str to enPlay of config
	if key = "Pause" then set str to enPause of config
	if key = "PauseToggle" then set str to enPuaseToggle of config
	if key = "Stop" then set str to enStop of config
	if key = "RndAll" then set str to enRndAll of config
	if key = "RndTripHop" then set str to enRndTripHop of config
	if key = "RndIDM" then set str to enRndIDM of config
	if key = "RndElectronica" then set str to enRndElectronica of config
	if key = "RndGoa" then set str to enRndGoa of config
	if key = "RndIndie" then set str to enRndIndie of config
	if key = "RndClassical" then set str to enRndClassical of config
	if key = "RndPostRock" then set str to enRndPostRock of config
	if key = "Radio_RadioRSG" then set str to enRadio_RadioRSG of config
	if key = "Radio_1Live" then set str to enRadio_1Live of config
	if key = "Radio_WDR2" then set str to enRadio_WDR2 of config
	if key = "NowPlaying" then set str to enNowPlaying of config
	if key = "SayNowPlaying" then set str to enSayNowPlaying of config
	return str
end getText

on displayMsg(msg, dur)
	if osdMsgShow of config then tell application "Remote Buddy" to osdmessage display text msg for duration dur
end displayMsg

on sayMsg(msg)
	set curVolume to output volume of (get volume settings)
	set volume output volume volSpeech of config
	if lng of config = "en" then say msg using enVoice of config
	if lng of config = "de" then say msg using deVoice of config
	set volume output volume curVolume
end sayMsg

-- -------------------------------

on SB_CheckSqueezeslave()
	do shell script "ps -ef | grep squeezeslave"
	set x to offset of "/Applications/Squeezeslave/squeezeslave" in the result
	if x = 0 then
		do shell script "/Applications/Squeezeslave/squeezeslave -o2 -R -r10 -M -m10:10:10:10:10:10"
		delay 2
		SB_Play()
	end if
end SB_CheckSqueezeslave

on SB_NextTrack()
	readConfig()
	displayMsg(getText("NxtTrack"), osdMsgDurMid of config)
	sendCLICommand("playlist index +1")
	set json to getJSON()
	set artist to filterJSON("artist", json)
	set title to filterJSON("title", json)
	delay (osdMsgDurShort of config)
	displayMsg(artist & " - " & title, osdMsgDurLong of config)
end SB_NextTrack

on SB_PreviousTrack()
	readConfig()
	displayMsg(getText("PrvTrack"), osdMsgDurMid of config)
	sendCLICommand("playlist index -1")
	set json to getJSON()
	set artist to filterJSON("artist", json)
	set title to filterJSON("title", json)
	delay (osdMsgDurShort of config)
	displayMsg(artist & " - " & title, osdMsgDurLong of config)
end SB_PreviousTrack

on SB_VolUp()
	readConfig()
	set lmsCLICommand to "mixer volume +" & volStep of config
	sendCLICommand(lmsCLICommand)
	set json to getJSON()
	set vol to filterJSON("volume", json)
	displayMsg(getText("VolUp") & " (" & vol & "%)", osdMsgDurShort of config)
end SB_VolUp

on SB_VolDown()
	readConfig()
	set lmsCLICommand to "mixer volume -" & volStep of config
	sendCLICommand(lmsCLICommand)
	set json to getJSON()
	set vol to filterJSON("volume", json)
	displayMsg(getText("VolDown") & " (" & vol & "%)", osdMsgDurShort of config)
end SB_VolDown

on SB_VolMute()
	readConfig()
	set lmsCLICommand to "mixer volume " & volMute of config
	sendCLICommand(lmsCLICommand)
	set json to getJSON()
	set vol to filterJSON("volume", json)
	displayMsg(getText("VolMute") & " (" & vol & "%)", osdMsgDurShort of config)
end SB_VolMute

on SB_VolNorm()
	readConfig()
	set lmsCLICommand to "mixer volume " & volNormal of config
	sendCLICommand(lmsCLICommand)
	set json to getJSON()
	set vol to filterJSON("volume", json)
	displayMsg(getText("VolNorm") & " (" & vol & "%)", osdMsgDurShort of config)
end SB_VolNorm

on SB_VolMuteToggle()
	readConfig()
	sendCLICommand("mixer muting")
	set json to getJSON()
	set vol to filterJSON("volume", json)
	displayMsg(getText("VolMuteToggle"), osdMsgDurShort of config)
end SB_VolMuteToggle

on SB_Play()
	readConfig()
	sendCLICommand("play")
	displayMsg(getText("Play"), osdMsgDurShort of config)
end SB_Play

on SB_Pause()
	readConfig()
	sendCLICommand("pause 1")
	displayMsg(getText("Pause"), osdMsgDurShort of config)
end SB_Pause

on SB_PauseToggle()
	readConfig()
	sendCLICommand("pause")
	displayMsg(getText("PauseToggle"), osdMsgDurShort of config)
end SB_PauseToggle

on SB_Stop()
	readConfig()
	sendCLICommand("stop")
	displayMsg(getText("Stop"), osdMsgDurShort of config)
end SB_Stop

on SB_rndAll()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("randomplay tracks")
	displayMsg(getText("RndAll"), osdMsgDurMid of config)
end SB_rndAll

on SB_rndTripHop()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Trip-Hop")
	displayMsg(getText("RndTripHop"), osdMsgDurMid of config)
end SB_rndTripHop

on SB_rndIDM()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=IDM")
	displayMsg(getText("RndIDM"), osdMsgDurMid of config)
end SB_rndIDM

on SB_rndElectronica()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Electronica")
	displayMsg(getText("RndElectronica"), osdMsgDurMid of config)
end SB_rndElectronica

on SB_rndGoa()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Goa")
	displayMsg(getText("RndGoa"), osdMsgDurMid of config)
end SB_rndGoa

on SB_rndIndie()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Indie")
	displayMsg(getText("RndIndie"), osdMsgDurMid of config)
end SB_rndIndie

on SB_rndClassical()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Classical")
	displayMsg(getText("RndClassical"), osdMsgDurMid of config)
end SB_rndClassical

on SB_rndPostRock()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=%22Post%20Rock%22")
	displayMsg(getText("RndPostRock"), osdMsgDurMid of config)
end SB_rndPostRock

on SB_Radio_RadioRSG()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist play __playlists/RadioRSG")
	displayMsg(getText("Radio_RadioRSG"), osdMsgDurMid of config)
end SB_Radio_RadioRSG

on SB_Radio_1Live()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist play __playlists/1Live")
	displayMsg(getText("Radio_1Live"), osdMsgDurMid of config)
end SB_Radio_1Live

on SB_Radio_WDR2()
	readConfig()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist play __playlists/WDR2BergischesLand")
	displayMsg(getText("Radio_WDR2"), osdMsgDurMid of config)
end SB_Radio_WDR2

on SB_TimeChange(step)
	if step > 0 then
		sendCLICommand("time +" & step)
		set str to getText("TimeForward")
	else
		sendCLICommand("time " & step)
		set str to getText("TimeBackward")
	end if
	set str to str & " " & step & " " & getText("Seconds")
	displayMsg(str, osdMsgDurMid of config)
end SB_TimeChange

on SB_TimeForwardSmall()
	readConfig()
	set step to timeStepSmall of config
	SB_TimeChange(step)
end SB_TimeForwardSmall

on SB_TimeForwardMid()
	readConfig()
	set step to timeStepMid of config
	SB_TimeChange(step)
end SB_TimeForwardMid

on SB_TimeForwardLarge()
	readConfig()
	set step to timeStepLarge of config
	SB_TimeChange(step)
end SB_TimeForwardLarge

on SB_TimeBackwardSmall()
	readConfig()
	set step to (timeStepSmall of config) * -1
	SB_TimeChange(step)
end SB_TimeBackwardSmall

on SB_TimeBackwardMid()
	readConfig()
	set step to (timeStepMid of config) * -1
	SB_TimeChange(step)
end SB_TimeBackwardMid

on SB_TimeBackwardLarge()
	readConfig()
	set step to (timeStepLarge of config) * -1
	SB_TimeChange(step)
end SB_TimeBackwardLarge

on SB_SayArtistTitle()
	readConfig()
	set json to getJSON()
	set artist to filterJSON("artist", json)
	set title to filterJSON("title", json)
	displayMsg(getText("NowPlaying"), osdMsgDurMid of config)
	sayMsg(getText("SayNowPlaying"))
	set artistSay to replace_say_chars(artist)
	displayMsg(artist, osdMsgDurLong of config)
	sayMsg(artistSay)
	displayMsg(artist & " - " & title, osdMsgDurLong of config)
	set titleSay to replace_say_chars(title)
	sayMsg(titleSay)
end SB_SayArtistTitle

-- --------------------------------

on replace_say_chars(the_text)
	set res to replace_chars(the_text, ".", " ")
	set res to replace_chars(res, "&", "and")
	return res
end replace_say_chars

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

just FYI (and my Backup ;) )...
 

VisioNOIR

Holländischer Prinz
Registriert
23.08.09
Beiträge
1.871
Edith flüsterte mir gerade, dass ich vergessen habe, warum und wie ich die, mit dem AS-Script geschriebene Config-Datei extern lese, zu erläutern.

Primär wird dieses Script via Remote-Buddy aufgerufen. Remote-Buddy deswegen, weil dieses geniale Programm der IR-Schnittstelle diverser Macs erst Leben einhaucht. Man kann für alle Tasten diverser Apple Remote-Fernbedienungen recht komplexe "Behaiviours" hinterlegen. Anstatt, dass man zig Apple Remotes rumliegen hat, kann man jene codierte IR-Signale auch diversen Multifunktions-Fernbedienungen beibringen (ich benutze dafür eine Logitech Harmony).

Remote Buddy unterstützt aber nur den "Core"-Funktionsumfang von Apple Script. Dateizugriffe gehören nicht dazu. Entsprechend benutze ich den Umweg über ein externes Apple Script, welches die Config-Datei liest und den Inhalt via Rückgabewert weitergibt. Jener Lese-Helfer sieht wie folgt aus:

Code:
on run
	set configFile to "Macintosh HD:Applications:SqueezePlayScripts:_config:configs.txt"
	set result to read file configFile as record
end run


Via Remote Buddy werden die Funktionen des geposteten Apple Scripts ähnlich wie folgt getriggert:
Code:
set myScript to load script "Macintosh HD:Applications:SqueezePlayScripts:AllCommands1#1.scpt" as alias
tell myScript to SB_Play()


Auf diese Weise ist es mir möglich, die komplette Logik in einer Datei (mit der Krücke des Auslesens der config-Datei in zwei derer) zu konzentrieren. Das ermöglicht es mir, umfangreiche Änderungen schnell in nur einem Script zu erledigen - sehr hilfreich und nützlich, wie ich in den vergangenen Wochen, in denen ich immer wieder daran rumgebastelt habe, bemerkt habe...
 
Zuletzt bearbeitet:

Pill

Adams Parmäne
Registriert
07.07.12
Beiträge
1.310
Sag mal warum packst du denn im writeConfig() Handler die config nicht einfach direkt in eine Liste?
 

VisioNOIR

Holländischer Prinz
Registriert
23.08.09
Beiträge
1.871
Gute Frage! Könnte ich machen, durchaus.

Die Intention dahinter war und ist, dass ich nach der Fertigstellung des Scripts dieses nicht mehr anfassen muss, bei Änderungen im Setup nur noch die Config-Datei entsprechend anpassen muss. Dazu sollte ich noch schreiben, dass ich mit jenem Script nicht nur einen Squeezebox-Player steuern möchte, sondern 4. Deswegen versuche ich, das Script so flexibel wie nur Möglich zu gestalten.

Nun, durch den Aufbau eines in einer Datei gespeicherten AS-Records, ist meine Vorgehensweise der "Vereinheitlichung/Vereinfachung" womöglich unnötig kompliziert und nicht mal eben mit einem Texteditor zu bewerkstelligen - was ursprünglich der Plan war.

Ich werde meine Vorgehensweise noch mal überdenken...

Danke für die Nachfrage, die meine heimlichen Zweifel bestärkt haben! ;)


Edith flüstert, dass der AS-Code, auch dank der Nachfrage von Dir, Pill, nun wie folgt aussieht:

Code:
-- ***********************************
-- ******* HOW TO CALL ROUTINES *******

--Internal:
--SB_rndElectronica()

--Applescript:
--set myScript to load script "Macintosh HD:Applications:SqueezePlayScripts:AllCommands1#1.scpt" as alias
--tell myScript to SB_NextTrack()

--Terminal:
--osascript -e 'set myScript to load script "Macintosh HD:Applications:SqueezePlayScripts:AllCommands1#1.scpt" as alias' -e 'tell myScript to SB_MuteIfSomethingIsRunning()'
-- or
--osascript -e "set myScript to load script \"Macintosh HD:Applications:SqueezePlayScripts:AllCommands1#1.scpt\" as alias" -e "tell myScript to SB_MuteIfSomethingIsRunning()"

--LaunchAgent/Lingon:
--<?xml version="1.0" encoding="UTF-8"?>
--<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
--<plist version="1.0">
--<dict>
--	<key>Label</key>
--	<string>com.msnetwork.SqueezeslaveMuter</string>
--	<key>ProgramArguments</key>
--	<array>
--		<string>osascript</string>
--		<string>-e</string>
--		<string>set myScript to load script "Macintosh HD:Applications:SqueezePlayScripts:AllCommands1#1.scpt" as alias</string>
--		<string>-e</string>
--		<string>tell myScript to SB_MuteIfSomethingIsRunning()</string>
--	</array>
--	<key>StartInterval</key>
--	<integer>10</integer>
--</dict>
--</plist>

--<?xml version="1.0" encoding="UTF-8"?>
--<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
--<plist version="1.0">
--<dict>
--	<key>Label</key>
--	<string>com.msnetwork.SqueezeslaveLauncher</string>
--	<key>ProgramArguments</key>
--	<array>
--		<string>osascript</string>
--		<string>-e</string>
--		<string>set myScript to load script "Macintosh HD:Applications:SqueezePlayScripts:AllCommands1#1.scpt" as alias</string>
--		<string>-e</string>
--		<string>tell myScript to SB_CheckSqueezeslave()</string>
--	</array>
--	<key>RunAtLoad</key>
--	<true/>
--	<key>StartInterval</key>
--	<integer>300</integer>
--</dict>
--</plist>

-- ******* HOW TO CALL ROUTINES *******
-- ***********************************


global config
global __currentPlayer
global currentPlayer


-- ******* JUST FOR TESTING-PURPOSE WITHIN APPLESCRIPT-EDITOR *******
--SB_CheckSqueezeslave()
SB_NextTrack()



-- ********************************
-- ******* INTERNAL ROUTINES *******

on _prepare()
	set __currentPlayer to "miniSqueeze"
	
	set config to {volMute:0, volNormal:70, volStep:3, volSpeech:30}
	set config to config & {timeStepSmall:5, timeStepMid:10, timeStepLarge:30}
	set config to config & {enVoice:"Vicki", deVoice:"Steffi"}
	set config to config & {lmsIP:"192.168.178.99 ", lmsHTTPPort:"9000", lmsCLIPort:"9090", lmsJSON:"/jsonrpc.js"}
	set config to config & {squeezeslaveApp:"/Applications/Squeezeslave/squeezeslave"}
	set config to config & {miniSqueezePar:"-o2 -R -r10 -M -m00:00:00:00:00:01", miniSqueezeMAC:"00:00:00:00:00:01", miniSqueezeMACPercent:"00%3A00%3A00%3A00%3A00%3A01"}
	set config to config & {osdMsgShow:true, osdMsgDurShort:1, osdMsgDurMid:3, osdMsgDurLong:6}
	set config to config & {lng:"en"}
	set config to config & {enSeconds:"seconds", deSeconds:"Sekunden"}
	set config to config & {enNxtTrack:"Squeezebox:Next Track", deNxtTrack:"Squeezebox:Nächster Titel"}
	set config to config & {enPrvTrack:"Squeezebox:Previous Track", dePrvTrack:"Squeezebox:Vorheriger Titel"}
	set config to config & {enVolUp:"Squeezebox:VolumeUp", deVolUp:"Squeezebox:Lauter"}
	set config to config & {enVolDown:"Squeezebox:VolumeDown", deVolDown:"Squeezebox:Leiser"}
	set config to config & {enVolMute:"Squeezebox:Mute", deVolMute:"Squeezebox:Stumm"}
	set config to config & {enVolNorm:"Squeezebox:Volume normalized", deVolNorm:"Squeezebox:Lautstärke normalisiert"}
	set config to config & {enVolMuteToggle:"Squeezebox:Mute toggle", deVolMuteToggle:"Squeezebox:Stumm umschalten"}
	set config to config & {enPlay:"Squeezebox:Play", deVolMuteToggle:"Squeezebox:Abspielen"}
	set config to config & {enPause:"Squeezebox:Pause", dePause:"Squeezebox:Pause"}
	set config to config & {enPauseToggle:"Squeezebox:Pause toggle", dePauseToggle:"Squeezebox:Pause umschalten"}
	set config to config & {enStop:"Squeezebox:Stop", deStop:"Squeezebox:Stopp"}
	set config to config & {enTimeForward:"Squeezebox:Forward", deTimeForward:"Squeezebox:Vorwärts"}
	set config to config & {enTimeBackward:"Squeezebox:Backward", deTimeBackward:"Squeezebox:Rückwärts"}
	set config to config & {enRndAll:"Squeezebox:Random play", deRndAll:"Squeezebox:Zufällig abspielen"}
	set config to config & {enRndTripHop:"Squeezebox:Random play : Trip-Hop", deRndTripHop:"Squeezebox:Zufällig abspielen : Trip-Hop"}
	set config to config & {enRndIDM:"Squeezebox:Random play : IDM", deRndIDM:"Squeezebox:Zufällig abspielen : IDM"}
	set config to config & {enRndElectronica:"Squeezebox:Random play : Electronica", deRndElectronica:"Squeezebox:Zufällig abspielen : Electronica"}
	set config to config & {enRndGoa:"Squeezebox:Random play : Goa", deRndGoa:"Squeezebox:Zufällig abspielen : Goa"}
	set config to config & {enRndIndie:"Squeezebox:Random play : Indie", deRndIndie:"Squeezebox:Zufällig abspielen : Indie"}
	set config to config & {enRndClassical:"Squeezebox:Random play : Classical", deRndClassical:"Squeezebox:Zufällig abspielen : Klassik"}
	set config to config & {enRndPostRock:"Squeezebox:Random play : Post Rock", deRndPostRock:"Squeezebox:Zufällig abspielen : Post Rock"}
	set config to config & {enRadio_RadioRSG:"Squeezebox:InternetRadio : Radio RSG", deRadio_RadioRSG:"Squeezebox:Internetradio : Radio RSG"}
	set config to config & {enRadio_1Live:"Squeezebox:InternetRadio : 1Live", deRadio_1Live:"Squeezebox:Internetradio : 1Live"}
	set config to config & {enRadio_WDR2:"Squeezebox:InternetRadio : WDR2 - Bergisches Land", deRadio_WDR2:"Squeezebox:Internetradio : WDR2 - Bergisches Land"}
	set config to config & {enNowPlaying:"Squeezebox:NowPlaying", deNowPlaying:"Squeezebox:Wird abgespielt"}
	set config to config & {enSayNowPlaying:"now playing", deSayNowPlaying:"wird abgespielt"}
	
	setPlayer(__currentPlayer)
end _prepare

on sendCLICommand(cmd)
	do shell script "echo " & MACPercent of currentPlayer & " " & cmd & " | nc " & lmsIP of config & lmsCLIPort of config
end sendCLICommand

on getJSON()
	set http to "http://" & lmsIP of config & ":" & lmsHTTPPort of config & lmsJSON of config
	set playerIP to MAC of currentPlayer
	set json_cmd to "status"
	set json_par1 to "-"
	set json_par2 to "1"
	set json_par3 to "tags:al"
	do shell script "curl -v -H -X POST -d '{\"id\":1,\"method\":\"slim.request\",\"params\":[\"" & playerIP & "\",[\"" & json_cmd & "\",\"" & json_par1 & "\"," & json_par2 & ",\"" & json_par3 & "\"]]}' " & http
	return the result
end getJSON

on filterJSON(_key, _json)
	set s to 0
	set e to 0
	set res to ""
	if _key = "artist" then
		set s to offset of "artist\"" in _json
		set e to offset of "album\"" in _json
	end if
	if _key = "title" then
		set s to offset of "title\"" in _json
		set e to offset of "artist\"" in _json
	end if
	if _key = "volume" then
		set s to offset of "volume\"" in _json
		set e to offset of "player_name\"" in _json
	end if
	set res to (characters (s + ((length of _key) + 3)) thru (e - 4) of _json) as string
	if _key = "artist" then set res to normalizeArtist(res)
	if _key = "volume" then
		if length of res = 3 then set res to character 2 of res
	end if
	return res
end filterJSON

on normalizeArtist(_artist)
	set res to _artist
	set commaOffset to offset of "," in _artist
	if commaOffset > 0 then
		set savedDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {" "}
		set delimitedList to every text item of _artist
		set res to item 2 of delimitedList as string
		set res to res & " " & item 1 of delimitedList as string
		repeat with counter from 3 to length of delimitedList
			set res to res & " " & item counter of delimitedList as string
		end repeat
		set AppleScript's text item delimiters to {","}
		set delimitedList to every text item of res
		set res to ""
		repeat with counter from 1 to length of delimitedList
			set res to res & " " & item counter of delimitedList as string
		end repeat
		set AppleScript's text item delimiters to savedDelimiters
	end if
	return res
end normalizeArtist

on getText(_key)
	-- http://latenightsw.com/freeware/RecordTools/index.html
	set str to ""
	set lngKey to get user property "lng" in config
	set lngKey to lngKey & _key
	return get user property lngKey in config
	return str
end getText

on displayMsg(_msg, _dur)
	if osdMsgShow of config then tell application "Remote Buddy" to osdmessage display text _msg for duration _dur
end displayMsg

on displayCurrentArtistTitle()
	set json to getJSON()
	set artist to filterJSON("artist", json)
	set title to filterJSON("title", json)
	delay (osdMsgDurShort of config)
	displayMsg(artist & " - " & title, osdMsgDurLong of config)
end displayCurrentArtistTitle


on sayMsg(_msg)
	set curVolume to output volume of (get volume settings)
	set volume output volume volSpeech of config
	if lng of config = "en" then say _msg using enVoice of config
	if lng of config = "de" then say _msg using deVoice of config
	set volume output volume curVolume
end sayMsg

-- ******* INTERNAL ROUTINES *******
-- ********************************

-- ********************************
-- ******* EXTERNAL ROUTINES *******

on SB_CheckSqueezeslave()
	readConfig()
	do shell script "ps -ef | grep squeezeslave"
	set x to offset of (squeezeslaveApp of config) in the result
	if x = 0 then
		do shell script squeezeslaveApp of config & " " & miniSqueezePar of config
		delay 2
		SB_Play()
	end if
end SB_CheckSqueezeslave

on SB_MuteIfSomethingIsRunning()
	set MuteSqueezePlay to 0
	if application "Plex" is running then
		set MuteSqueezePlay to MuteSqueezePlay + 1
	end if
	if application "Blu-ray Player" is running then
		set MuteSqueezePlay to MuteSqueezePlay + 1
	end if
	if application "DVD Player" is running then
		set MuteSqueezePlay to MuteSqueezePlay + 1
	end if
	if application "MPlayerX" is running then
		set MuteSqueezePlay to MuteSqueezePlay + 1
	end if
	if application "QuickTime Player" is running then
		set MuteSqueezePlay to MuteSqueezePlay + 1
	end if
	if application "VLC" is running then
		set MuteSqueezePlay to MuteSqueezePlay + 1
	end if
	tell application "EyeTV"
		set EyeTVPlays to playing
		if EyeTVPlays then
			set MuteSqueezePlay to MuteSqueezePlay + 1
		end if
	end tell
	
	--readConfig()
	if MuteSqueezePlay ≥ 1 then
		SB_VolMuteOnOff(1)
		--displayMsg("Squeezebox:AutoMute", osdMsgDurShort of config)
	else
		SB_VolMuteOnOff(0)
		--displayMsg("Squeezebox:AutoUnMute", osdMsgDurShort of config)
	end if
end SB_MuteIfSomethingIsRunning

on setPlayer(_player)
	set currentPlayer to {PlayerName:_player}
	set currentPlayer to currentPlayer & {MAC:get user property (_player & "MAC") in config}
	set currentPlayer to currentPlayer & {MACPercent:get user property (_player & "MACPercent") in config}
end setPlayer

on SB_NextTrack()
	_prepare()
	displayMsg(getText("NxtTrack"), osdMsgDurMid of config)
	sendCLICommand("playlist index +1")
	displayCurrentArtistTitle()
end SB_NextTrack

on SB_PreviousTrack()
	_prepare()
	displayMsg(getText("PrvTrack"), osdMsgDurMid of config)
	sendCLICommand("playlist index -1")
	displayCurrentArtistTitle()
end SB_PreviousTrack

on SB_VolUp()
	_prepare()
	set lmsCLICommand to "mixer volume +" & volStep of config
	sendCLICommand(lmsCLICommand)
	set json to getJSON()
	set vol to filterJSON("volume", json)
	displayMsg(getText("VolUp") & " (" & vol & "%)", osdMsgDurShort of config)
end SB_VolUp

on SB_VolDown()
	_prepare()
	set lmsCLICommand to "mixer volume -" & volStep of config
	sendCLICommand(lmsCLICommand)
	set json to getJSON()
	set vol to filterJSON("volume", json)
	displayMsg(getText("VolDown") & " (" & vol & "%)", osdMsgDurShort of config)
end SB_VolDown

on SB_VolMute()
	_prepare()
	set lmsCLICommand to "mixer volume " & volMute of config
	sendCLICommand(lmsCLICommand)
	set json to getJSON()
	set vol to filterJSON("volume", json)
	displayMsg(getText("VolMute") & " (" & vol & "%)", osdMsgDurShort of config)
end SB_VolMute

on SB_VolNorm()
	_prepare()
	set lmsCLICommand to "mixer volume " & volNormal of config
	sendCLICommand(lmsCLICommand)
	set json to getJSON()
	set vol to filterJSON("volume", json)
	displayMsg(getText("VolNorm") & " (" & vol & "%)", osdMsgDurShort of config)
end SB_VolNorm

on SB_VolMuteToggle()
	_prepare()
	sendCLICommand("mixer muting toggle")
	displayMsg(getText("VolMuteToggle"), osdMsgDurShort of config)
end SB_VolMuteToggle

on SB_VolMuteOnOff(muting)
	_prepare()
	sendCLICommand("mixer muting " & muting)
end SB_VolMuteOnOff

on SB_Play()
	_prepare()
	sendCLICommand("play")
	displayMsg(getText("Play"), osdMsgDurShort of config)
	delay (osdMsgDurShort of config)
	--displayCurrentArtistTitle()
end SB_Play

on SB_Pause()
	_prepare()
	sendCLICommand("pause 1")
	displayMsg(getText("Pause"), osdMsgDurShort of config)
end SB_Pause

on SB_PauseToggle()
	_prepare()
	sendCLICommand("pause")
	displayMsg(getText("PauseToggle"), osdMsgDurShort of config)
end SB_PauseToggle

on SB_Stop()
	_prepare()
	sendCLICommand("stop")
	displayMsg(getText("Stop"), osdMsgDurShort of config)
end SB_Stop

on SB_rndAll()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("randomplay tracks")
	displayMsg(getText("RndAll"), osdMsgDurMid of config)
	sendCLICommand("play")
	displayCurrentArtistTitle()
end SB_rndAll

on SB_rndTripHop()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Trip-Hop")
	displayMsg(getText("RndTripHop"), osdMsgDurMid of config)
	sendCLICommand("play")
	displayCurrentArtistTitle()
end SB_rndTripHop

on SB_rndIDM()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=IDM")
	displayMsg(getText("RndIDM"), osdMsgDurMid of config)
	sendCLICommand("play")
	displayCurrentArtistTitle()
end SB_rndIDM

on SB_rndElectronica()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Electronica")
	displayMsg(getText("RndElectronica"), osdMsgDurMid of config)
	sendCLICommand("play")
	displayCurrentArtistTitle()
end SB_rndElectronica

on SB_rndGoa()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Goa")
	displayMsg(getText("RndGoa"), osdMsgDurMid of config)
	sendCLICommand("play")
	displayCurrentArtistTitle()
end SB_rndGoa

on SB_rndIndie()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Indie")
	displayMsg(getText("RndIndie"), osdMsgDurMid of config)
	sendCLICommand("play")
	displayCurrentArtistTitle()
end SB_rndIndie

on SB_rndClassical()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=Classical")
	displayMsg(getText("RndClassical"), osdMsgDurMid of config)
	sendCLICommand("play")
	displayCurrentArtistTitle()
end SB_rndClassical

on SB_rndPostRock()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist loadtracks genre.namesearch=%22Post%20Rock%22")
	displayMsg(getText("RndPostRock"), osdMsgDurMid of config)
	sendCLICommand("play")
	displayCurrentArtistTitle()
end SB_rndPostRock

on SB_Radio_RadioRSG()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist play __playlists/RadioRSG")
	displayMsg(getText("Radio_RadioRSG"), osdMsgDurMid of config)
	sendCLICommand("play")
	--displayCurrentArtistTitle()
end SB_Radio_RadioRSG

on SB_Radio_1Live()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist play __playlists/1Live")
	displayMsg(getText("Radio_1Live"), osdMsgDurMid of config)
	sendCLICommand("play")
	--displayCurrentArtistTitle()
end SB_Radio_1Live

on SB_Radio_WDR2()
	_prepare()
	sendCLICommand("playlist clear")
	sendCLICommand("playlist play __playlists/WDR2BergischesLand")
	displayMsg(getText("Radio_WDR2"), osdMsgDurMid of config)
	sendCLICommand("play")
	--displayCurrentArtistTitle()
end SB_Radio_WDR2

on SB_TimeChange(step)
	if step > 0 then
		sendCLICommand("time +" & step)
		set str to getText("TimeForward")
	else
		sendCLICommand("time " & step)
		set str to getText("TimeBackward")
	end if
	set str to str & " " & step & " " & getText("Seconds")
	displayMsg(str, osdMsgDurMid of config)
end SB_TimeChange

on SB_TimeForwardSmall()
	_prepare()
	set step to timeStepSmall of config
	SB_TimeChange(step)
end SB_TimeForwardSmall

on SB_TimeForwardMid()
	_prepare()
	set step to timeStepMid of config
	SB_TimeChange(step)
end SB_TimeForwardMid

on SB_TimeForwardLarge()
	_prepare()
	set step to timeStepLarge of config
	SB_TimeChange(step)
end SB_TimeForwardLarge

on SB_TimeBackwardSmall()
	_prepare()
	set step to (timeStepSmall of config) * -1
	SB_TimeChange(step)
end SB_TimeBackwardSmall

on SB_TimeBackwardMid()
	_prepare()
	set step to (timeStepMid of config) * -1
	SB_TimeChange(step)
end SB_TimeBackwardMid

on SB_TimeBackwardLarge()
	_prepare()
	set step to (timeStepLarge of config) * -1
	SB_TimeChange(step)
end SB_TimeBackwardLarge

on SB_SayArtistTitle()
	_prepare()
	set json to getJSON()
	set artist to filterJSON("artist", json)
	set title to filterJSON("title", json)
	displayMsg(getText("NowPlaying"), osdMsgDurMid of config)
	sayMsg(getText("SayNowPlaying"))
	set artistSay to replace_say_chars(artist)
	displayMsg(artist, osdMsgDurLong of config)
	sayMsg(artistSay)
	displayMsg(artist & " - " & title, osdMsgDurLong of config)
	set titleSay to replace_say_chars(title)
	sayMsg(titleSay)
end SB_SayArtistTitle

-- ******* EXTERNAL ROUTINES *******
-- ********************************

-- ********************************
-- ******* COMMON ROUTINES *******

on replace_say_chars(the_text)
	set res to replace_chars(the_text, ".", " ")
	set res to replace_chars(res, "&", "and")
	return res
end replace_say_chars

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

-- ******* COMMON ROUTINES *******
-- ********************************

Ich habe den verkomplizierenden Schritt über eine externe config-Datei entfernt, die Möglichkeit geschaffen, den SB-Player zu setzen und es endlich geschafft, auf AS-Records via variablen Strings (via http://latenightsw.com/freeware/RecordTools/index.html) zuzugreifen.

Pill (oder wer anders), hast Du (Ihr) noch andere Fragen, die mich dazu inspirieren, den Code umzubauen? ;)
 
Zuletzt bearbeitet: