r/applescript • u/Extreme_Commercial_2 • Nov 30 '22
Cannot create aliases if a subfolder in the target folder shares the same name
Hey all,
I'm trying to write a script that will help organize my sample libraries for easier use within Ableton. With my current script I am able to create aliases at the target folder, but if a subfolder in the target folder shares the same name (ie. samples) I'm being given an error code.
I would like it if when its creating the aliases in the target folder that it would just append or add the aliases into subfolders if the name already exists, and if the name doesn't exist to create that subfolder.
on run
set SourceFolder to choose folder with prompt "Select a folder to create an alias of:" as string
my MainScript(SourceFolder)
my EndAlias()
end run
on open of SourceFolderList
repeat with ThisFolder in the SourceFolderList
set ThisFolderPath to ThisFolder as string
if last character of ThisFolderPath is ":" then
my MainScript(ThisFolderPath)
end if
end repeat
end open
on MainScript(SourceFolder)
tell application "Finder"
set SourceFolder to folder (SourceFolder)
set the TargetFolder to ("Elation:Music Production:Native Instruments:NI Sample Packs" as alias)
end tell
if SourceFolder is not "" and TargetFolder is not "" then
set NewFolderName to (name of SourceFolder as string)
set CreatedFolder to CreateNewFolder(TargetFolder, NewFolderName)
my DuplicateFolderStructure(SourceFolder, CreatedFolder)
end if
end MainScript
on DuplicateFolderStructure(SourceFolder, TargetFolder)
tell application "Finder"
try
set NewAliases to every file of SourceFolder
make new alias file at TargetFolder to NewAliases
set FolderList to every folder of SourceFolder
repeat with ThisFolder in the FolderList
set CreatedFolder to my CreateNewFolder(TargetFolder, get name of ThisFolder)
my DuplicateFolderStructure(ThisFolder, CreatedFolder)
end repeat
end try
end tell
end DuplicateFolderStructure
on CreateNewFolder(TargetFolder, NewFolderName)
tell application "Finder"
try
if not (exists folder NewFolderName) then
set the NewFolder to make new folder at TargetFolder with properties {name:NewFolderName}
else
set the NewFolder to folder NewFolderName
end if
return NewFolder
end try
end tell
end CreateNewFolder
on EndAlias()
display dialog "Aliases Created" buttons {"ok"}
end EndAlias
3
Upvotes
View all comments
Show parent comment
1
u/ChristoferK Dec 02 '22
Ah. This was my first suspicion because of the error numbers observed. Error numbers that start from
-10000are the worst. They infer that there's nothing wrong with the code per se, but that AppleScript just can't execute it, i.e. a bug in AppleScript. With each major version of macOS, these errors crop up more and more as the operating system is updated, but AppleScript isn't (and rarely is, and likely never will be).On Monterey, the script runs well. So there may be some fundamental changes to Finder that now make certain AppleScript functions either behave differently, or not work at all.
However, it's very possible that this is the issue:
This isn't a valid HFS path. Or, rather, it is, but it refers to an external drive called
"Users", on which the subfolders"gavin","Desktop", and so on reside. I presume this isn't what you meant.The first component in the HFS path needs to be your primary boot disk's name, e.g.
But, the simplest way to assure a correct path is like this:
When I used a similarly erroneous path on my system, I get an identical result to yours: the folder is created in the right place (which is a bit weird actually, but nevermind), but none of the subfolders are, and the error thrown is as you described, highlighting those same lines.
So give it a try with a correct path, and I'll keep my fingers crossed.