LoginSignup
3
3

More than 5 years have passed since last update.

peco-itunes

Last updated at Posted at 2014-07-08
_itunes_list_tracks() {
  osascript <<EOF
tell application "iTunes"
  set accumulator to ""
  repeat with t in every track
    set output to "" & (id of t) & tab & "\"" & (name of t) & "\"" & tab & "\"" & (artist of t) & "\""
    set trackInfo to do shell script "echo " & quoted form of output without altering line endings
    set accumulator to accumulator & trackInfo
  end repeat
end tell
EOF
}

_itunes_delete_playlist() {
  osascript <<EOF
tell application "iTunes"
  try
    delete user playlist "$1"
  end try
end tell
EOF
}

_itunes_add_track_to_playlist() {
  osascript <<EOF
set myPlaylistName to "$2"
tell application "iTunes"
  try
    set myPlaylist to user playlist myPlaylistName
  on error
    set myPlaylist to (make new user playlist with properties {name:myPlaylistName})
  end try

  duplicate (every track whose id is "$1") to myPlaylist
end tell
EOF
}

_itunes_play_playlist() {
  osascript <<EOF
tell application "iTunes"
  play user playlist "$1"
end tell
EOF
}

peco-itunes() {
  local playlistName="Peco-iTunes"

  local tracks=$(_itunes_list_tracks | peco --prompt "${playlistName}>" | awk '{print $1}')

  if [ -z "$tracks" ] ; then
    return
  fi

  _itunes_delete_playlist $playlistName

  local IFS=$'\n'
  for track in $tracks ; do
    IFS=" "; _itunes_add_track_to_playlist "$track" "$playlistName"
  done

  _itunes_play_playlist "$playlistName"
}
3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3