Create playlist from selected items and then open with default program?

Hello,

I'd like to make a simple button that writes the paths of the selected items to a temporary m3u and then runs that file with the default program.

I tried the following and it's erroring out. I'd appreciate any help!

function OnClick(clickData)
{
    var cmd = clickData.func.command;
    var fsu = DOpus.FSUtil;
    var tab = clickData.func.sourcetab;

    var selItems = tab.selected;

    if (selItems.count == 0) {
        DOpus.Output("No items selected.");
        return;
    }

    // Create an empty array to store the selected file paths
    var filePaths = [];

    // Iterate through selected items and add their paths to the array
    for (var i = 0; i < selItems.count; i++) {
        filePaths.push(selItems(i).realpath);
    }

    if (filePaths.length > 0) {
        // Create the M3U playlist by joining the selected file paths
        var playlistContent = filePaths.join("\n");

        // Define the path for the temporary M3U file
        var tempPlaylistPath = fsu.GetTempFileName("temp", "playlist", "m3u");

        // Write the playlist content to the temporary file
        fsu.WriteFile(tempPlaylistPath, playlistContent);

        // Open the M3U playlist using CMD's start command
        cmd.RunCommand('Run ' + '"start \"\" \"' + tempPlaylistPath + '\""', true);
    }
}
1 Like
  • You might need to close the file after writing to it and before sending it to the other program.

  • cmd.RunCommand('Run ' doesn't make sense unless you have a Run command installed somewhere. There isn't usually any Run command in Opus or Windows.

  • You can use the FileType ACTION=shellex command in Opus to do the equivalent of double-clicking a file. Before running it via cmd.RunCommand(...), first call cmd.ClearFiles() and then cmd.AddFile(tempPlaylistPath) to tell it which the file to run the command on.

If you still have errors, let us know the details of what happens and what the error is.

nAIce try :wink:

3 Likes

Thank you for the guidance! Here's the final script, working like a charm. I've modified to just write to the same temp file over and over instead of create a new one everytime

function OnClick(clickData)
{
    var cmd = clickData.func.command;
    var fsu = DOpus.FSUtil;
    var tab = clickData.func.sourcetab;

    var selItems = tab.selected;

    if (selItems.count == 0) {
        DOpus.Output("No items selected.");
        return;
    }

    // Create an empty array to store the selected file paths
    var filePaths = [];

    // Iterate through selected items and add their paths to the array
    for (var i = 0; i < selItems.count; i++) {
        filePaths.push(selItems(i).realpath);
    }

    if (filePaths.length > 0) {
        // Create the M3U playlist by joining the selected file paths
        var playlistContent = filePaths.join("\n");

        // Create temp playlist file and write playlistContent to it
        var playlist = fsu.OpenFile("C:\Users\Shawn\AppData\Local\Temp\dopus_playist_from_selection.m3u", "wa");
		playlist.write(playlistContent);

        // Open the M3U playlist using CMD's start command
		cmd.ClearFiles();
		cmd.AddFile(playlist.path);
        cmd.RunCommand("FileType shellex");
    }
}

AI see what you did there

You're not closing the file after writing to it, which could cause problems (although may be OK, depends on the software opening it).

You're also running FileType shellex at the end, which I'm surprised works at all. The command should be FileType ACTION=shellex

You know, I did that because I was getting syntax errors. I was putting on its own line just: "FileType ACTION=shellex" without wrapping in run command. So I looked up the docs here: https://www.gpsoft.com.au/help/opus12/index.html#!Documents/FileType_Command.htm

The docs state:

Trigger a file-type-defined event for the selected files. For example, you could use this to trigger the drag-and-drop event for a file from a button or hotkey. This is the default argument for the FileType command - you do not need to specify the ACTION keyword.
Example: FileType drop

Thanks for the tip about file.Close()!

1 Like

Ah, my mistake. I didn't realise FileType had a default argument. Either method is fine in that case!

1 Like