Uncategorized 26 Jan 2003 02:28 am

Applescript revision 1

Ok, here is an altered verison of the script. I am putting it inline even though it will lose formatting to try a new Apple feature. the new Applescript Editor is available as an application service, so if the app supports it, you can execute inline Applescript snippets. So, browsing this site in Safari, I can just highlight and execute the following script:

— this little script will play a song that you choose out of a list
— of songs whose name contains a string that you choose

— choose wisely
tell application “iTunes”
activate
tell playlist “Library”
set chosenName to “” — or your favorite default
display dialog “Please enter the song search string” default answer “”
set search to text returned of result
if search is not “” then
set chosenName to search
else
seeYa()
end if
set trackList to tracks whose name contains chosenName
if length of trackList is greater than 0 then
set trackNameList to {}
repeat with trackName in trackList
set trackNameList to trackNameList & name of trackName
end repeat
if (count of items in trackNameList) = 1 then — thanks Fred for the smooth autoplay option!
set nameToPlay to item 1 of trackNameList
else
set nameToPlay to choose from list my ASCII_Sort(trackNameList) with prompt “Which song” as string
end if
if nameToPlay is not false then
play track (nameToPlay as string)
else
seeYa()
end if
else
display dialog “No Matches to ” & chosenName buttons {“OK”} default button “OK”
end if
end tell
end tell

on seeYa()
display dialog “Thanks anyway!” buttons {“OK”} default button “OK”
stop
end seeYa

— borrowed from an Apple script
on ASCII_Sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to “”
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is “” then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end ASCII_Sort

Subscribe to the comments through RSS Feed

Leave a Reply