2017-01-16 8 views
0

私はAppleScriptをいくつかのアーティストのトップソングを再生できるようにしようとしています。私はScriptEditor経由でiTunesのスクリプトライブラリを見たけど、私には役に立たないものは何も見つかりませんでした。このケースが可能な場合、誰かが正しい方向に私を転送することを願っています。ありがとう。AppleScriptのアーティストのTOP曲を再生

+0

"トップソング"はどのように定義しますか?最高の演奏数を持つ曲は? –

+0

@ oa- iTunesで特別なパラグラフ「トップソング」があります –

答えて

1

iTunesからトラックを取得する基本的な方法は、get every trackです。たとえば、

tell application "iTunes" to set topTracks to every track of playlist "Library" whose artist is "Golden Earring" and rating is 100 

iTunes GUIでは1〜5星(または星印なし)ですが、バックエンドでは0〜100です。バックエンドの評価に20を掛けます(またはGUIのスター数についてはバックエンドの評価を20で割る)。0星は0に等しくなります。

iTunesには奇妙な点があります。 and rating is 100を追加すると、簡単なもの(アーティスト)と難しいもの(レーティング)を行うのではなく、両方のクエリを検索してまとめているかのように、クエリが非常に遅くなります。そのため、これはおそらく、はるかに高速に特定のアーティストの五格のトラックのすべてを得ることで、非常に次のようになります。

tell application "iTunes" 
    set artistTracks to every track of playlist "Library" whose artist is "Golden Earring" 
    set topTracks to {} 
    repeat with possibleTrack in artistTracks 
     if the rating of possibleTrack is 100 then copy possibleTrack to end of topTracks 
    end repeat 
end tell 

、「トップソング」で、あなたはトップX曲を意味している場合xに達するのに十分なレーティング5のトラックがあるという保証はありませんので、特定の番のトラックをにするには、5トラックを取得し、十分なあなたがそれをする方法の一例がありますが、それはあなたが「トップソング」をどのように定義するかに部分的に依存します。

あなたは誰もGUIでトラックを一時停止していないか、進めることを合理的に確実な場合 topTracksリスト内の次のトラックに進みとき選択するための
tell application "iTunes" 
    set desiredArtist to "chi coltrane" 

    --get topCount tracks 
    set topCount to 5 
    set fiveStars to {} 
    set fourStars to {} 
    set threeStars to {} 
    set twoStars to {} 
    set oneStar to {} 
    set noStars to {} 
    set allTracks to every track of playlist "Library" whose artist is desiredArtist 

    --collect tracks into bins according to star rating 
    --this is much faster than doing five searches with "and rating is" 
    repeat with possibleTrack in allTracks 
     copy (rating of possibleTrack)/20 as integer to starRating 

     if starRating is 5 then 
      copy possibleTrack to end of fiveStars 
     else if starRating is 4 then 
      copy possibleTrack to end of fourStars 
     else if starRating is 3 then 
      copy possibleTrack to end of threeStars 
     else if starRating is 2 then 
      copy possibleTrack to end of twoStars 
     else if starRating is 1 then 
      copy possibleTrack to end of oneStars 
     else 
      copy possibleTrack to end of noStars 
     end if 
    end repeat 
end tell 

--collect the top tracks 
set topTracks to {} 
getTracks(fiveStars) 
getTracks(fourStars) 
getTracks(threeStars) 
getTracks(twoStars) 
getTracks(oneStar) 
getTracks(noStars) 

--play the tracks, if any 
if (count of topTracks) > 0 then 
    tell application "iTunes" 
     repeat with topTrack in topTracks 
      set topTrackID to the persistent ID of topTrack 
      play topTrack 
      --wait until this song is no longer playing, then go to the next 
      repeat while the persistent ID of current track is topTrackID 
       --delay a tenth of a second to avoid too much of the next random track 
       delay 0.1 
      end repeat 
     end repeat 
    end tell 
end if 

--add more tracks from the given trackList, until topCount is reached 
--within a list, the choice of which tracks to use to reach topCount is somewhat arbitrary 
on getTracks(trackList) 
    global topTracks, topCount 
    if (count of topTracks) ≥ topCount then return 
    repeat with possibleTrack in trackList 
     if (count of topTracks) ≥ topCount then 
      return 
     end if 
     copy possibleTrack to end of topTracks 
    end repeat 
end getTracks 

その他のオプションは、the duration of topTrack as numberのために遅延などがあります。トラックが変更されたときに通知を受け取るハンドラを設定することもできます。

+0

この大きな回答に非常に感謝します。しかし、私のライブラリからではなく、Apple Musicリストからトラックを入手できますか? –

+0

私はそうは思わない。しかし、あなたの最初のステップは、「アーティストのトップ・ソング」が意味するものをより明確に定義する必要があります。つまり、曲を演奏するために満たさなければならない基準は何ですか?この例に達するために取った手順と例を提供するのに役立つかもしれません。いったんこれを行うと、iTunesを使用しているソリューションやその他のメカニズムを使用したソリューションが明らかになることがあります。 –

+0

iTunesには特別な段落「トップソング」があり、このリストを取得したいと考えています。しかし、あるアーティストのランダムな曲のリストを手に入れることもできます。 –

関連する問題