Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Select a song from VLC and run this Applescript to Add a song to iTunes, Trash it from its original location, and then removing it from the VLC Playlist. It has only been tested for a single song selection but can accomodate playlists of any size.
global songAdded
global songDeleted
global selectedSong
global safetyCheck
# This variable is used as a boolean to see if a file has been added to iTunes or not
set songAdded to false
# This variable is used as a boolean to see if a file has been deleted or not
set songDeleted to false
# This variable is used to store the alias location of the song to be deleted / added
set selectedSong to false
# This variable is used to track whether we've attempted a safe workaround to deleting a current song
set safetyCheck to false
getSong()
addSong(selectedSong)
deleteSong(selectedSong)
finishUp()
on getSong()
# Reveal selected song from the VLC playlist in the Finder
tell application "VLC"
activate
try
tell application "System Events" to keystroke "r" using {shift down, command down}
end try
end tell
# save the selected song
if is_active("Finder", true) is true then
tell application "Finder"
set selectedSong to (get selection as alias)
end tell
end if
end getSong
# Add Song to iTunes Library
on addSong(selectedSong)
# Bail out if there's no saved selected song
if selectedSong is not false then
tell application "iTunes"
launch
end tell
if is_running("iTunes", true) is true then
try
tell application "iTunes"
add selectedSong to playlist "Music" of source "Library"
set songAdded to true
end tell
end try
end if
end if
end addSong
# Trash the song from the Finder
on deleteSong(selectedSong)
# Only continue if there is a selected song that has been properly saved and that we've already added it to iTunes
if selectedSong is not false and songAdded is true then
# Only delete the file if the file is safe to delete (Not currently Playing)
if safeSongCheck(selectedSong) is true then
try
# Delete the Selected file
tell application "Finder" to delete selectedSong
delay 0.1
set songDeleted to true
end try
else
attemptSafety()
end if
end if
end deleteSong
on finishUp()
tell application "Finder" to close window 1
tell application "VLC" to activate
# Delete the selected song from the playlist if it has been deleted
if songDeleted is true then
if is_active("VLC", true) is true then
try
# Press Delete Key
tell application "System Events" to key code 51
delay 0.1
if safetyCheck is true then
ignoring application responses
tell application "VLC"
play
delay 0.1
end tell
try
tell application "System Events"
tell its application process "VLC"
tell its window "Open Source"
click button "Cancel"
end tell
end tell
end tell
end try
end ignoring
end if
end try
end if
end if
end finishUp
on safeSongCheck(selection)
set currentSongPath to currentSongPath()
try
tell application "Finder"
# If it is currently Playing the song, it is NOT safe.
# If the Path of the currently playing song in VLC (which is a POSIX Path)
# matches the POSIX path of the selection, it is NOT safe.
if POSIX path of selection is currentSongPath then
return false
else
return true
end if
end tell
end try
end safeSongCheck
on attemptSafety()
if songDeleted is false then
# Can't delete a currently playing song
# Try and switch songs and select the previous song we tried to delete
tell application "VLC" to activate
if safetyCheck is false then
try
tell application "VLC" to stop
delay 0.1
set safetyCheck to true
deleteSong(selectedSong)
end try
end if
end if
end attemptSafety
# Utility functions
# Return the path of current playing song otherwise return false
on currentSongPath()
tell application "VLC"
try
if playing then
return path of current item
else
return false
end if
end try
end tell
end currentSongPath
on is_running(appName, waitBoolean)
tell application "System Events"
set appRunning to (name of processes) contains appName
if waitBoolean is true then
# Wait for the App to start running
set wait to 0
repeat until appRunning is true or wait is greater than 8
set appRunning to (name of processes) contains appName
set wait to wait + 0.1
delay 0.1
end repeat
end if
return appRunning
end tell
end is_running
on is_active(appName, waitBoolean)
tell application "System Events"
set activeApp to (name of first application process whose frontmost is true) contains appName
if waitBoolean is true then
# Wait for the App to become active
set wait to 0
repeat until activeApp is true or wait is greater than 2
set activeApp to (name of first application process whose frontmost is true) contains appName
set wait to wait + 0.1
delay 0.1
end repeat
end if
return activeApp
end tell
end is_active
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment