Hello good peoples of macscripter.net :lol:
I came across (read: hijacked and slightly modified) a script that seemed useful to help me take multiple files using a naming convention and parse them by renaming them and moving them into partially-eponymous directories.
– can handle files with and without extensions
– can be modified for a variety of delimiters and naming conventions
A lot to unpack here though - I have many questions being a student of AppleScript.
The overall purpose of the script is to move and rename all my file-variations to subfolders with those subfolders being eponymous to the variation within the name of the file itself.
example of files names: _.
financials_publicview.rtf
financials_serviceview.rtf
financials_customerview-rtf
financials_managementview.rtf
example of folder names and resulting files:
/…//publicview/financials.rtf
/…//serviceview/financials.rtf
/…//customerview/financials.rtf
/…//managementview/financials.rtf
… the script assumes much about the file nomenclature: that the name of the subfolder is always the portion between the (last) delimiter and the dot before the file’s extension, and that there is always at least one delimiter in the file name. *I am sure this can be adjusted to several file naming conventions, delimiters, and so on…
Per the original, this script uses the command line interface ditto which is able to create intermediate directories on-the-fly. It separates name and extension of each selected file and strips the subfolder name from the file name. It’s not necessary to create the subfolders “manually”.
(*
A variation on this most excellent script found here: https://stackoverflow.com/questions/36051457/applescript-rename-and-move-organize-files-to-subfolders
*)
tell application "Finder"
set workingFiles to selection
if workingFiles is {} then return
set destinationDirectory to POSIX path of (choose folder with prompt "Select a destination for the renamed file(s):")
(*
if you only wanted to use the same folder as selected items, use this:
set destinationDirectory to POSIX path of (container of item 1 of workingFiles as text)
*)
end tell
(*
Delimiters assumptions: that you're naming your file variations with the convention <name>_<variation>.extension where the extension is optional for this script. You can modify the script to suit your own conventions if needed.
*)
set {TID, text item delimiters} to {text item delimiters, "_"}
repeat with fileIndexer in workingFiles
set {workingFileName, workingfileExtension} to NameExtensionParser(fileIndexer)
tell text items of workingFileName to set {prefix, suffix} to {items 1 thru -2 as text, item -1}
set newFilePath to quoted form of (destinationDirectory & suffix & "/" & prefix & workingfileExtension)
set sourceFile to quoted form of POSIX path of (fileIndexer as text)
do shell script "/usr/bin/ditto " & sourceFile & space & newFilePath
(*
append this to the shell script statement if you would like to remove source file: & "; /bin/rm " & sourceFile.
*)
end repeat
set text item delimiters to TID
-- Handler defined for splitting files into names and extensions
on NameExtensionParser(meatball)
set {name:workingFileName, name extension:workingfileExtension} to meatball
if workingfileExtension is missing value then return {workingFileName, ""}
return {text 1 thru ((count workingFileName) - (count workingfileExtension) - 1) of workingFileName, "." & workingfileExtension}
end NameExtensionParser
Question #1
How can I source files by a “choose folder” dialog (much like I can choose a destination folder as the parent directory for final results?) I have tried several iterations of statements… as shown below:
“set workingFiles to every item of (choose file with prompt “Choose the file(s) you would like to move:” with multiple selections allowed) as ?” (where ? == list, item, POSIX file, etc…)
– set workingFiles to every POSIX file of (choose file with prompt “Choose the file(s) you would like to move:” with multiple selections allowed)
…Yet, I just can’t get the right statement working as aliases seem to plague the script and trip errors. If I simply run this script - as is above - after selecting files in the Finder, I have no issues - this script works as published.
Question #2, parts a, b and c
For the sake of being as student learning the AS-language, can someone walk me through what these statements and expressions are actaullly doing:
a) “if workingFiles is {} then return”
who dat? I have a hunch that this statement helps with none vs one vs many files selected… but can someone elaborate on what is actually happening? returning the empty set?
b) “tell text items of workingFileName to set {prefix, suffix} to {items 1 thru -2 as text, item -1}”
– here I am confused on text items, the list that is set, and the manipulations to the list (e.g. “items 1 thru -2”, “item -1”). I am not sure of how to understand the mechanics what is happening with these expressions - can someone walk me through this?
c) from the handler: “return {text 1 thru ((count workingFileName) - (count workingfileExtension) - 1) of workingFileName, “.” & workingfileExtension}”
whaaa?? returning by counting through what exactly? (text? 1 thru… ) and why offer up the “-1” for the extension aspect of this? Am I missing the significance of the “text” class?
I appreciate the genius of all of you… thanks in advance for any help you are able to provide.
Model: Crop Circle Mac running on a Infinity Stone
AppleScript: 2.7
Browser: Firefox 57.0
Operating System: Mac OS X 10.13.2