The Finder
What's in a filename, any filename?
I've had occasion to copy a number of .mp3 files into a folder for later importing into and playing through iTunes. These file names are composed of the name of the song, the name of the artist and other bits all beginning with "--".
The syntax might be described as ::= <tune name> & < - > & <artist> & < -- > & <.mp3>
with the script converting it to ::= <tune name> & < - > & <artist> & < #> & <.mp3>
The < #> is used as a "not yet processed" flag to be removed after final processing.
I want to prepare all song files in a folder ready for loading into iTunes. This short script will do just that. It will only look at file names ending in .mp3, and ignores all other items such as folders. In fact it will only alter file names ending in .mp3 file with the specific syntax given above.
It requires the Satimage OASAX, that is freely available (used for its excellent implementation of rexexp) to be installed. This script could also be used as the basis of a script to replace every file's name ending in .mp3 with a number indexed filename eg. 123.mp3 or any other systematic alteration to the file names in a folder.
Notice the update <folderName> instruction at the end of the script. It causes the current finder display of a folder contents to be synchronised with the on-disk values immediately the change is made to the file name. I discovered this when I had run this script without the immediate-update-after-change. Without closing the finder window, I tried to copy and paste the file names to another folder. What happened surprised me. The finder tried to copy non existent files because it was using the (old) displayed file names! This script could be extended to also import the files into iTunes and then get iTunes to process them even further, using of course, the power of iTunes.
-- Ian W. Parker - first worked on: 2009-04-13, last worked on 2009-04-17 -- Clean up names of .mp3 files -- requires Satimage OSAX for regexp capabilities -- ------------------------------------------------------------------- -- open folder using navigator set work_folder to choose folder with prompt ¬ "Music ..." default location desktop as alias --get list of all files in the folder, and process repeat with file_name in list folder work_folder -- check to see if the filename needs cleaning if file_name ends with ".mp3" and file_name contains "--" then -- identify extent of substring to remove using regexp set target_string to re_compile "--(.*)." syntax "GREP" -- remove target substring set final_string to change target_string into " #" in file_name with regexp -- recompose the filename string set final_string to final_string & ".mp3" -- rename file tell application "Finder" set name of file (work_folder & file_name as string) to final_string update work_folder end tell end if end repeat
AppleScripting - Make Empty Folders
In this example we simply create 4 empty folders in a particular location. Because of the presence of the try...on error..end try block it is possible to run this with SOME folders present. It will make new folders IF they do not already exist. The idea can be extended so that you can pick up the folder names from a text file.
-- -------------------------------------------------------------------------
-- Example - create a set of empty folders from a list, Ian Parker, 2007
-- -------------------------------------------------------------------------
set folderList to {"Folder-01", "Folder-02", "Folder-03", "Folder-04"}
--Choose the destination folder
set rootPath to (choose folder with prompt "Put the empty folders in...")
--Set path to chosen folder
repeat with theItem in folderList
try -- to make a folder if it does not already exist
tell application "Finder"
make new folder at rootPath with properties {name:theItem}
end tell
on error
--
end try
end repeat
AppleScripting - Manually Removing unwanted files from a folder
Sometimes you want to remove files from a folder that contain particular substrings. For example, when copying a series of files, you may wish to delete all files in a folder whose name contains the substring " Advert " or " Promo ".
This is a simple example which can be run from anywhere, and which targets the currently logged-in user's "Music" folder. It's path is known to the system. This small script could also be wrapped in a folder monitor and be automatically performed after each file is added.
-- Remove Ad or Promo files
-- Vers 0.1, Ian W. Parker first worked on 2010-08-28, last worked on 2010-08-28
set items_deleted to 0
set work_folder to path to music folder
-- get list of every file name and determine if it should be sent to the trash
repeat with next_item in list folder work_folder
set {do_delete, a_file} to {false, ((work_folder & next_item) as string)}
if (a_file contains " Advert ") or (a_file contains " Ad.") then set do_delete to true
if (a_file contains " Promo ") then set do_delete to true
-- send the files to the trash (can be retrieved)
if do_delete then
tell application "Finder" to delete (a_file as alias)
set items_deleted to items_deleted + 1
end if
end repeat
-- simple report of how many files were deleted
(items_deleted & " files deleted") as string