Here is a version is re-written to keep everything inside iTunes (Quicktime is no longer called up to loop video).
The practical goal was to keep the process invisible”the script can do all it’s work while iTunes plays, iTunes can do most of it’s functions behind the fullscreen, then just a barely noticeable switch to the playlist or track that is “up next” (no ugly flash of the Playlist video, just right into the next video… when it works).
The “shuffle” process finishes by placing a single “filler” track at the end of the playlist; the background app catches this, and sets it to loop until the next “show” is called up.
However, this script is NOT foolproof yet. mostly because of the vagaries of “full screen.”
Processes running in full screen no longer acknowledge calls to “window 1””officially, they seem to have NO windows. So that’s a problem. iTunes brings additional complications”it treats full screen like an ongoing status linked to playing that sort-of-but-not-really-still-exists when iTunes is stopped. For example, when iTunes stops, it will drop out of FullScreen display”however, FullScreen is not entirely off, just waiting for play to pop back into fullscreen. So… if a script tells something to switch to fullscreen when fullscreen is kind of already enabled it might pop out of fullscreen or it might play the video in an imaginary fullscreen that is inexplicably off to the side of the playlist without actually showing you the screen because it’s not really a window so it doesn’t exist in userspace. or something. Not to mention, this is compounded if you come out via a different order than you went in”play, then go fullscreen = exit fullscreen, then stop, or there will be hell to pay. maybe. honestly, it’s near to driving me nuts.
I welcome any insight, with the caveat: you will NOT encounter this simply telling iTunes to turn fullscreen on or off. The full weirdness only appears when Fullscreen starts to get, like, layered (ie., by this script).
If you’re still reading after all that. please take a look.
tell application "System Events" to set frontmost of process "iTunes" to true -- make sure iTunes is current app WITHOUT "activate" -- activate will break out of fullscreen? i think?
try
set theEventName to first paragraph of (do shell script "/usr/local/bin/icalbuddy -eed -nc -b \"\" eventsNow") -- Get Current Calendar Event via iCalBuddy hasseg.org/icalBuddy
on error
set theEventName to "Dead Air" -- problem? just play music
end try
set TheEventType to the first character of theEventName -- possible characters include ★ ♥ ▶ ◼︎ ✱ ♫
if TheEventType is "◼︎" then set TheEventType to SignOff() -- stop playing things
if TheEventType is "â–¶" then set TheEventType to TestPattern("[Filler]") -- play a loop while we wait to play things
-- future expansion: extract event after "â–¶" to play specific filler list
if TheEventType is not "" then NowPlaying(theEventName) -- play things
if TheEventType is not "" then tell application "Dayfiller" to run -- background app will wait until playlist finishes, then starts a "Filler" and quits itself.
to NowPlaying(thePlaylistName)
tell application "iTunes"
set playlistMergedName to "Up Next"
set thePlaylistNameWhenItStartsPlaying to "Now Playing"
set PlaylistFiller to "[Filler]" -- future expansion = different fillers by time of day, name of show...
set PlaylistInterstitialsName to "[ID-D]" -- intersitials for the [D]aytime broadcast
if ((hours of (current date)) > 21) or ((hours of (current date)) < 5) then set PlaylistInterstitialsName to "[ID-N]" --unless it's [N]ight
if (not (exists playlist playlistMergedName)) then
set playlistMergedList to make new user playlist with properties {name:playlistMergedName} -- if no playlist, make it
else
set playlistMergedList to user playlist playlistMergedName -- if playlist, clean it out
delete tracks of playlistMergedList
end if
set theTrackCount to (count of tracks of playlist thePlaylistName) -- length based on requested Show
if (every file track of user playlist thePlaylistName whose video kind is music video) ≠{} then -- it's not music, put ID between tracks
--Shuffle based on "CK's Evenly-shuffled Playlists v0.6" by Charles Kelly http://www.manythings.org/mac/
repeat with i from 1 to theTrackCount
set theTrack to some track of playlist PlaylistInterstitialsName
set enabled of (duplicate theTrack to playlistMergedList) to true
set theTrack to track i of playlist thePlaylistName
set enabled of (duplicate theTrack to playlistMergedList) to true
end repeat
else -- it is music, put ID every few tracks
set i to 1
repeat while i < theTrackCount
set theTrack to some track of playlist PlaylistInterstitialsName
set enabled of (duplicate theTrack to playlistMergedList) to true
set theNumberOfSongs to (random number from 3 to 7)
repeat with aNumber from 0 to theNumberOfSongs
try
set theTrack to track (i + aNumber) of playlist thePlaylistName
set enabled of (duplicate theTrack to playlistMergedList) to true
on error
-- must be out of numbers so do nothing
end try
end repeat
set i to i + theNumberOfSongs + 1
end repeat
end if
--finish by adding a single "filler" track; when the Dayfiller.app sees this, it will set it to loop.
set theTrack to some track of playlist PlaylistFiller
set enabled of (duplicate theTrack to playlistMergedList) to true
-- now make it play...
my ShuffleAndRepeat("Off")
set theVolume to sound volume
if player state is playing then my FadeOut(5, 0.5) -- iTunes already doing stuff
-- if the player state is stopped then -- iTunes was not doing stuff. so confirm windows exist
-- activate -- except commented out because window 1 is nothing but trouble at this point
-- tell application "System Events"
-- if exists (window 1 of process "iTunes") then
-- tell application "iTunes"
-- delay 0.25
-- set visible of browser window 1 to true
-- delay 0.25
-- set view of browser window 1 to playlistMergedList
-- end tell
-- end if
-- end tell
-- end if
--this order: Play, Visuals True, Fullscreen True
delay 0.25
play playlistMergedList -- play "Up Next"
set sound volume to theVolume -- restore volume if required
delay 0.25
my ToggleVisualizer(true)
delay 0.25
my toggleFullScreen(true)
if (exists playlist thePlaylistNameWhenItStartsPlaying) then delete playlist thePlaylistNameWhenItStartsPlaying -- get rid of old "Now Playing"
tell playlistMergedList to set name to thePlaylistNameWhenItStartsPlaying -- change "Up Next" to "Now Playing"
end tell
end NowPlaying
to SignOff()
tell application "iTunes"
--this order: Fullscreen False, Visuals False, Stop
set theVolume to sound volume
if player state is playing then my FadeOut(5, 0.5)
delay 0.25
my toggleFullScreen(false)
delay 0.25
my ToggleVisualizer(false)
delay 0.25
stop
set sound volume to theVolume
end tell
delay 0.25
tell application "System Events" to if exists (window 1 of process "iTunes") then tell application "iTunes" to close every window
return "" -- clear theEventType, skip to the end
end SignOff
to TestPattern(thePlaylistName)
tell application "iTunes"
set theVolume to sound volume
if player state is playing then my FadeOut(5, 0.5)
my ShuffleAndRepeat("On")
--this order: Play, Visuals True, Fullscreen True
play playlist thePlaylistName
set sound volume to theVolume
my toggleFullScreen(true)
end tell
return "" -- clear theEventType, skip to the end Daisy
end TestPattern
to toggleFullScreen(theScreenShouldBe) -- true or false
-- based on http://stackoverflow.com/questions/8215501/applescript-use-lion-fullscreen --
set countAllWindows to count (windows of application "iTunes" whose visible is true)
tell application "System Events" to set countProcessWindows to count windows of process "iTunes"
if countAllWindows is not countProcessWindows then
set theFullScreenIs to true
else
set theFullScreenIs to false
tell application "System Events"
set frontmost of process "iTunes" to true
tell process "iTunes"
tell front window
set theFullScreenIs to get value of attribute "AXFullScreen"
end tell
end tell
end tell
end if
tell application "iTunes"
if theScreenShouldBe is not theFullScreenIs then tell application "System Events" to keystroke "f" using {command down, control down}
end tell
end toggleFullScreen
to ToggleVisualizer(theVisualizerShouldBe) -- true or false
tell application "iTunes"
set theVisualizerIs to {visuals enabled}
try
if theVisualizerIs is not theVisualizerShouldBe then set visuals enabled to theVisualizerShouldBe
on error
--i have no idea, just sometimes it doesn't work
end try
end tell
end ToggleVisualizer
to FadeOut(tick, thismany)
tell application "iTunes"
repeat
if sound volume is less than or equal to tick then
set sound volume to 0
exit repeat
end if
set sound volume to ((sound volume) - tick)
delay thismany
end repeat
end tell
end FadeOut
to ShuffleAndRepeat(OnOrOff)
set {theShuffle, theRepeat} to {"Off", "Off"} -- i don't like GUI-scripting, but this is the only way that worked...
if OnOrOff = "On" then set {theShuffle, theRepeat} to {"On", "One"}
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Shuffle"'s menu 1 whose name ends with theShuffle)
tell application "System Events" to tell process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Repeat"'s menu 1 to perform action "AXPress" of menu item theRepeat
end ShuffleAndRepeat
and here is the background app that waits to catch the Filler track. it mostly kind of works…
on idle
set theTrackGenre to ""
tell application "iTunes" to if player state is playing then set theTrackGenre to (genre of current track as string)
if theTrackGenre is "FILLER" then
tell application "System Events" to tell process "iTunes"'s menu bar 1's menu bar item "Controls"'s menu 1's menu item "Repeat"'s menu 1 to perform action "AXPress" of menu item "One"
tell me to quit
else
return 5
end if
end idle
on quit
continue quit
end quit
Edit: okay, step one”remove quotes from true&false so they’re actually booleans not strings. HAHAHAHA um yeah.