Here is my goal
Make these file names:
FC_AUPM116_9_New_Horizons_Instrumental_Johnson_Tschallener_1160140.wav_Stereo.aif
FC_AUPM119_12_A_Broken_Heart_Instrumental_Webster_1189757.wav_Stereo.aif
FC_EN11_6_Honour_Among_Thieves_Instrumental_Whittaker_Gilbey_1229000.wav_Stereo.aif
FC_EN12_7_Constantly_Searching_Instrumental_Gregory_1229274.wav_Stereo.aif
This:
AUPM116_9_New_Horizons
AUPM119_12_A_Broken_Heart
EN11_6_Honour_Among_Thieves
EN12_7_Constantly_Searching
Here is what I get:
AUPM116 9 New_Horizons
AUPM119 12
EN11 6 Honour Among Thieves
EN12 7 Constantly Searching
“A” from A_Broken_Heart thinks its in the variable delete_words
set delete_words to “Main, Track, Alternative, Version, Vocal, Instrumental, underscore, backing”
Here is my code:
set FC_music_tracks to every file in folder merge_folder whose name starts with "FC"
set all_FC to ""
set new_fc_name to ""
set delete_words to "Main, Track, Alternative, Version, Vocal, Instrumental, underscore, backing"
set AppleScript's text item delimiters to "_"
repeat with a_fc_cut in FC_music_tracks
set AppleScript's text item delimiters to "_"
set fc_name to name of a_fc_cut
set text_items to every text item of fc_name
repeat with a_text_item in text_items
if a_text_item contains "FC" then
else
if a_text_item is in delete_words then — THIS WILL EXIT IF a_text_item IS “A”
exit repeat
else
set new_fc_name to new_fc_name & a_text_item & " "
end if
end if
end repeat
--display dialog new_fc_name as string -- THIS WORKS TO HERE
if new_fc_name is not in all_FC then
set all_FC to all_FC & new_fc_name & tab & tab & "39s" & return
end if
set new_fc_name to ""
end repeat
set AppleScript's text item delimiters to ""
any ideas are more than welcome.
This is a solution with help of ApplScriptObjC and Regular Expression.
The benefit of Regular Expression is the ability to search for all delete_words in one expression.
The code searches for the end of FC_ and the beginning of _ plus one of the delete_words. Then it extracts the substring between the bounds.
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
property regularExpression : 1024
property caseInsensitive : 1
tell application "Finder" to set FC_music_tracks to every file in folder merge_folder whose name starts with "FC"
set all_FC to ""
set new_fc_name to ""
set excludePattern to "_(Main|Track|Alternative|Version|Vocal|Instrumental|underscore|backing)"
repeat with a_fc_cut in FC_music_tracks
set fc_name to name of a_fc_cut
set nsFileName to (current application's NSString's stringWithString:fc_name)
set lowerBound to (nsFileName's rangeOfString:"FC_" options:(regularExpression + caseInsensitive))'s |length|()
set upperBound to (nsFileName's rangeOfString:excludePattern options:(regularExpression + caseInsensitive))'s location()
set new_fc_name to (nsFileName's substringWithRange:{location:lowerBound, |length|:(upperBound - lowerBound)}) as text
if new_fc_name is not in all_FC then
set all_FC to all_FC & new_fc_name & tab & tab & "39s" & return
end if
set new_fc_name to ""
end repeat
Please avoid to wrap the code in a big tell application block
Here is an pure AppleScript version
set FC_music_tracks to every file in folder merge_folder whose name starts with "FC"
set all_FC to {}
set new_fc_name to ""
set text item delimiters to {"FC_", "_Main", "_Track", "_Alternative", "_Version", "_Vocal", "_Instrumental", "_backing"}
repeat with a_fc_cut in FC_music_tracks
set new_fc_name to text items of name of a_fc_cut
set end of all_FC to item 2 of new_fc_name
end repeat
set end of all_FC to ""
set text item delimiters to tab & tab & "39s" & return
set all_FC to all_FC as text
set text item delimiters to ""
Here’s a minor optimisation of robert’s effort. The names of the files are fetched in bulk and the contents of the repeat are simplified.
tell application "System Events" to set FC_music_tracks to name of every file in folder merge_folder whose name starts with "FC"
set all_FC to {}
set astid to text item delimiters
set text item delimiters to {"FC_", "_Main", "_Track", "_Alternative", "_Version", "_Vocal", "_Instrumental", "_backing"}
repeat with a_fc_cut in FC_music_tracks
set end of all_FC to text item 2 of a_fc_cut
end repeat
set end of all_FC to ""
set text item delimiters to tab & tab & "39s" & return
set all_FC to all_FC as text
set text item delimiters to astid
return all_FC