Skip to content

Instantly share code, notes, and snippets.

@FunctionDJ
Created March 4, 2022 10:52
Show Gist options
  • Select an option

  • Save FunctionDJ/47e37ea673ef3fd22279ee17bec13168 to your computer and use it in GitHub Desktop.

Select an option

Save FunctionDJ/47e37ea673ef3fd22279ee17bec13168 to your computer and use it in GitHub Desktop.

Revisions

  1. FunctionDJ created this gist Mar 4, 2022.
    89 changes: 89 additions & 0 deletions funky-delete.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,89 @@
    /** @param {FbMetadbHandle} track */
    const playTrack = track => {
    const result = fb.RunContextCommandWithMetadb("Play", track)
    if (result !== true) {
    throw new Error(`Play command failed for track "${track.Path}"`)
    }
    }

    const makeLogger = (...prefixed) => (
    (...args) => {
    console.log(...prefixed, ...args)
    }
    )

    /** @param {FbMetadbHandleList} tracks */
    const deleteTracks = tracks => {
    const command = tracks.Count > 1 ? "Delete files" : "Delete file"
    fb.RunContextCommandWithMetadb(command, tracks)
    }

    /**
    * @param {FbMetadbHandleList} tracksToAvoid
    * @param {(...args: any[]) => void} log
    */
    const assurePlayheadNotOnTracks = (tracksToAvoid, log) => {
    const nowPlaying = fb.GetNowPlaying()
    const isPaused = nowPlaying === null

    if (isPaused) {
    log("paused")
    return
    }

    const nowPlayingTrackIsInTracks = tracksToAvoid.Convert().some(t => t.Path === nowPlaying.Path)

    if (!nowPlayingTrackIsInTracks) {
    log("nowplaying is not in tracks")
    return
    }

    const currentPlaylist = plman.GetPlaylistItems(plman.ActivePlaylist)
    const firstTrackToAvoid = getHandleFromListAtIndex(tracksToAvoid, 0)
    const playlistPositionOfFirstToAvoid = currentPlaylist.Find(firstTrackToAvoid)

    if (playlistPositionOfFirstToAvoid === -1) {
    log("First track to avoid not found in active playlist")
    }

    const nextValidTrack = currentPlaylist.Convert()
    .find(track => {
    const isNotToAvoid = tracksToAvoid.Find(track) === -1
    const trackPosition = currentPlaylist.Find(track)
    const isAfterFirstToAvoid = trackPosition > playlistPositionOfFirstToAvoid
    return isNotToAvoid && isAfterFirstToAvoid
    })

    if (nextValidTrack === undefined) {
    log("no track to jump to, stopping playback")
    fb.Stop()
    } else {
    log("playing " + nextValidTrack.Path)
    try {
    playTrack(nextValidTrack)
    } catch (error) {
    log("error: " + error.message)

    // show context menu of next valid track with options for debugging purposes
    const ctx = fb.CreateContextMenuManager()
    ctx.InitContext(new FbMetadbHandleList(nextValidTrack))
    const menu = window.CreatePopupMenu()
    ctx.BuildMenu(menu, 1)
    menu.TrackPopupMenu(0, 0)
    fb.Stop()
    }
    }
    }

    const funkyDelete = () => {
    const log = makeLogger("funkyDelete:")

    const selectedTracks = fb.GetSelections()
    assurePlayheadNotOnTracks(selectedTracks, log)

    for (const track of selectedTracks.Convert()) {
    log(`deleting ${track.Path}`)
    }

    deleteTracks(selectedTracks)
    }