I have a nice script for creating disk images. The applescript has a nice gui, and it uses the hdiutil command utility to create the script, after prompting the user for all important info.
Well today I found out that if there is a broken alias anywhere in the source folder, hdiutil just… fails. Complete abort!
Pure madness. Is there any way I can pre-scan a folder, and all sub-folders, to check for broken aliases? That way I could do that before trying to actual create the disk image, so I can have a software failure instead of a hard failure?
First, is your disk indexed? You could use spotlight to find files that are aliases. You can also include that in your applescript. This would be the easiest way to find any alias files. You could put this in a do shell script command. Applescript can then find which files lack an ‘original item’.
mdfind "kMDItemFSTypeCode == 'alis'"
Second, what would you want the script to do with them? There are some threads here that discuss finding and repairing broken aliases.
Also, please provide the error message that you get.
1008com. I assume you’re referring to broken alias files, which are often simply referred to as aliases. With that caveat, I’ve included below an ASObjC script that will do what you want and that worked without issue on my Tahoe computer. It’s a minor rewrite of a script by Nigel (here). My script may be overkill for your purposes, and, if so, the linked thread also contains a Finder script that can be edited to do what you want.
use framework "Foundation"
use scripting additions
--prompt for and get contents of source folder
set theFolder to current application's |NSURL|'s fileURLWithPath:(POSIX path of (choose folder))
set fileManager to current application's NSFileManager's defaultManager()
set folderContents to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:6 errorHandler:(missing value))'s allObjects() --option 6 skips package contents and hidden files
--find broken alias files and add to an array
set brokenAliasArray to current application's NSMutableArray's new()
set aliasKey to current application's NSURLIsAliasFileKey
set booleanTrue to current application's NSNumber's numberWithBool:true --an optimization
repeat with anItem in folderContents
set {theResult, anAlias} to (anItem's getResourceValue:(reference) forKey:(aliasKey) |error|:(missing value))
if anAlias is booleanTrue then
set aURL to (current application's NSURL's URLByResolvingAliasFileAtURL:(anItem) options:0 |error|:(missing value)) -- change option 0 to 768 to prevent UI feedback and volume mounting when aliases are resolved
if aURL is (missing value) then (brokenAliasArray's addObject:anItem)
end if
end repeat
--make array into string and display dialog
set brokenAliasString to ((brokenAliasArray's valueForKeyPath:"path")'s componentsJoinedByString:linefeed) as text
if brokenAliasString is "" then set brokenAliasString to "None found"
display dialog "Broken Alias files in the selected folder are listed below. Do you want to move them to the Trash?" & linefeed & linefeed & brokenAliasString default button 1
--move broken alias files to the Trash
repeat with aURL in brokenAliasArray
(fileManager's trashItemAtURL:(aURL) resultingItemURL:(missing value) |error|:(missing value))
end repeat
BTW, I created a disk image of a folder with broken alias files using the hdiutil utility, and the disk image was created without issue. I wonder why we’re seeing different results.
The following script uses Finder to get broken alias files. It works fine if the number of files in the source folder are small but otherwise is too slow.
--prompt for the source folder
set theFolder to choose folder with prompt "Select a folder that will be searched for broken alias files"
--get and make a string of all broken alias files in the source folder
set brokenAliases to ""
tell application "Finder"
try
set aliasFiles to every alias file of the entire contents of theFolder as alias list
on error
display dialog "No alias files were found in the selected folder" buttons {"OK"} cancel button 1 default button 1
end try
repeat with aFile in aliasFiles
if not (exists original item of aFile) then set brokenAliases to brokenAliases & (POSIX path of aFile) & linefeed
end repeat
end tell
--display the broken alias files
if brokenAliases = "" then set brokenAliases to "None Found"
display dialog "Broken alias files in the selected folder are:" & linefeed & linefeed & brokenAliases buttons {"OK"} default button 1
# Note: this script is a rewrite of a script posted by KniazidisR at https://www.macscripter.net/t/remove-dead-aliases-from-directory-and-from-its-subdirectories/71574