« [AVFoundation]オーディオファイルの時間の合計を求める | トップページ | [Preview.app]画像をファイル名順(パス順)に開く »

[Music.app]選択曲の合計時間を求める

【スクリプトエディタで開く】

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
#                       com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application


---iTune呼び出し
tell application "Music"
  try
    ----選択されているファイルの実体先をリストで取得
    set listTrackSelection to selection as list
  on error
    ---選択していないとエラーになる
    log "曲を選択してください"
  end try
  log listTrackSelection
end tell

set numTotalTime to 0 as integer
set strOutPutText to "\r" as text

repeat with itemTrack in listTrackSelection
  tell application "Music"
    
    set strArtist to artist of itemTrack as text
    set strTitle to name of itemTrack as text
    set strTime to time of itemTrack as text
    set numDuration to duration of itemTrack as text
    (*
    tell itemTrack
      set aliasFileAlias to location as alias
    end tell
    *)
  end tell
  (*
  set strFilePath to POSIX path of aliasFileAlias
  set ocidFilePath to (refMe's NSString's stringWithString:strFilePath)
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:ocidFilePath)
  set ocidFileName to ocidFilePathURL's lastPathComponent
  set strPrefixName to (ocidFileName's stringByDeletingPathExtension) as text
      *)
  set strOutPutText to strOutPutText & ("" & strTime & "" & strTitle & " - " & strArtist & "\r")
  set numTotalTime to numTotalTime + numDuration as number
  
end repeat
##############################
##表示
##############################
set numMin to (round of (numTotalTime / 60) rounding down)
set numSec to (numTotalTime - (numMin * 60)) as integer


log "合計時間は" & numTotalTime & "秒です"
log "合計時間は約;" & numMin & "" & numSec & ""
log strOutPutText

set strOutPutText to "合計時間は" & numTotalTime & "秒です\r" & "合計時間は約;" & numMin & "" & numSec & "\r" & strOutPutText as text


set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns" as alias
set strDefaultAnswer to "入力してください" as text
tell application "Finder"
  (display dialog "結果です" with title "結果です" default answer strOutPutText buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 30 without hidden answer)
end tell


|

« [AVFoundation]オーディオファイルの時間の合計を求める | トップページ | [Preview.app]画像をファイル名順(パス順)に開く »

AppleScript Music」カテゴリの記事