From fa5868f4a75bb6793591e53f5edb3f5838c9fd47 Mon Sep 17 00:00:00 2001 From: Peter Vaiciulis <98245483+p5quared@users.noreply.github.com> Date: Fri, 26 Jul 2024 18:04:32 -0400 Subject: [PATCH 1/4] fix single quote issue --- lua/apple-music/init.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lua/apple-music/init.lua b/lua/apple-music/init.lua index 4876d12..1d30d3c 100644 --- a/lua/apple-music/init.lua +++ b/lua/apple-music/init.lua @@ -102,6 +102,8 @@ end ---@usage require('apple-music').play_track("Sir Duke") M.play_track = function(track) print("Playing " .. track) + -- Escape tracks that have single quotes in their name + local escaped_track = track:gsub("'", "'\\''") local command = string.format([[ osascript -e ' tell application "Music" From 49a9e5d9744f4515b093101334613f63697ca154 Mon Sep 17 00:00:00 2001 From: Peter Vaiciulis <98245483+p5quared@users.noreply.github.com> Date: Fri, 26 Jul 2024 18:10:27 -0400 Subject: [PATCH 2/4] make escape function --- lua/apple-music/init.lua | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lua/apple-music/init.lua b/lua/apple-music/init.lua index 1d30d3c..361e87b 100644 --- a/lua/apple-music/init.lua +++ b/lua/apple-music/init.lua @@ -44,6 +44,11 @@ local execute = function(cmd) return pcall(exe, cmd) end +--- Escape single and double quotes from names +local sanitize_name = function(name) + return name:gsub("'", "'\\''"):gsub('"', '\\"') +end + local grab_major_os_version = function() local command = [[ osascript -e 'set osver to system version of (system info)' ]] local _, result = execute(command) From b0c0759b39000e737eb29548052e2fdf97e6f83d Mon Sep 17 00:00:00 2001 From: Peter Vaiciulis <98245483+p5quared@users.noreply.github.com> Date: Fri, 26 Jul 2024 18:16:42 -0400 Subject: [PATCH 3/4] sanitize all names --- lua/apple-music/init.lua | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/lua/apple-music/init.lua b/lua/apple-music/init.lua index 361e87b..e8e4bf4 100644 --- a/lua/apple-music/init.lua +++ b/lua/apple-music/init.lua @@ -75,10 +75,12 @@ local set_track_favorited_state = function(track, state) if grab_major_os_version() < 14 then command_property = "loved" end + + local sanitized_trackname = sanitize_name(track) local command = string.format( [[ osascript -e 'tell application "Music" to set %s of track "%s" to "%s"' ]], command_property, - track, + sanitized_trackname, state ) execute(command) @@ -92,8 +94,6 @@ end ---@mod apple-music.nvim PLUGIN OVERVIEW local M = {} ----NOTE: Requires the song to be an exact title (not fuzzy) - ---Setup the plugin ---@param opts table|nil: Optional configuration for the plugin --- * {temp_playlist_name: string} - The name of the temporary playlist to use @@ -107,14 +107,14 @@ end ---@usage require('apple-music').play_track("Sir Duke") M.play_track = function(track) print("Playing " .. track) - -- Escape tracks that have single quotes in their name - local escaped_track = track:gsub("'", "'\\''") + + local sanitized = sanitize_name(track) local command = string.format([[ osascript -e ' tell application "Music" play track "%s" end tell' - ]], track) + ]], sanitized) local result = execute(command) end @@ -123,17 +123,16 @@ end ---@param playlist string: The name of the playlist to play ---@usage require('apple-music').play_playlist("Slow Dance") M.play_playlist = function(playlist) + print("Playing playlist: " .. playlist) + + local sanitized = sanitize_name(playlist) local cmd = string.format([[ osascript -e ' tell application "Music" to play playlist "%s" end' - ]], playlist) + ]], sanitized) - if execute(cmd) then - print("Playing playlist: " .. playlist) - else - print("Failed to play playlist: " .. playlist) - end + execute(cmd) end ---Play an album by name @@ -141,6 +140,9 @@ end ---@param album string: The name of the album to play ---@usage require('apple-music').play_album("Nashville Skyline") M.play_album = function(album) + print("Playing album: " .. album) + + local sanitized = sanitize_name(album) local command = string.format([[ osascript -e ' tell application "Music" @@ -151,13 +153,9 @@ M.play_album = function(album) end repeat play tempPlaylist end tell' - ]], M.temp_playlist_name, album) + ]], M.temp_playlist_name, sanitized) - if execute(command) then - print("Playing album: " .. album) - else - print("Failed to play album: " .. album) - end + execute(command) end ---Play the next track From d9287e12924368d3d4380cc6f3b546bde4b1781a Mon Sep 17 00:00:00 2001 From: "Peter V." <98245483+p5quared@users.noreply.github.com> Date: Sat, 27 Jul 2024 20:36:05 -0400 Subject: [PATCH 4/4] Update lua/apple-music/init.lua Co-authored-by: Daniel Lazar <38068717+ducktordanny@users.noreply.github.com> --- lua/apple-music/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/apple-music/init.lua b/lua/apple-music/init.lua index e8e4bf4..6dca0e3 100644 --- a/lua/apple-music/init.lua +++ b/lua/apple-music/init.lua @@ -46,7 +46,7 @@ end --- Escape single and double quotes from names local sanitize_name = function(name) - return name:gsub("'", "'\\''"):gsub('"', '\\"') + return name:gsub("\\", "\\\\"):gsub("'", "'\\''"):gsub('"', '\\"') end local grab_major_os_version = function()