Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于控制iTunes和Rdio的Applescript_Applescript_Itunes_Rdio - Fatal编程技术网

用于控制iTunes和Rdio的Applescript

用于控制iTunes和Rdio的Applescript,applescript,itunes,rdio,Applescript,Itunes,Rdio,我有一个与结合使用的Applescript,它可以播放或暂停iTunes或中的当前曲目,具体取决于我打开的曲目。在我的脚本中,Rdio优先,因为我总是打开iTunes,并且只有在我出于特定目的需要时才打开Rdio 通常,在iTunes中播放曲目时,我点击我的全局快捷键来运行该脚本,最多需要15秒来停止该曲目。我想在这里分享这个脚本,看看是否有一个突出的问题,或者是否有一个更简单、更有效的方法来处理它 我很感激能得到的任何帮助 tell application "System Events"

我有一个与结合使用的Applescript,它可以播放或暂停iTunes或中的当前曲目,具体取决于我打开的曲目。在我的脚本中,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

很难找到这样的问题。一般来说,你的脚本看起来不错。这里有一些想法可能会帮助你解决问题

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

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

    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

非常感谢你的建议!获取此错误:错误“Rdio获取错误:无法继续暂停”。编号-1708Brandon,我没有Rdio,因此无法验证上面的第一个代码。我想你的问题在于“使用术语”的东西,所以试着不用它。我添加了一个带有新代码的“编辑”部分。我希望有帮助。