// ==== Script info ================================================================================ // Name: S2MP (Send to Media Player) // Date created: 2022-01-20 15:03 (UTC +0200) // Version: 0.1.1 // URL: https://resource.dopus.com/t/s2mp-send-files-matching-file-type-groups-to-media-player-of-choice/40398 // License: MIT License (see below) // Author(s): filewrangler (https://resource.dopus.com/u/filewrangler) // // Description: A Directory Opus script to send files selected in the source file display which match a given file type group ("Music" & "Videos" by default) via M3U8 playlist to a media player of choice (VLC by default). // // If a selected item is a directory, all files within it will be considered (recursive). // Playlists containing absolute paths are created in the user's temporary directory (by default) as a means to send the files to a given media player. // // To install the script, drag & drop the script file onto the list of scripts at "Preferences > Toolbars > Scripts", enable the script, and add the code `exec_s2mp` to a Standard Function button script. // Alternatively, paste the contents of the script directly into a button script (set Function to Script Function and its Type to JScript). // // See the script's NOTE comments for some user configurable options (e.g. setting a custom media player). // ================================================================================================= // NOTE: Edit these variables to configure the script. // Check Opus' script log for any script output/errors. // NOTE: The media player's name (just for display purposes). var media_player_name = 'VLC'; // NOTE: Absolute path to the media player's executable. // Backslashes must be escaped, e.g. `\\` (single forwardslashes also work). var media_player_exe_path = 'C:\\Program Files\\VideoLAN\\VLC\\vlc.exe'; // NOTE: The executable's arguments. // For VLC, it may be desireable to set this to `--one-instance --playlist-enqueue` which will send all files (via playlist) to one VLC instance, with any additional playlists being queued (if this sounds ideal, also set the below `should_use_unique_playlist_file_names` to `true`). // VLC: https://wiki.videolan.org/VLC_command-line_help/. // Backslashes and double-quotation marks must be escaped, e.g. `\\`, `\"` (single forwardslashes also work). var media_player_args = ''; // NOTE: The file type groups to query the selected files against (only files that match will be written to the playlist). // Additional file type groups can be added by appending their display names. // Remove `, "videos"` if the media player can only play audio files. // Default value is `DOpus.create().vector("music", "videos");`. var file_type_groups = DOpus.create().vector('music', 'videos'); // NOTE: The absolute directory path that all playlists are written to. // When set to `""` (an empty value), the user's temporary directory (%temp%) will be used. // Only paths not requiring elevated privileges are valid. // Backslashes must be escaped, e.g. "\\" (single forwardslashes also work). var playlist_save_dir_path = ''; // NOTE: When `true`, playlist files will be uniquely named with each run of the script. // To re-use (i.e. write to) the same playlist each time, set to the variable to `false`. var should_use_unique_playlist_file_names = false; // NOTE: When `true`, the total number of playlist items is written as a comment at the top of the playlist file. var should_write_file_count_comment_to_playlist = false; // NOTE: When `true`, a new tab is opened with the created playlist file selected and in view (instead of opening the playlist in the media player). var should_open_playlist_dir_instead_of_media_player = false; // NOTE: When `true` and `should_open_playlist_dir_instead_of_media_player` is also `true`, the directory opens in the destination file display (toggles on dual mode if it is currently disabled). var should_playlist_dir_open_in_dest_file_display = false; // ================================================================================================= // NOTE: All user options are above this line. var script_short_name = 'S2MP'; var script_long_name = 'Send to Media Player'; var script_full_name = script_short_name + ' (' + script_long_name + ')'; function OnClick(clickData) { verify_user_options_are_valid(); print('Script executed...'); var source_tab = clickData.func.sourcetab; var selected_items = source_tab.selected; if (selected_items.count === 0) { abort( "No items selected! Please select some items from the lister's source file display and try again." ); } var factory = DOpus.create(); var files_matching_a_group = factory.vector; var playlist_contents = ''; for (var i = 0; i < selected_items.count; i++) { var selected_item = selected_items(i); if (selected_item.is_dir || selected_item.is_symlink || selected_item.is_junction) { var dir_enum = DOpus.fsutil.readdir(selected_item, 'r'); var dir_items = dir_enum.next(-1); for (var j = 0; j < dir_items.count; j++) { if (is_in_group(dir_items(j), file_type_groups)) { files_matching_a_group.append([dir_items(j)]); playlist_contents += dir_items(j) + '\n'; } } } // Else is (likely) a file... else { if (is_in_group(selected_item, file_type_groups)) { files_matching_a_group.append([selected_item]); playlist_contents += selected_item + '\n'; } } } if (files_matching_a_group.count === 0) { abort('Selected files did not match any specified file type groups!'); } if (should_write_file_count_comment_to_playlist) { playlist_contents = '# ' + script_full_name + ': ' + files_matching_a_group.count + ' file(s) total.\n' + playlist_contents; } var blob = factory.blob(); blob.copyfrom(playlist_contents, 'utf8'); var playlist_name = script_short_name + '_' + media_player_name; var playlist_ext_with_dot = '.m3u8'; if (!playlist_save_dir_path) { playlist_save_dir_path = DOpus.aliases('temp').path; } else { playlist_save_dir_path = DOpus.fsutil.newpath(playlist_save_dir_path); } var playlist_path = null; var date_in_ms = new Date().valueOf(); if (should_use_unique_playlist_file_names) { playlist_path = DOpus.fsutil.newpath( playlist_save_dir_path + '\\' + playlist_name + ' [' + date_in_ms + ']' + playlist_ext_with_dot ); } else { playlist_path = DOpus.fsutil.newpath( playlist_save_dir_path + '\\' + playlist_name + playlist_ext_with_dot ); } var playlist_file = DOpus.fsutil.openfile(playlist_path, 'w', 'noelevate'); if (playlist_file.error === 0) { playlist_file.write(blob); print( 'Playlist created containing ' + files_matching_a_group.count + ' file(s): ' + playlist_path ); playlist_file.close(); } else { var error = playlist_file.error; playlist_file.close(); abort('Error writing playlist file (' + playlist_path + ')! Error: ' + error); } blob.free(); var cmd = factory.command; if (should_open_playlist_dir_instead_of_media_player) { if (should_playlist_dir_open_in_dest_file_display) { cmd.addline('@if:set dual=off'); cmd.addline('set dual=toggle,dest,remember'); cmd.addline('@if:common'); cmd.addline( 'go "' + playlist_save_dir_path + '" newtab=findexisting,tofront openindest' ); cmd.addline('set dest=toggle'); } else { cmd.addline('go "' + playlist_save_dir_path + '" newtab=findexisting,tofront'); } print("Opening the playlist file's directory..."); cmd.addline('select none'); cmd.addline('select "' + playlist_name + playlist_ext_with_dot + '" setfocus exact'); } else { print('Opening playlist in ' + media_player_name + '...'); cmd.addline( '@async:"' + media_player_exe_path + '" ' + media_player_args + ' "' + playlist_path + '"' ); } cmd.run(); print('Complete!'); } function print(message) { DOpus.output(message); } function log(message) { message = script_short_name + ': ' + message; print(message); } function abort(message) { if (!message) { message = 'Aborting!'; } throw new Error(message); } function is_in_group(item, groups) { for (var i = 0; i < groups.count; i++) { if (item.ingroup('disp:' + groups(i))) { return true; } } return false; } function verify_user_options_are_valid() { if ( !DOpus.fsutil.exists(media_player_exe_path) && !should_open_playlist_dir_instead_of_media_player ) { abort( 'Path "' + media_player_exe_path + "\" does not exist! Please set `media_player_exe_path` to a media player's executable file's path and try again." ); } if (playlist_save_dir_path && !DOpus.fsutil.exists(playlist_save_dir_path)) { abort( 'Path "' + playlist_save_dir_path + '" does not exist! Please set `playlist_save_dir_path` to an existing directory path and try again.' ); } } // NOTE: These two functions, `OnInit()` and `on_exec_s2mp()`, can optionally be removed when pasting the script directly into a button's JScript. function OnInit(initData) { initData.name = 'S2MP (Send to Media Player)'; initData.version = '0.1.1'; initData.copyright = '(c) 2022 filewrangler'; initData.url = 'https://resource.dopus.com/t/s2mp-send-files-matching-file-type-groups-to-media-player-of-choice/40398'; initData.desc = 'Send files matching file type groups to media player of choice'; initData.default_enable = false; initData.min_version = '12.26'; var cmd = initData.addcommand(); cmd.name = 'exec_s2mp'; cmd.method = 'on_exec_s2mp'; DOpus.output('Script initialised!'); } function on_exec_s2mp(clickData) { OnClick(clickData); } // ==== License ==================================================================================== // MIT License // // Copyright (c) 2022 filewrangler // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // =================================================================================================