Skip navigation

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

343 Views 1 Reply Latest reply: Mar 15, 2013 8:12 PM by red_menace RSS
iPhanner Calculating status...
Currently Being Moderated
Mar 13, 2013 6:48 AM

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)
  • red_menace Level 6 Level 6 (14,275 points)

    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):

     

    property oldText : missing value

     

    on idle -- check periodically

      checkAlbum()

      return 30 -- or however many seconds before the next time

    end idle

    on checkAlbum() -- check if the current album name has changed

      tell application "iTunes"

        if player state is not playing then return

        set albumName to (get album of current track)

      end tell

      set myText to text 1 thru 10 of albumName

      if myText is not oldText then

        set oldText to myText -- update

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

      end if

    end checkAlbum


Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.