Skip to content

Instantly share code, notes, and snippets.

@ingmar
Created October 30, 2018 23:18
Show Gist options
  • Save ingmar/a48e9e223cdcc4c804a1e09ae7cc77df to your computer and use it in GitHub Desktop.
Save ingmar/a48e9e223cdcc4c804a1e09ae7cc77df to your computer and use it in GitHub Desktop.
Make syncthing sync only the songs in my Library that are in an iTunes playlist
// Include files in this file - each line must start with an exclamation mark "!"
#include syncthis.txt
// Exclude everything by default
*
set myPlaylist to "5-4★ Rated"
on splitStr(theString, theDelimiter)
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theArray to every text item of theString
set AppleScript's text item delimiters to oldDelimiters
return theArray
end splitStr
on joinStr(theArray, theDelimiter)
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to theDelimiter
set theString to theArray as text
set AppleScript's text item delimiters to oldDelimiters
return theString
end joinStr
-- Get iTunes Library path
use framework "iTunesLibrary"
tell application "iTunes"
set lib to current application's ITLibrary's libraryWithAPIVersion:"1.0" |error|:(missing value)
set mediaFolderPath to POSIX path of (lib's mediaFolderLocation as alias)
end tell
-- Get file paths from Playlist
set dn to {}
tell application "iTunes"
set pl to user playlist myPlaylist
repeat with i from 1 to count tracks of pl
set tr to track i of pl
set af to location of tr
set uf to POSIX path of af
set prefixlen to length of mediaFolderPath
set sf to characters (prefixlen + 1) through -1 of uf as string
copy "!" & sf to the end of dn
-- This is where it gets horrible, because of how .stignore works
set pp to my splitStr(sf, "/")
repeat while length of pp > 1
set pp to items 1 thru -2 of pp
-- exclude all other contents of it not expliciately mentioned
copy my joinStr(pp, "/") & "/*" to the end of dn
-- but implicitly include the directory
copy "!" & my joinStr(pp, "/") to the end of dn
end repeat
end repeat
end tell
-- Open output in a TextEdit document
set text item delimiters to linefeed
tell application "TextEdit"
activate
set doc to make new document with properties {text:dn as text}
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment