Last active
December 14, 2015 03:58
-
-
Save vladkorotnev/5024342 to your computer and use it in GitHub Desktop.
applescript to control and search itunes on admin's machine. example of background scripting of terminals.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
global adminer | |
global assum | |
global artiste | |
global oldcontenta | |
global foundTracks | |
-- vars for old trackname, old artist, old terminal screen, search result | |
to split(someText, delimiter) --split text | |
set AppleScript's text item delimiters to delimiter | |
set someText to someText's text items | |
set AppleScript's text item delimiters to {""} --> restore delimiters to default value | |
return someText | |
end split | |
on sendMineServ(txt) --tell terminal window | |
tell first window of application "Terminal" | |
if get custom title of selected tab is "Server Start.sh" then | |
try | |
do script (txt) in selected tab | |
on error | |
do script ("msg " & adminer & " Error sending message to Minecraft Server") in selected tab | |
end try | |
end if | |
end tell | |
end sendMineServ | |
on tweetSong() --nowplaying | |
tell application "iTunes" to set assum to get name of current track | |
tell application "iTunes" to set artiste to get artist of current track | |
sendMineServ("say * Now playing - " & assum & " by " & artiste & " *") | |
end tweetSong | |
set foundTracks to {} | |
tweetSong() --tell song | |
tell first window of application "Terminal" to set consoled to contents --remember the term | |
tell first window of application "Terminal" to set oldcontenta to contents | |
set adminer to "vladkorotnev" | |
-- 2013-03-02 21:17:24 [INFO] [Minecraft] <vladkorotnev> itplayta track/artist | |
repeat --forever loop | |
tell application "iTunes" to set assumneue to get name of current track | |
tell application "iTunes" to set artistneue to get artist of current track | |
if assumneue is not assum or artistneue is not artiste then tweetSong() --new track | |
tell first window of application "Terminal" to set consoled to contents --new terminal | |
if not (oldcontenta is consoled) then --it has changes | |
set tid to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to ASCII character 10 -- (a line feed) | |
set newTxt to (text items of consoled) -- not text of, text items of | |
set delta to item ((length of newTxt) - 2) of newTxt | |
set AppleScript's text item delimiters to tid | |
--let the parsing begin | |
ignoring case | |
-- just wanna know who's playing | |
if "[INFO] [Minecraft] <" & adminer & "> whatnow" is in delta then tweetSong() | |
-- play track with known artist | |
if "[INFO] [Minecraft] <" & adminer & "> itplayta" is in delta then | |
try | |
set cmdargs to last item of split(delta, "[INFO] [Minecraft] <" & adminer & "> itplayta ") | |
-- cmdargs now "Track/Artist" | |
set artista to last item of split(cmdargs, "/") | |
set albuma to first item of split(cmdargs, "/") | |
tell application "iTunes" | |
play (every track of library playlist 1 whose artist is artista and name is albuma) | |
-- is now playing! | |
end tell | |
on error | |
sendMineServ("msg " & adminer & " Error while looking for track") | |
sendMineServ("msg " & adminer & " Sure it exists?") | |
end try | |
end if | |
-- just the name is known | |
if "[INFO] [Minecraft] <" & adminer & "> itplay " is in delta then | |
try | |
set cmdargs to last item of split(delta, "[INFO] [Minecraft] <" & adminer & "> itplay ") | |
tell application "iTunes" | |
-- pass the args directly | |
play (every track of library playlist 1 whose name is cmdargs) | |
end tell | |
on error | |
sendMineServ("msg " & adminer & " Error while looking for track") | |
sendMineServ("msg " & adminer & " Sure it exists?") | |
end try | |
end if | |
-- help text | |
if "[INFO] [Minecraft] <" & adminer & "> itunes" is in delta then | |
sendMineServ("msg " & adminer & " * usage: itplay track *") | |
sendMineServ("msg " & adminer & " * usage: itplayta track/artist *") | |
sendMineServ("msg " & adminer & " * usage: itsearch query *") | |
sendMineServ("msg " & adminer & " ** after search: itfound number **") | |
sendMineServ("msg " & adminer & " * case sensitive *") | |
end if | |
-- search | |
if "[INFO] [Minecraft] <" & adminer & "> itsearch" is in delta then | |
try | |
set cmdargs to last item of split(delta, "[INFO] [Minecraft] <" & adminer & "> itsearch ") | |
-- search for it | |
set foundTracks to {} | |
sendMineServ("msg " & adminer & " Found iTunes items:") | |
tell application "iTunes" | |
repeat with t in (search library playlist 1 for cmdargs) | |
try | |
set end of foundTracks to ((name of t as string) & "/" & (artist of t as string)) | |
end try | |
end repeat | |
end tell | |
set i to 1 | |
repeat with a in foundTracks | |
sendMineServ("msg " & adminer & " [" & (i as string) & "] " & a) | |
set i to (i + 1) | |
delay 0.4 | |
end repeat | |
on error | |
sendMineServ("msg " & adminer & " Error while searching for items") | |
end try | |
end if | |
-- play found items | |
if "[INFO] [Minecraft] <" & adminer & "> itfound" is in delta then | |
try | |
set cmdargs to last item of split(delta, "[INFO] [Minecraft] <" & adminer & "> itfound ") as number | |
set aral to item cmdargs of foundTracks | |
set artista to last item of split(aral, "/") | |
set albuma to first item of split(aral, "/") | |
tell application "iTunes" | |
tell application "iTunes" | |
play (every track of library playlist 1 whose artist is artista and name is albuma) | |
-- is now playing! | |
end tell | |
end tell | |
on error | |
sendMineServ("msg " & adminer & " Error while selecting item") | |
sendMineServ("msg " & adminer & " Sure that selection exists?") | |
end try | |
end if | |
end ignoring | |
-- remember terminal | |
tell first window of application "Terminal" to set consoled to contents | |
tell first window of application "Terminal" to set oldcontenta to contents | |
end if | |
delay 1 | |
end repeat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment