0

I have an Applescript I use in conjunction with Alfred that plays or pauses the current track in iTunes or Rdio, depending on which I have open. In my script Rdio takes precedence because I always have iTunes open and only open Rdio when I need it for a specific purpose.

Often, when a track is playing in iTunes and I hit my global shortcut to run this script it takes up to 15 seconds to stop the track. I wanted to share the script here and see if there might be a glaring issue, or if there is a much simpler, more efficient way to handle it.

I appreciate any help I can get!

tell application "System Events"
    if (name of processes) contains "iTunes" then
        set iTunesRunning to true
    else
        set iTunesRunning to false
    end if
    if (name of processes) contains "Rdio" then
        set RdioRunning to true
    else
        set RdioRunning to false
    end if
end tell

if RdioRunning then
    tell application "Rdio"
        if player state is paused or player state is stopped then
            play
        else if player state is playing then
            pause
        end if
    end tell
else if iTunesRunning then
    tell application "iTunes"
        if player state is paused or player state is stopped then
            play
        else if player state is playing then
            pause
        end if
    end tell
end if
uzi
  • 5,085
  • 2
  • 16
  • 11
Brandon Durham
  • 7,096
  • 13
  • 64
  • 101

1 Answers1

1

It's difficult to track down such issues. In general your script looks fine. Here's some ideas that may help with your problem though.

In general applescripts are interpreted at run-time which means that every time you run your script the byte-code has to be changed into machine language code by another program (applescript runner)... this is normally not a problem but in your case maybe it's causing some slowness. So the idea is to write your script so that doesn't need to happen. We can do that by saving the script as an applescript application because applications are saved in the machine language form and thus do not require another program to execute the code. In addition we can take advantage that the commands for both applications are identical, so we can use a "using terms from" block. In your code you query system events for the "name of processes" twice, so the last optimization we can make is to only do that once.

So try this and see if it helps. I'm not certain it will but it's worth a try. Remember to save it as an application.

    tell application "System Events" to set pNames to name of application processes

    if "Rdio" is in pNames then
        set appName to "Rdio"
    else if "iTunes" is in pNames then
        set appName to "iTunes"
    else
        return
    end if

    using terms from application "iTunes"
        tell application appName
            if player state is paused or player state is stopped then
                play
            else if player state is playing then
                pause
            end if
        end tell
    end using terms from

EDIT: If the above code doesn't work then try this. As mentioned, try it as an application and see if it helps. The same principles apply... one less query of system events and saving as an application to prevent needing to interpret the code.

tell application "System Events" to set pNames to name of application processes

if "Rdio" is in pNames then
    tell application "Rdio"
        if player state is paused or player state is stopped then
            play
        else if player state is playing then
            pause
        end if
    end tell
else if "iTunes" is in pNames then
    tell application "iTunes"
        if player state is paused or player state is stopped then
            play
        else if player state is playing then
            pause
        end if
    end tell
end if
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • Thanks so much for the suggestion! Getting this error: error "Rdio got an error: Can’t continue pause." number -1708 – Brandon Durham Sep 10 '11 at 18:37
  • Brandon, I don't have Rdio so I wasn't able to validate the first code above. I assume your problem is with the "using terms" stuff so try it without that. I added an "EDIT" section with new code. I hope it helps. – regulus6633 Sep 10 '11 at 19:50