Sunday, July 5, 2020

#1 2020-05-07 11:35:12 am

sebrady
Member
Registered: 2005-09-13
Posts: 36

trying to parse a file name

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:

Applescript:

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.

Offline

 

#2 2020-05-07 12:23:34 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11688
Website

Re: trying to parse a file name

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.

Applescript:


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

Last edited by StefanK (2020-05-07 12:25:18 pm)


regards

Stefan

Offline

 

#3 2020-05-07 12:53:34 pm

robertfern
Member
Registered: 2011-11-29
Posts: 63

Re: trying to parse a file name

Here is an pure AppleScript version

Applescript:


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 ""

Last edited by robertfern (2020-05-07 01:09:56 pm)

Offline

 

#4 2020-05-07 04:52:28 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5233

Re: trying to parse a file name

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.

Applescript:

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


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)