Monday, January 23, 2017

#26 2016-12-11 10:05:25 am

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

When I booted from my HD running 10.9.5, I got the error -50.
When I booted from my HD running 10.10.5, I got the error -50.

In both cases, the 5th playList named "Vidéos personnelles" in French was empty.
On my daily used HD, the same playlist contain two items which make the difference.

this is what’s creepy: the "Now Playing" playlist was definitely populated.

But something was making iTunes behave like it could not play it.

Both yourself and Mr Garvey hit upon the “empty playlist” point, like, iTunes was convinced there was nothing there it could play... That is why I started searching for Any Other Explanation, settling on my ersatz “change everything to a music video and make sure shuffle is off...”

I am not satisfied with my explanation; i cannot find anything that confirms or denies if iTunes treats playing a “TV Show” differently from playing a “Music Video.” But... my silly solution works for some reason: everything plays now. (Of course, that’s the sort of “I have no idea what I”m doing” solution that leads to sloppy code...)

I will keep investigating, and I will keep you posted.

—Æ

Last edited by aechadwick (2016-12-11 10:15:55 am)

Offline

 

#27 2016-12-13 03:49:38 pm

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

I let the Dayparting script run Sunday and Monday and it worked great! with only a couple caveats...

Sometimes, an event would come up “” leaving the thePlaylistName empty… the was easily addressed but changing the Alert trigger to “1 minute after” to make sure the script was well within the event. I also added a failsafe by wrapping the get Event name block in a try and adding a Dead Air contingency.

In the interest of possible event collision (since the script just starts playlists and walks away), I added a quick “if already playing, do a fade-and-switch” thing.

My next step is make the script “universal,” able to play Videos or Music-with-Visualizer, depending on playlist type. Right now, it checks the name of the playlist for a musical note character, which is effective but inelegant. The Music-option simply plays through a playlist while showing the visualizer... I would like to make that more interesting, like, by continuing the fake-Station-ID gag, and maybe on-the-fly visualizer setting changes...

Here is the current 'Script, incorporating better variable names as suggested by Mr Koenig. Also, a couple screenshots of the iTunes and iCal setup, for fun and reference.

—Æ

Applescript:

try
   set thePlaylistName 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 thePlaylistName to "♫ Dead Air" -- event blank for some reason? just play music
end try

set theFirstCharacter to first character of thePlaylistName -- it’d be sexier if script could simply determine if Playlist has Video or Not...

tell application "iTunes"
   
   if theFirstCharacter is not "♫" then -- it’s not music, shuffle video with station IDs...
       
       --Shuffle based on “CK's Evenly-shuffled Playlists v0.6” by Charles Kelly http://www.manythings.org/mac/
       set playlistMergedName to "Now Playing"
       set PlaylistInterstitialsName to "[ID-D]" -- Intersitials for the [D]aytime broadcast
       
       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 MergedTracks to {}
       repeat with i from 1 to (count of tracks of playlist thePlaylistName) -- length based on requested Show
           set theTrack to some track of playlist PlaylistInterstitialsName
           set enabled of (duplicate theTrack to playlistMergedList) to true
           set MergedTracks to MergedTracks & (database ID of theTrack)
           set theTrack to track i of playlist thePlaylistName
           set enabled of (duplicate theTrack to playlistMergedList) to true
           set MergedTracks to MergedTracks & (database ID of theTrack)
       end repeat
       
   else
       set playlistMergedList to user playlist thePlaylistName -- it's just music… -- still, be fun to have station ID, like, every fifth song…
       
   end if
   
   activate
   
   if player state is playing then
       set theVolume to sound volume
       my FadeOut(5, 0.5)
       stop
       set sound volume to theVolume
   end if
   
   set visible of browser window 1 to true
   set view of browser window 1 to playlistMergedList
   
   tell application "System Events" to tell process "iTunes" to set value of attribute "AXFullScreen" of window 1 to true
   
   play playlistMergedList
   if theFirstCharacter is "♫" then set visuals enabled to true -- it's music, so play the visualizer
   
end tell

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

iTunes...
http://i.imgur.com/k1JRzsM.png

iCal...
http://i.imgur.com/ruRwFBN.png

Offline

 

#28 2016-12-13 05:57:34 pm

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

i have devised...

Applescript:

   if (every file track of user playlist thePlaylistName whose video kind is music video) ≠ {} then

making the start of the script

Applescript:

try
   set thePlaylistName 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 thePlaylistName to "Dead Air" -- event blank for some reason? just play music
end try

tell application "iTunes"
   
   if (every file track of user playlist thePlaylistName whose video kind is music video) ≠ {} then
       -- it’s not music, shuffle video with station IDs...
       
       --Shuffle based on “CK's Evenly-shuffled Playlists v0.6” by Charles Kelly http://www.manythings.org/mac/

...which works. I expect there is a more elegant solution; I will keep experimenting.

—Æ

Offline

 

#29 2016-12-14 02:20:16 pm

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

This latest iteration will determine if the source playlist is Music or Video—Video playlists get an “ interstitial” between each video; Music gets an “interstitial” every few songs, the rest of the time the iTunes Visualizer plays!

I’ve played with this all morning, and it works really well... except the transitions are uggggggggly.

Within a playlist, video-to-video works fine...

However, between “shows” there is a flash of the iTunes Browser Window, when it builds and starts to play—gross!

and the switch from “interstitial” back to visualizer always involves a flash of playlist window—gross!!!

i will now experiment with someway to circumvent or minimize that.

in the meantime, see what you think!

—Æ

Applescript:

try
   set thePlaylistName 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 thePlaylistName to "Dead Air" -- event blank for some reason? just play music
end try

tell application "iTunes"
   
   set playlistMergedName to "Now Playing"
   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
   set MergedTracks to {}
   
   if (every file track of user playlist thePlaylistName whose video kind is music video) ≠ {} then -- it’s not music
       
       --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 MergedTracks to MergedTracks & (database ID of theTrack)
           set theTrack to track i of playlist thePlaylistName
           set enabled of (duplicate theTrack to playlistMergedList) to true
           set MergedTracks to MergedTracks & (database ID of theTrack)
       end repeat
       
   else -- it's music
       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 MergedTracks to MergedTracks & (database ID of theTrack)
           
           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
                   set MergedTracks to MergedTracks & (database ID of theTrack)
               on error
                   -- must be out of numbers so do nothing
               end try
           end repeat
           set i to i + theNumberOfSongs + 1
       end repeat
       
   end if
   
   activate
   
   if player state is playing then
       set theVolume to sound volume
       my FadeOut(5, 0.5)
       pause
       set sound volume to theVolume
   end if
   
   set visible of browser window 1 to true
   
   tell application "System Events" to tell process "iTunes" to set value of attribute "AXFullScreen" of window 1 to true
   
   set visuals enabled to true -- make sure iTunes Visualizer options are set to “Play Videos”
   
   play playlistMergedList
   
end tell

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

Offline

 

#30 2017-01-03 05:16:38 pm

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

Hola, amigos. I know it's been a long time since I rapped at ya…

This up-to-the-minute version of my "dayparting" script performs a few different actions, based on the name of the iCal event; these include having QuickTime start or stop a video "test pattern," as well as the original "generate a shuffled playlist” that started the project. There is also an event logging process that I have been using to keep track of functions.

This project was functional in time for christmas; i spent the time between christmas and new year’s eve playing with different functions and troubleshooting the existing processes.

This current version has been running for a a week with mostly good results. Sometimes events get called a little wrong, maybe QuickTime fails to start, maybe the value of attribute “AXFullScreen" is a freakin’ mystery… and there’s occasional flashes of iTunes between videos (ugh)… but it queues up The Muppet Show each day, and plays Cartoons to great effect.

(to be clear, I know this script is of ludicrously narrow function… i merely hope it inspires some unobvious solution in anyone else out there. Also, i just appreciate other eyes perusing my ersatz code— let me know what you think!)

Have fun!

Applescript:

if application "QuickTime Player" is running then StopQuicktime()

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 not "◼︎" and theEventName is not "Dead Air" then set ComingUp to GetNextEvent(theEventName)

if TheEventType is "◼︎" then set TheEventType to SignOff() -- stop playing things

if TheEventType is "▶" then set TheEventType to TestPattern() -- get ready to play things

if TheEventType is not "" then NowPlaying(theEventName) -- play things

LogThisAction("[Event “" & theEventName & "” Executed]")
LogThisAction("[up next “" & ComingUp & "”]")

on NowPlaying(thePlaylistName)
   tell application "iTunes"
       activate
       
       if player state is playing then
           set theVolume to sound volume
           my FadeOut(5, 0.5)
           pause
           set sound volume to theVolume
           my LogThisAction("paused iTunes to switch playlists")
       end if
       
       set playlistMergedName to "Now Playing"
       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
       set MergedTracks to {}
       
       if (every file track of user playlist thePlaylistName whose video kind is music video) ≠ {} then -- it’s not music
           
           --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 MergedTracks to MergedTracks & (database ID of theTrack)
               set theTrack to track i of playlist thePlaylistName
               set enabled of (duplicate theTrack to playlistMergedList) to true
               set MergedTracks to MergedTracks & (database ID of theTrack)
           end repeat
           
       else -- it's music
           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 MergedTracks to MergedTracks & (database ID of theTrack)
               
               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
                       set MergedTracks to MergedTracks & (database ID of theTrack)
                   on error
                       -- must be out of numbers so do nothing
                   end try
               end repeat
               set i to i + theNumberOfSongs + 1
           end repeat
           
       end if
       
       try -- this sequence of try/error statements annoys me
           set visible of browser window 1 to true
       on error
           my LogThisAction("problem setting visible of broswer window…")
       end try
       
       try
           set view of browser window 1 to playlistMergedList
       on error
           my LogThisAction("problem setting view of browser window…")
       end try
       
       -- tell application "System Events" to tell process "iTunes" to set value of attribute "AXFullScreen" of window 1 to true
       set isFullScreen to false
       tell application "System Events" to tell process "iTunes" to set isFullScreen to value of attribute "AXFullScreen" of window 1
       if isFullScreen is false then
           tell application "System Events" to keystroke "f" using {command down, control down}
           delay 0.5
           my LogThisAction("(enabled fullscreen)")
       else
           my LogThisAction("(already fullscreen)")
       end if
       
       if visuals enabled is false then set visuals enabled to true -- make sure iTunes Visualizer options are set to “Play Videos”
       
       play playlistMergedList
       
       my LogThisAction("Now Playing " & thePlaylistName)
       my LogThisAction("Starting with " & (name of track 2 of playlistMergedList))
       
       tell application "Chiller" to run -- background app will wait until playlist finishes, then starts a “Filler” and quits itself.
       
   end tell
end NowPlaying

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 LogThisAction(themessage)
   set theLogFile to "TV-" & (do shell script "date +'%d%b%Y'" as string) & ".log" -- make file named "TV-DayMonthYear.log"
   set theLine to (do shell script "date +'%a %d-%b-%Y %H:%M'" as string) & " '" & themessage & "'" -- quoted $message to avoid trouble characters
   do shell script "echo " & theLine & " >> ~/Documents/" & theLogFile
end LogThisAction

to StopQuicktime()
   tell application "QuickTime Player"
       set theLogEventModifer to ""
       activate
       delay 0.5
       try
           set presenting of the front document to false
       on error
           my LogThisAction("problem turning off “presenting”…")
           set theLogEventModifer to " maybe"
       end try
       delay 0.5
       try
           stop the front document
       on error
           my LogThisAction("problem stopping Movie…")
           set theLogEventModifer to " maybe"
       end try
       delay 0.5
       try
           close the front document
       on error
           my LogThisAction("problem closing Movie…")
           set theLogEventModifer to " maybe"
       end try
       quit
       my LogThisAction("Stopped Quicktime" & theLogEventModifer)
   end tell
end StopQuicktime

to SignOff()
   tell application "iTunes"
       set theLogEventModifer to ""
       activate
       delay 0.5
       if visuals enabled is true then set visuals enabled to false
       delay 0.5 -- all these sad delays because the script moves faster than the mini...
       tell application "System Events" to tell process "iTunes" to set value of attribute "AXFullScreen" of window 1 to false
       delay 0.5
       stop
       delay 0.5
       tell application "System Events" to keystroke "w" using command down
       my LogThisAction("Sign’d off iTunes" & theLogEventModifer)
   end tell
   return "" -- clear theEventType / skip to the end
end SignOff

to TestPattern()
   tell application "Finder" to set theTestPattern to some file of folder "Test Patterns" of folder "Movies" of home -- get random screen filler loop
   tell application "QuickTime Player"
       activate -- all these sad delays because the script moves faster than the mini...
       delay 0.5
       open theTestPattern
       delay 0.5
       set looping of the front document to true
       delay 0.5
       set presenting of the front document to true
       delay 0.5
       play the front document
       my LogThisAction("played test pattern " & theTestPattern)
   end tell
   return "" -- clear theEventType / skip to the end
end TestPattern

on GetNextEvent(theEventName)
   set theEventList to paragraphs of (do shell script "/usr/local/bin/icalbuddy -npn -nc -iep \"title\" -b \"\" eventsToday") -- via iCalBuddy hasseg.org/icalBuddy
   set NextEvent to ""
   repeat with i from 1 to count of theEventList
       if item i of theEventList is theEventName then set NextEvent to (item (i + 1) of theEventList)
   end repeat
   if the first character of NextEvent is "◼︎" then set NextEvent to "Tune in Tomorrow!"
   return NextEvent
end GetNextEvent

here is the current schedule...

http://i.imgur.com/FEHtdjg.png

and here it is in action.

http://i.imgur.com/QYDcfhu.jpg

Offline

 

#31 2017-01-03 05:22:50 pm

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

oh! and here is “Chiller.app,” the backgrounder called to wait out a playlist...

(the name is simply a play on “filler,” the official term for things inserted to finish a time slot)

Applescript:

on idle
   set thePlaylistName to "Chiller (Space)"
   tell application "iTunes"
       
       activate
       
       --set visible of browser window 1 to true
       --tell application "System Events" to tell process "iTunes" to set value of attribute "AXFullScreen" of window 1 to true
       try
           if player state is not stopped then return 5
       on error
           return 5
       end try
       
       play playlist thePlaylistName
       
       tell me to quit
       
   end tell
   return 5
end idle

on quit
   continue quit
end quit

Offline

 

#32 2017-01-09 11:52:54 am

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

trial and error has lead to some massaging and some streamlining. (honestly, it's like “Full Screen” is different from “Fullscreen” is different from “AXFullScreen,” or something, fit to drive a person crazy)

see what you think...

Applescript:


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

LogThisAction("[Event “" & theEventName & "” started]")

if application "QuickTime Player" is running then PleaseStopQuicktime()

if application "Chiller.app" is running then tell application "Chiller.app" to quit -- just in case

tell application "System Events" to set activeApp to name of first application process whose frontmost is true
if "iTunes" is in activeApp then
   tell application "iTunes" to if visuals enabled is true then tell application "System Events" to keystroke "t" using {command down} -- set visuals enabled to false
   PleaseStopiTunes()
   toggleFullScreen("off")
end if

set TheEventType to the first character of theEventName -- possible characters include ★ ♥ ▶ ◼︎ ✱ ♫

if TheEventType is "◼︎" then
   set TheEventType to SignOff() -- stop playing things
   LogThisAction("[Broadcast Concluded]")
end if

if TheEventType is "▶" then
   set TheEventType to TestPattern() -- get ready to play things
   LogThisAction("[Broadcasting Test Pattern]")
end if

if TheEventType is not "" then
   NowPlaying(theEventName) -- play things
   tell application "Chiller.app" to run -- background app will wait until playlist finishes, then starts a “Filler” and quits itself.
   LogThisAction("[Event “" & theEventName & "” is broadcasting…]")
end if

-- that’s it --

--now the handlers...--

on NowPlaying(thePlaylistName)
   tell application "iTunes"
       activate
       
       my PleaseStopiTunes()
       
       set playlistMergedName to "Now Playing"
       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
       
       -- make the playlist…
       
       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
       set MergedTracks to {}
       
       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 MergedTracks to MergedTracks & (database ID of theTrack)
               set theTrack to track i of playlist thePlaylistName
               set enabled of (duplicate theTrack to playlistMergedList) to true
               set MergedTracks to MergedTracks & (database ID of theTrack)
           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 MergedTracks to MergedTracks & (database ID of theTrack)
               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
                       set MergedTracks to MergedTracks & (database ID of theTrack)
                   on error
                       -- must be out of numbers so do nothing
                   end try
               end repeat
               set i to i + theNumberOfSongs + 1
           end repeat
           LogThisAction("Playlist Shuffled…")
       end if
       
       -- now make it play...
       
       delay 0.25 -- all these delays because script moves faster than computer…
       try -- this sequence of try/error statements annoys me…
           set visible of browser window 1 to true
       on error
           my LogThisAction("problem with “set visible of browser window 1 to true”")
       end try
       
       delay 0.25
       try
           set view of browser window 1 to playlistMergedList
       on error
           my LogThisAction("problem with “set view of browser window 1 to playlistMergedList”")
       end try
       
       delay 0.25
       play playlistMergedList
       
       delay 0.25
       my toggleFullScreen("on")
       
       delay 0.25
       if visuals enabled is false then tell application "System Events" to keystroke "t" using {command down} -- set visuals enabled to true
       
       my LogThisAction("Now Playing " & thePlaylistName)
       
       my LogThisAction("Starting with " & (name of track 2 of playlistMergedList))
       
       delay 0.25
       
   end tell
end NowPlaying

to LogThisAction(themessage)
   set theLogFile to "TV-" & (do shell script "date +'%d%b%Y'" as string) & ".log" -- make file named "TV-DayMonthYear.log"
   set theLine to (do shell script "date +'%a %d-%b-%Y %H:%M'" as string) & " '" & themessage & "'" -- quoted $message to avoid trouble characters
   do shell script "echo " & theLine & " >> ~/Documents/" & theLogFile
end LogThisAction

to PleaseStopQuicktime()
   tell application "QuickTime Player"
       activate
       delay 0.25
       try
           set presenting of the front document to false
       on error
           -- do nothing
       end try
       delay 0.25
       try
           stop the front document
       on error
           -- do nothing
       end try
       delay 0.25
       try
           close the front document
       on error
           -- do nothing
       end try
       quit
       my LogThisAction("Stopped Quicktime")
   end tell
end PleaseStopQuicktime

to SignOff()
   tell application "iTunes"
       activate
       delay 0.25 -- all these sad delays because the script moves faster than the mini...
       my toggleFullScreen("off")
       delay 0.25
       stop
       delay 0.25
       tell application "iTunes" to close every playlist window -- alternative to -- tell application "System Events" to keystroke "w" using command down
       my LogThisAction("Sign’d off iTunes")
   end tell
   return "" -- clear theEventType / skip to the end
end SignOff

to TestPattern()
   tell application "Finder" to set theTestPattern to some file of folder "Test Patterns" of folder "Movies" of home -- get random screen filler loop
   tell application "QuickTime Player"
       activate -- all these sad delays because the script moves faster than the mini...
       delay 0.5
       open theTestPattern
       delay 0.5
       set looping of the front document to true
       delay 0.5
       set presenting of the front document to true
       delay 0.5
       play the front document
       my LogThisAction("played test pattern " & theTestPattern)
   end tell
   return "" -- clear theEventType, skip to the end Daisy
end TestPattern

to toggleFullScreen(theScreenShouldBe) -- ok, actually just say "off" or "on"
   tell application "iTunes"
       activate
       try
           tell application "System Events" to tell process "iTunes" to set isFullScreen to value of attribute "AXFullScreen" of window 1
       on error
           tell application "System Events" to key code 53 -- escape
           set isFullScreen to false
       end try
       if theScreenShouldBe is "on" and isFullScreen is false then tell application "System Events" to keystroke "f" using {command down, control down}
       --if theScreenShouldBe is "on" and isFullScreen is true then    do nothing    
       --if theScreenShouldBe is "off" and isFullScreen is false then do nothing
       if theScreenShouldBe is "off" and isFullScreen is true then tell application "System Events" to key code 53 -- escape
       my LogThisAction("Toggle’d AXFullScreen to " & theScreenShouldBe)
   end tell
end toggleFullScreen

to PleaseStopiTunes()
   tell application "iTunes"
       activate
       if player state is playing then
           set theVolume to sound volume
           
           --fade volume
           set {tick, thismany} to {5, 0.5}
           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
           
           --then stop player
           stop -- maybe pause?
           set sound volume to theVolume
           delay 0.25
           
           tell application "iTunes" to close every playlist window
           
           my LogThisAction("stopped iTunes…")
       end if
   end tell
end PleaseStopiTunes

Offline

 

#33 2017-01-09 12:40:18 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2743

Re: DIY Dayparting

Hello

This morning, in an other thread, I wrote that I was surprised when I discovered that the Finder is able to move or duplicate a file in a folder defined by a string while I assumed that an alias or a Finder reference was required.

This evening I discovered in your code a similar behavior.

When I saw that you urged QuickTime Player to  open theTestPattern after defining this variable by :
tell application "Finder" to set theTestPattern to some file of folder "Test Patterns" of folder "Movies" of home
which create the Finder reference :
document file "NaturePatterns01.jpg" of folder "Test Patterns" of folder "Movies" of folder "myHome" of folder "Users" of startup disk of application "Finder"

I was really surprised.
My old practice let me think that only Finder is able to work upon Finder references.
I tested and discovered that I was wrong.

According to my practice I would have coded :

Applescript:

tell application "Finder" to set theTestPattern to (some file of folder ((home as text) & "Movies:Test Patterns:")) as alias

but I'm glad, thanks to your message I learnt something.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) lundi 9 janvier 2017 19:39:50

Offline

 

#34 2017-01-09 04:22:06 pm

StefanK
Member
From: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11402
Website

Re: DIY Dayparting

Yvan,

I was expecting that you as a persuaded "Finder-phobic" would have suggested

Applescript:

tell application "System Events" to set theTestPattern to (some file of folder "Test Patterns" of movies folder) as alias

wink


regards

Stefan

Offline

 

#35 2017-01-09 08:52:50 pm

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

Mr Koenig, I must admit i acquired that code after exhausting several other avenues. After years of stumbling over "Finder" references, I was gobsmacked to just... be able to open some file...

and Mr K, i expect you're being sassy but I am myself very intrigued by that bit of code—i do love bossing "System Events" around.

—Æ

Offline

 

#36 2017-01-17 05:45:51 pm

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

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.

Applescript:


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...

Applescript:


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.

Last edited by aechadwick (2017-01-18 12:06:59 pm)

Offline

 

#37 2017-01-20 11:37:02 am

aechadwick
Member
Registered: 2006-07-27
Posts: 31

Re: DIY Dayparting

Well. Well, well. Well, well, well, well… Long time no viddy, droogs. Here’s how it goes.

Upon repeated trials, i isolated at least one problem: commands after "play" seem to make iTunes drop out of fullscreen (without actually turning fullscreen off, see prior, "driving me nuts").

Also, calls to outside apps will make iTunes drop out of fullscreen. That was obvious with Tell Application "Quicktime", but less obvious with tell application "Dayfiller.app" to run—it seriously didn’t even occur to me.

The little background app is currently required to catch the end of a playlist.

I edited Dayfiller.app’s .plist to ensure it starts-and-runs as “background only”:

Show Package Contents > Dayfiller.app/Contents/Info.plist

add the following (after the first <dict> and before the first <key> works fine...)

<key>LSBackgroundOnly</key>
<string>True</string>

so far, this works fine. I will post further modifications & updates as they appear.

—Æ

Last edited by aechadwick (2017-01-20 11:37:46 am)

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.797 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)