Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript to start a playlist at a certain time of day on certain days

I am wanting to create an Applescript that will start a playlist at 7 AM and end at 7 PM Monday - Friday and start another playlist that runs from 7PM - 7AM Monday - Friday and All day on Sat and Sun. Is this even possible? I would also like to set it and forget about it. In the event of a power outage, I would also like it to start the script back up automatically. Is this possible?

Posted on Dec 19, 2016 2:15 PM

Reply
Question marked as Top-ranking reply

Posted on Dec 20, 2016 6:44 AM

Your requests can be broken into two separate components - there's the 'do something' part - start a playlist; stop a playlist, etc. - and a scheduling component.


The 'do something' here is pretty trivial and could look as simple as:


tell application "iTunes"

play playlist "90’s Music"

end tell


Just substitute the name of the playlist you want to play.


The trickier part is the scheduling - getting it to run at the set time. There is nothing built-in to AppleScript to do this.

You have two basic options - simple and complex.


The simple options is to use some other application to trigger the script. An obvious choice here would be Calendar.app which has the ability to run an AppleScript for any given event on the calendar. Just create a repeating event at 7am every weekday and link your morning 'Play' AppleScript, and another event at 7pm every day to your evening AppleScript.


That's the simple thing, although it doesn't meet your 'auto-restart' condition - if the power goes out, nothing would happen until the next calendar event (either 7pm or 7am, as appropriate).


A slightly more complex implementation could be setup to run at startup and stay alive in the background, periodically checking the time to work out what playlist should be active. Like:


use AppleScriptversion "2.4" -- Yosemite (10.10) or later

use scripting additions



property dayList : "Daytime Music"

property nightList : "Evening Tunes"


on idle {}

my doIt()


-- check again on the hour:

return (3600 - ((get time of (get current date)) mod 3600))

end idle


on getCurrentPlaylist()


-- assume it's evening/weekend

set currentPlaylist to nightList

set curTime to (get current date)

set hr to (time of curTime) div hours

if hr ≥ 7 and hr < 19 then

if (weekday of curTime as integer) - 1 ≤ 5 then

set currentPlaylist to dayList

end if

end if

return currentPlaylist

end getCurrentPlaylist


on run

my doIt()

end run


on doIt()

set pl to my getCurrentPlaylist()

tell application "iTunes"


playplaylistpl

end tell

end doIt


As you can see, this is much more complex, although 90% of the code is in working out the current date/time and establishing which playlist is the right one. The good thing about this is that if you save it as a Stay Open application and add it to your login items, it will run as soon as you log in (which, of course, requires automatic login for your account, which is a whole other issue 😉 )

5 replies
Sort By: 
Question marked as Top-ranking reply

Dec 20, 2016 6:44 AM in response to athedrummaster

Your requests can be broken into two separate components - there's the 'do something' part - start a playlist; stop a playlist, etc. - and a scheduling component.


The 'do something' here is pretty trivial and could look as simple as:


tell application "iTunes"

play playlist "90’s Music"

end tell


Just substitute the name of the playlist you want to play.


The trickier part is the scheduling - getting it to run at the set time. There is nothing built-in to AppleScript to do this.

You have two basic options - simple and complex.


The simple options is to use some other application to trigger the script. An obvious choice here would be Calendar.app which has the ability to run an AppleScript for any given event on the calendar. Just create a repeating event at 7am every weekday and link your morning 'Play' AppleScript, and another event at 7pm every day to your evening AppleScript.


That's the simple thing, although it doesn't meet your 'auto-restart' condition - if the power goes out, nothing would happen until the next calendar event (either 7pm or 7am, as appropriate).


A slightly more complex implementation could be setup to run at startup and stay alive in the background, periodically checking the time to work out what playlist should be active. Like:


use AppleScriptversion "2.4" -- Yosemite (10.10) or later

use scripting additions



property dayList : "Daytime Music"

property nightList : "Evening Tunes"


on idle {}

my doIt()


-- check again on the hour:

return (3600 - ((get time of (get current date)) mod 3600))

end idle


on getCurrentPlaylist()


-- assume it's evening/weekend

set currentPlaylist to nightList

set curTime to (get current date)

set hr to (time of curTime) div hours

if hr ≥ 7 and hr < 19 then

if (weekday of curTime as integer) - 1 ≤ 5 then

set currentPlaylist to dayList

end if

end if

return currentPlaylist

end getCurrentPlaylist


on run

my doIt()

end run


on doIt()

set pl to my getCurrentPlaylist()

tell application "iTunes"


playplaylistpl

end tell

end doIt


As you can see, this is much more complex, although 90% of the code is in working out the current date/time and establishing which playlist is the right one. The good thing about this is that if you save it as a Stay Open application and add it to your login items, it will run as soon as you log in (which, of course, requires automatic login for your account, which is a whole other issue 😉 )

Reply

Dec 20, 2016 6:41 AM in response to Camelot

Thank you so much! I have a couple of questions about it. When I input my playlist and data, it seems that it is not checking the date and time and then changing the playlist accordingly. I am running directly from script editor and I am manually setting the time of my system clock to AM/PM. Does this need to go into the iTunes scripts folder? I have not been able to find that in my User library and am wondering if it is now called the iTunes Plug-ins folder.


Also, I have been reading about how to shuffle on dougs apple scripts and unfortunately his script for shuffle seems to not work in newer versions of iTunes. I was looking through Applescript documentation from Apple and I can't seem to find anything about shuffling a playlist. Any idea of how to do it without it mimicking the option click of the shuffle button?

Reply

Dec 20, 2016 7:02 AM in response to athedrummaster

tell application "System Events"  
  tell process "iTunes" to if exists then  
  click menu item "songs" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar 1  
  end if  
end tell

This worked to shuffle, but it seems to then break the time aspect. I inserted this bit of code as so:


on doIt()
     set pl to my getCurrentPlaylist()
     tell application "iTunes"
          tell application "System Events"
               tell process "iTunes" to if exists then
                    click menu item "songs" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar 1
                    click menu item "On" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar 1
               end if
          end tell
          play playlist pl
     end tell
end doIt
Reply

Dec 20, 2016 1:21 PM in response to athedrummaster

If you're running the script in Script Editor it doesn't matter where (or even if) the script is saved. There must be some other flaw (and I admit, I didn't test the code personally).

Try adding some debugging - even a simple 'display dialog' to show the value of some variables through the flow.


As for shuffling, shuffle enabled is the relevant property of the application, so you just need to tell the app whether you want it on or off:


set shuffle enabled to true

Reply

Dec 20, 2016 1:20 PM in response to Camelot

This is the final script that ended up working. I had to do some tweeking and get a second set of eyes on it, but this works flawlessly. Thanks so much for your help!

use AppleScriptversion "2.4" -- Yosemite (10.10) or later

use scripting additions


global currentList

property dayList : "Day"

property nightList : "Night"


on idle {}

set pl to my getPlayList()

if currentList is not equal to pl then

set currentList to my getPlayList()

my runiTunes()

end if


return (30)

end idle


on getPlayList()


-- assume it's evening/weekend

set pl to nightList

set curTime to (get current date)

set hr to (time of curTime) div hours

if hr ≥ 7 and hr < 19 then

if (weekday of curTime as integer) - 1 ≤ 5 then

set pl to dayList

end if

end if

return pl

if pl is not equal to currentList then

set currentList to pl

my runiTunes()

end if

end getPlayList


on setiTunesSettings()

tell application "System Events"

tell process "iTunes" to if exists then


clickmenu item "songs" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar 1


clickmenu item "On" of menu "Shuffle" of menu item "Shuffle" of menu "Controls" of menu bar 1


clickmenu item "All" of menu "repeat" of menu item "repeat" of menu "controls" of menu bar 1

end if

end tell

end setiTunesSettings


on run

set currentList to my getPlayList()

my runiTunes()

my setiTunesSettings()

end run


on runiTunes()

set pl to my getPlayList()

tell application "iTunes"


playplaylistpl

end tell

end runiTunes

Reply

Applescript to start a playlist at a certain time of day on certain days

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.