0

我有一个与Alfred一起使用的 Applescript,它可以播放或暂停 iTunes 或Rdio中的当前曲目,具体取决于我打开的曲目。在我的脚本中,Rdio 具有优先权,因为我总是打开 iTunes,并且仅在出于特定目的需要它时才打开 Rdio。

通常,当在 iTunes 中播放曲目并且我点击全局快捷方式来运行此脚本时,最多需要 15 秒才能停止曲目。我想在这里分享脚本,看看是否存在明显的问题,或者是否有更简单、更有效的方法来处理它。

我很感激我能得到的任何帮助!

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
4

1 回答 1

1

很难追查此类问题。一般来说,您的脚本看起来不错。这里有一些想法可能有助于解决您的问题。

一般来说,applescripts 在运行时解释,这意味着每次运行脚本时,字节码都必须由另一个程序(applescript runner)更改为机器语言代码......这通常不是问题,但在你的情况下也许它会导致一些缓慢。所以想法是编写你的脚本,这样就不需要发生。我们可以通过将脚本保存为 applescript 应用程序来做到这一点,因为应用程序以机器语言形式保存,因此不需要其他程序来执行代码。此外,我们可以利用两个应用程序的命令相同的优势,因此我们可以使用“使用术语来自”块。在您的代码中,您两次查询系统事件以获取“进程名称”,因此我们可以进行的最后一次优化是只执行一次。

所以试试这个,看看它是否有帮助。我不确定它会不会,但值得一试。请记住将其保存为应用程序。

    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

编辑:如果上面的代码不起作用,那么试试这个。如前所述,将其作为应用程序尝试,看看是否有帮助。同样的原则也适用......对系统事件的查询减少并保存为应用程序以防止需要解释代码。

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
于 2011-09-10T06:11:41.217 回答