Selective Play/Pause VLC and iTunes with AppleScript

3

2

I have 5 button mouse, and i currently using one button for play/pause the music in iTunes (with USB Overdrive app). But sometimes i'm using VLC (i don't want scre up my music library with lot of s*it), and because i can assign AppleScript to the mousebuttons in USB Overdrive, i want to write an script wich checking wich program running currently. If iTunes, then it will tell play/pause to iTunes, but when VLC running it will tell to play/pause to VLC.

It works only in Automator, but when i save it as script or app and launch it in Finder it screwd up. If VLC not running it will open it. :( Why? And why it works correctly in Automator?

Works now:

on run {input, parameters}
    idle
    return input
end run

on idle
    set x to isAppLoaded("VLC")
    if x then
        tell application "VLC" to play
    end if
    set x to isAppLoaded("iTunes")
    if x then
        tell application "iTunes" to playpause
    end if
end idle

on isAppLoaded(app_name)
    tell application "System Events"
        set app_list to every application process whose name is app_name
        if the (count of app_list) > 0 then
            set x to true
        else
            set x to false
        end if
    end tell
    return x
end isAppLoaded

Thanks! miqlas

miqlas

Posted 2011-05-08T09:18:43.440

Reputation: 31

This code (and mine) behave very strangely in Automator. I suspect some kind of optimization that launches all referenced applications right at the start, but to be honest, I have no idea why it behaves like this in Automator. – Daniel Beck – 2011-05-08T12:02:50.780

Answers

2

It works for me in AppleScript Editor like the following, which you can use to save as Script (execute via AppleScript menu) or Application:

on isAppLoaded(app_name)
    tell application "System Events" to set app_list to every «class pcap» whose name is app_name
    return ((count of app_list) > 0)
end isAppLoaded

if isAppLoaded("VLC") then tell application "VLC" to play
if isAppLoaded("iTunes") then tell application "iTunes" to playpause

VLC doesn't start if it's already running (it does query for a file if none is loaded though). Tested on 10.6.7.

Daniel Beck

Posted 2011-05-08T09:18:43.440

Reputation: 98 421