I’m struggling to edit a script to work in the way I require.
The problem:
I have folders that are supplied to me in a master folder named (for example) SUPPLIER1 and within this folder loose are files named (for example):
712037 - HD4859.pdf
85698 - FSH56.ai
5269 - the new files.eps, etc etc.
What I want to do is by dropping the above master folder onto the script for it to create folders from the filenames, but renaming everything after the “-” as the master folder. So the above would create folders as such:
712037 - SUPPLIER1
85698 - SUPPLIER1
5269 - SUPPLIER1
I would like the loose files to be moved into their relevant newly created folders but their original filenames maintained.
I have a script with creates folders from filenames and works really well but that’s as far as I’ve got.
Script below:
on adding folder items to theFolder after receiving theFiles
repeat with aFile in theFiles
tell application "System Events" to set {Nm, Ex, pPath} to aFile's {name, name extension, POSIX path of container}
set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
set thePath to (pPath & "/" & BN & "/" as text)
do shell script "mkdir -p " & quoted form of thePath
delay 0.5
tell application "Finder" to move aFile to POSIX file thePath
end repeat
Dave. I’m not knowledgeable about Folder Action Scripts and can’t help with that part of your request. The following–which utilizes a choose-folder dialog–will create the new folders and move the files as you want. It overwrites existing files in the destination folder.
set theFolder to choose folder
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" - "}
tell application "Finder"
set theFiles to every file in theFolder as alias list
repeat with aFile in theFiles
set aFileName to name of aFile
if aFileName contains " - " then
set {aPath, aFolder} to {container, name of container} of aFile
set newFolder to (text item 1 of aFileName & " - " & aFolder)
try
make new folder at aPath with properties {name:newFolder}
end try
move aFile to folder ((aPath as text) & newFolder) with replacing
end if
end repeat
end tell
set AppleScript's text item delimiters to ATID
Would you mind enclosing AppleScript code in [applescript] and [/applescript] tags when posting it here? They make it appear in a box with a clickable link, as in peavine’s reply above. There’s a button for them above the text window when you post.
Here is the Folder Action version of Peavine’s script:
-- TESTING/DEBUGGING part
-- global theFolder, theFiles, aFileName, newFolder, aPath
-- set theitems to {alias "HARD_DISK:Users:123:Desktop:SUPPLIER1:"}
-- set thisHotFolder to alias "HARD_DISK:Users:123:Desktop:thisHotFolder:"
-- adding folder items to thisHotFolder after receiving theitems
on adding folder items to thisHotFolder after receiving theitems
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {" - "}
set theFolder to item 1 of theitems
tell application "Finder"
-- check if the dropped item is FOLDER indeed
try
set theFolder to folder (theFolder as text)
on error -- the dropped item is not folder
return
end try
set theFiles to every file in theFolder as alias list
repeat with aFile in theFiles
set aFileName to name of aFile
if aFileName contains " - " then
set {aPath, aFolder} to {container, name of container} of aFile
set newFolder to (text item 1 of aFileName & " - " & aFolder)
try
make new folder at aPath with properties {name:newFolder}
end try
move aFile to folder ((aPath as text) & newFolder) with replacing
end if
end repeat
end tell
set AppleScript's text item delimiters to ATID
end adding folder items to
Just for learning purposes, I rewrote my script in Post 2 using ASObjC. It works the same as the previous script but should be a bit faster. As before, existing files in the destination folders are overwritten.
use framework "Foundation"
use scripting additions
on main()
set theFolder to (choose folder)
set theFiles to getFiles(theFolder)
repeat with aFile in theFiles
set aFile to (current application's NSString's stringWithString:(POSIX path of aFile))
set aFilePath to aFile's stringByDeletingLastPathComponent()
set aFileName to aFile's lastPathComponent()
set aFileNameStart to item 1 of parseText(aFileName, " - ")
set aWorkingFolder to aFilePath's lastPathComponent()
set aDestinationFolder to current application's NSString's stringWithFormat_(("%@/%@ - %@/"), (aFilePath as text), aFileNameStart, aWorkingFolder) as text
makeFolder(aDestinationFolder)
moveFile(aFile as text, aDestinationFolder & aFileName)
end repeat
end main
on getFiles(sourceFolder)
set fileManager to current application's NSFileManager's defaultManager()
set theFolder to current application's |NSURL|'s fileURLWithPath:(POSIX path of sourceFolder)
set folderContents to fileManager's contentsOfDirectoryAtURL:theFolder includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
set thePred to current application's NSPredicate's predicateWithFormat:("lastPathComponent CONTAINS ' - ' and pathExtension != ''")
set theFiles to folderContents's filteredArrayUsingPredicate:thePred
return theFiles as list
end getFiles
on parseText(theString, theDelimiter)
set theString to current application's NSString's stringWithString:theString
set theList to (theString's componentsSeparatedByString:theDelimiter)
return theList as list
end parseText
on makeFolder(theFolder)
set fileManager to current application's NSFileManager's defaultManager()
if (fileManager's fileExistsAtPath:theFolder) then return false
set theResult to fileManager's createDirectoryAtPath:theFolder withIntermediateDirectories:false attributes:(missing value) |error|:(missing value)
if (theResult as boolean) is false then errorDialog("An error occurred while creating a folder")
return true
end makeFolder
on moveFile(sourceFile, destinationFile)
set fileManager to current application's NSFileManager's defaultManager()
if (fileManager's fileExistsAtPath:destinationFile) then (fileManager's removeItemAtPath:destinationFile |error|:(missing value))
set theResult to fileManager's moveItemAtPath:sourceFile toPath:destinationFile |error|:(missing value)
if (theResult as boolean) is false then
errorDialog("An error occurred during the move process")
error number -128
end if
return true
end moveFile
on errorDialog(dialogText)
display dialog dialogText buttons {"OK"} default button 1 cancel button 1 with title "" with icon stop
end errorDialog
main()