【问题标题】:How can I check for existence of Itunes track in Applescript without throwing error如何在不抛出错误的情况下检查 Applescript 中是否存在 Itunes 曲目
【发布时间】:2015-08-31 03:02:21
【问题描述】:

这个脚本的想法(简化以显示问题)是检查轨道是否存在,

    tell application "iTunes"
        set matchtrack to get first track in playlist 1 whose persistent ID is "F1A68AD90AA66648"
        if matchtrack is not {} then
            print name of matchtrack
        else
            print "no track found"
        end if
    end tell

不幸的是,如果轨道不存在,则会出错

"iTunes got an error: Can’t get track 1 of playlist 1 whose persistent ID = \"F1A68AD90AA66648\"." number -1728 from track 1 of playlist 1 whose persistent ID = "F1A68AD90AA66648"
'

而不仅仅是打印“未找到轨道”

【问题讨论】:

    标签: applescript itunes


    【解决方案1】:

    get first track给一个track对象或者一个错误

    tracks给出一首曲目的列表或空列表

    tell application "iTunes"
        set matchtrack to tracks in playlist 1 whose persistent ID is "F1A68AD90AA66648"
        if matchtrack is not {} then
            return name of item 1 of matchtrack
        else
            return "no track found"
        end if
    end tell
    

    【讨论】:

      【解决方案2】:

      最简单的方法是使用try block,如下所示:

      set persistentID to "F1A68AD90AA66648"
      tell application "iTunes"
          try
              set matchtrack to get first track in playlist 1 whose persistent ID is persistentID
              if matchtrack is not {} then
                  log (get name of matchtrack)
              else
                  log "no track found"
              end if
          on error the error_message number the error_number
              log "Error (probably no track found)"
              -- display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
          end try
      end tell
      

      顺便说一句:使用log 代替print(get name of matchtrack) 代替(name of matchtrack)

      这可行,但会遍历命名播放列表中的所有曲目:

      set flagFound to false
      set mTrack to ""
      
      
      # SETUP THOSE FIRST
      # -------------------------------------------------------
      property Library_Name : "Library" # "Mediathek" in german
      property Playlist_Name : "Bob Marley"
      
      set persistentID to "F1A68AD90AA66648"
      # -------------------------------------------------------
      
      tell application "iTunes"
      
          tell source Library_Name
              tell playlist Playlist_Name
                  repeat with i from the (count of tracks) to 1 by -1
      
                      if (the persistent ID of track i is persistentID) then
                          set mTrack to track i
                          set flagFound to true
                          exit repeat
                      end if
      
                  end repeat
              end tell
          end tell
      
          if flagFound then
      
              tell mTrack
                  log "Found…"
                  log "Name: " & (get name)
                  log "persistent ID: " & (get persistent ID)
              end tell
      
          else
      
              log "no track with persistent ID " & persistentID & " found"
      
          end if
      
      end tell
      

      【讨论】:

      • 好吧,我明白了,但在你的例子中,'log "no track found"' 行将永远不会被调用,因为如果找不到 track 总是会出错?
      • 是的,当然,这就是为什么我们有 on 错误 - 它被记录的部分
      • 但是 else stement 有什么意义呢?我更喜欢一种能够以不必引发错误的方式进行检查的解决方案。
      • else 语句是我刚刚包装到 try 块中的代码中留下的,但在 iTunes 停止抛出此类错误的情况下,它会派上用场,它应该留在那里 | - 另一种方法见补充。
      • 使用 try 块来防止脚本不会因错误对话框而停止是绝对正常的。它不会改变脚本的运行方式,它将以相同的方式运行。 AppleScript 在“尝试”之类的词方面与其他语言不太相似。
      猜你喜欢
      • 2021-04-28
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      相关资源
      最近更新 更多