Hi, I have to write a short presentation about AppleScript to present to my class in High school. I was wondering weather it was possible for someone to write a AppleScript for opening Keynote then get keynote to play a presentation and then for the keynote to say something eg; hello Class by (your name?). I only want to show a AppleScript to demo it as everybody in my school seems to be against macs and I want to show them the power they have using AppleScript.
tell application "Keynote"
activate
open "path:to:my:presentation"
start slideshow
delay 1
repeat until playing of slideshow 1 is false
delay 2
end repeat
say "Howdy"
end tell
Thanks for your help, its only today that I discovered about AppleScript, and I think that it is awsome. I am starting to learn the basics, (in school we are taught VB, and I find it sooooo difficult to understand, but the AppleScript is much easier to understand I find!) But thanks once again for you help!
Tom, If you really want to impress the folks (convert them to Macs) ya might come up with something a tad more intersting to the age group than automating Keynote. Maybe some sort of iTunes automation perhaps.
Well here is a quick example I came up with. It generates a random playlist of ten songs based on a genre the user specifies. The code isnt the cleanest in the world, and it could stand for sme error checking, but it should help (hopefully) to portray the power of Applescript. So the next time your hanging with friends and tired of listening to the same old playlists just generate one on the fly!
tell application "iTunes"
repeat
try
display dialog "Please enter a genre" default answer ""
set playlist_genre to text returned of result
set track_list to every track of view of window 1 whose genre contains playlist_genre
exit repeat
on error
display dialog "I'm sorry there were no tracks whose genre's contained \"" & playlist_genre & "\". Please try again."
end try
end repeat
make new playlist with properties {name:playlist_genre}
repeat with i from 1 to 10
set track_number to random number from 1 to (count of track_list)
set track_location to location of item track_number of track_list
add track_location to user playlist playlist_genre
end repeat
end tell
if the genre doesn´t exist, it comes up with a error message:
žitem 1 of {}" can´t be read.
I have added try:
tell application "iTunes"
repeat
try
display dialog "Please enter a genre" default answer ""
set playlist_genre to text returned of result
set track_list to every track of view of window 1 whose genre contains playlist_genre
exit repeat
on error
display dialog "I'm sorry there were no tracks whose genre's contained \"" & playlist_genre & "\". Please try again."
end try
end repeat
make new playlist with properties {name:playlist_genre}
repeat with i from 1 to 10
try
set track_number to random number from 1 to (count of track_list)
set track_location to location of item track_number of track_list
add track_location to user playlist playlist_genre
end try
end repeat
end tell
tell application "iTunes"
repeat
display dialog "Please enter a genre" default answer ""
set playlist_genre to text returned of result
set track_list to every track of view of window 1 whose genre contains playlist_genre
if track_list = {} then
display dialog "I'm sorry there were no tracks whose genre's containes: " & playlist_genre & return & return & "Do you want to run the script again?" & return
end if
if not track_list = {} then
make new playlist with properties {name:playlist_genre}
repeat with i from 1 to 10
set track_number to random number from 1 to (count of track_list)
set track_location to location of item track_number of track_list
add track_location to user playlist playlist_genre
end repeat
end if
end repeat
end tell
Hi here is an evolved version.
adds _RP to end of the play list to make sure other playlist are not touched
Displays a List to choose from of Genre’s from the tracks in the library.
If Playlist already exists it removes the previous tracks.
Tries to Check to make sure duplicates are not added. (may need some work )
Offers to the choice of exit, play playlist , or run again
global playlist_genre_rand -- global makes this variable globally available
my makeMe() -- this calls the subroutine makeMe() in this case the first part of the script
-- subroutines are used for better control over the script
on makeMe()
-- get genre
tell application "iTunes"
-- looks for tracks with genre
set genreLIst to {} -- a place holder for a list
set gen_list to genre of file tracks of library playlist 1 -- using file tracks looks for files (excludes Radio which are Url tracks) -- this line will also carry out the action of looking for genre of tracks and place the results in gen_list
repeat with i from 1 to number of items in gen_list -- counts the number of items held in gen_list
set this_item to item i of gen_list -- sets this_item to to item (i) numeber 1,2,3 et..) in gen_list
if this_item is not in genreLIst then -- if item (what is NOW this_item ) is not in the list genreLIst
if this_item is not "" then -- and if item (what is NOW this_item ) is not blank (nothing) then
copy this_item to end of genreLIst -- copy this_item to end of the list genreLIst
end if
end if
end repeat
if not genreLIst = {} then -- if the list genlist is not empty then do below actions
-- display
set genre_choose to choose from list genreLIst OK button name "OK" cancel button name "Exit" with prompt "Choose a genre" with title "Genre List" without multiple selections allowed -- genre_choose will hold thye result of the choose from list line.
if genre_choose is not false then -- if you HAVE chosen, and not cancelled, then
set playlist_genre to genre_choose -- set the variable playlist_genre to be the same as the result of genre_choose
-- call subroutine makeTracks(playlist_genre)
my makeTracks(playlist_genre) -- putting (a variable ) in the brackets like this will make the variable inside avalible to the subroutine, in this case makeTracks()
-- when the above subroutine is completed the script will return here to run the next part.
display dialog "What Next" buttons {"Exit", "Play " & playlist_genre, "Again"} default button 2
if the button returned of the result is "Play - " & genre_choose then
-- play the new playlist
play playlist playlist_genre_rand
else if the button returned of the result is "Again" then
--start again
my makeMe()
end if
end if
-- end
else if genreLIst = {} then -- if the list genlist was empty, the above actions would have been skipped, and an ELSE statement like this would have been looked for, for what to do next.
display dialog "There are no
genre's set in your library " & return & "Set some genre's and run the script again" -- So the next thing would be to display this.
end if
end tell
end makeMe
on makeTracks(playlist_genre) -- beginning of a subroutine
tell application "iTunes"
-- looks for tracks with genre
set track_list to every track of library playlist 1 whose genre contains playlist_genre
if not track_list = {} then
-- sets the name of the playlist (_RP is added to make sure no other playlist is touched)
set playlist_genre_rand to playlist_genre & "_RP" as string
if not (exists playlist playlist_genre_rand) then
--make the playlist if it does not exist
make new playlist with properties {name:playlist_genre_rand}
else
-- if the playlist already exists then remove the current tracks from it.
set track_list_d to tracks of user playlist playlist_genre_rand
repeat with i from 1 to number of items in track_list_d
set this_item to item i of track_list_d
delete this_item
end repeat
end if
set view of browser window 1 to playlist playlist_genre_rand -- show you the playlist
set biglist to {}
repeat with i from 1 to 10
-- create random number from 1 to count of number of tracks found
set track_number to random number from 1 to (count of track_list)
-- checks to see if track is already used
if track_number is not in biglist then
copy track_number to end of biglist
set track_location to location of item track_number of track_list
-- if not then adds it to playlist
add track_location to user playlist playlist_genre_rand
else if track_number is in biglist then
-- if track is already used , try and get a different track
set counter to 0
repeat until track_number is not in biglist or counter is (count of track_list)
set counter to counter + 1
set track_number to random number from 1 to (count of track_list)
copy track_number to end of biglist
end repeat
set track_location to location of item track_number of track_list
add track_location to user playlist playlist_genre_rand
end if
end repeat
end if
end tell
end makeTracks
edit 1 iTunes browser window is set to playlist edit 1 I have some radio stations in my library, this will cause an error, so changing tracks to file track fixes this.
I know I got your intent, I but could not resist, It was a nice idea and fun playing with the script. But I also wanted the OP to be able to just show off with Applescript with something that had a real end result and did something cool, which the idea of this script is. Thanks. I am actually using it.