Hey all,
I need help converting a string to a track property that will help my current code.
I have a display dialog showing what I am attempting to do, these are all strings.
Potentially there is a better way to achieve the same result, maybe scripting o’s property which I need to learn when to invoke.
In the past, I’ve been relying on many lines of code to filter out text if the item begins with, contains, or ends with before I use ASOC in a subroutine. I’m attempting to do the filtering process using strings with repeat loops here which will help de-duplicate many lines of code.
I’m only focusing on 3 track tags here, but many more will be added. The idea is to remove domain names through ASOC after I have filtered results by text.
use framework "Foundation"
use scripting additions
tell application "Music"
set trackID to (get persistent ID of every file track of library playlist 1)
set trackName to (get name of every file track of library playlist 1)
set trackArtist to (get artist of every file track of library playlist 1)
set trackComment to (get comment of every file track of library playlist 1)
repeat with a from 1 to count of the trackID
set listBeginsWith to {" "}
set listContains to {" ", "www.", "http"}
set listEndsWith to {".com", ".net", ".org", ".co", ".su", ".xyz", ".club", ".me", ".ro", ".biz", ".info", ".pro", ".us", " ", "-"}
--set trackTag to {{name:item a of trackName}, {artist:item a of trackArtist}, {comment:item a of trackComment}}
set trackTag to {{"name", item a of trackName}, {"artist", item a of trackArtist}, {"comment", item a of trackComment}}
repeat with thisTag in trackTag
repeat with thisBeginsWith in listBeginsWith
if item 2 of thisTag begins with thisBeginsWith then
display dialog "PROPERTY: " & item 1 of thisTag & return & "TAG: " & item 2 of thisTag & return & "BEGINS WITH: " & thisBeginsWith
set item 1 of thisTag of (some track of library playlist 1 whose persistent ID is item a of trackID) to my cleanUpText(item 2 of thisTag)
end if
end repeat
repeat with thisContains in listContains
if item 2 of thisTag contains thisContains then
display dialog "PROPERTY: " & item 1 of thisTag & return & "TAG: " & item 2 of thisTag & return & "CONTAINS: " & thisContains
set item 1 of thisTag of (some track of library playlist 1 whose persistent ID is item a of trackID) to my cleanUpText(item 2 of thisTag)
end if
end repeat
repeat with thisEndsWith in listEndsWith
if item 2 of thisTag ends with thisEndsWith then
display dialog "PROPERTY: " & item 1 of thisTag & return & "TAG: " & item 2 of thisTag & return & "ENDS WITH: " & thisEndsWith
set item 1 of thisTag of (some track of library playlist 1 whose persistent ID is item a of trackID) to my cleanUpText(item 2 of thisTag)
end if
end repeat
end repeat
end repeat
end tell
on cleanUpText(someText)
set theNSString to current application's NSString's stringWithString:someText
set theOptions to (current application's NSRegularExpressionSearch as integer) + (current application's NSRegularExpressionAnchorsMatchLines as integer) + (current application's NSRegularExpressionCaseInsensitive as integer)
set theNSString to theNSString's stringByReplacingOccurrencesOfString:"(http.*)?(www\\.)?[\\w]+\\.(com|net|org|co|su|xyz|club|me|ro|biz|info|pro|us)" withString:"" options:theOptions range:{location:0, |length|:theNSString's |length|()}
set theNSString to theNSString's stringByReplacingOccurrencesOfString:"^[\\d]+\\. +" withString:"" options:theOptions range:{location:0, |length|:theNSString's |length|()}
set theNSString to theNSString's stringByReplacingOccurrencesOfString:"-( +)?$" withString:"" options:theOptions range:{location:0, |length|:theNSString's |length|()}
set theNSString to theNSString's stringByReplacingOccurrencesOfString:" +" withString:" " options:theOptions range:{location:0, |length|:theNSString's |length|()}
set theWhiteSet to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
set theNSString to theNSString's stringByTrimmingCharactersInSet:theWhiteSet
return theNSString as text
end cleanUpText