How can I repeat an AppleScript function if a variable is changed?

I am creating a script that will append the first 10 integers of an album name to a website URL for a band - part that I have working so far looks like this:



tell application "iTunes"

if player state is playing then

set albumName to (get album of current track)

set myText to text 1 thru 10 of albumName

end if

end tell

temptext = myText

end if

open location "http://bandsite.com/shows/" & myText


So if an album is named "1999-01-04 - Great Hits" - this will return "1999-01-04" and append it to the bandsite.com/shows/ URL.


I need the above function to repeat itself whenever the album is changed in iTunes not the track. My work in progress code for that is:


Set temptext = myText

if temptext is not myText then

tell application "iTunes"

if player state is playing then

set albumName to (get album of current track)

set myText to text 1 thru 10 of albumName

end if

end tell

temptext = myText

end if

open location "http://bandsite.com/shows/" & myText

repeat while temptext = text

end repeat


I keep getting snytax errors. Any help would be greatly appreciated. Thank you!

MacBook Pro with Retina display, OS X Mountain Lion (10.8.1)

Posted on Mar 13, 2013 6:48 AM

Reply
1 reply

Mar 15, 2013 8:12 PM in response to iPhanner

In AppleScript, using the equal sign in a statement such as temptext = myText tests for equality - it does not assign a value. The set command is used to assign a value to a variable.


Using a repeat statement to check for an album change is also not really the way to go, since it tends to max out your CPU usage - if you create a stay-open application, it will run in the background and the idle handler will be called periodically (the default is every 30 seconds):


propertyoldText : missing value


onidle-- check periodically


checkAlbum()


return30-- or however many seconds before the next time

endidle


oncheckAlbum() -- check if the current album name has changed


tellapplication"iTunes"

ifplayer stateisnotplayingthenreturn

setalbumNameto (getalbumofcurrent track)


endtell


setmyTexttotext1thru10ofalbumName


ifmyTextisnotoldTextthen

setoldTexttomyText-- update

open location"http://bandsite.com/shows/" & myText


endif

endcheckAlbum

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How can I repeat an AppleScript function if a variable is changed?

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