Remove Dead Aliases from Directory and from it's SubDirectories

Many applications and services create a bunch of dead aliases on your computer online. Is recomended before running this script to temporarily turn off the Internet. Although the script works with the Internet on too.

It is better do not choose System folder, or Startup Disk, because 1) the script consumes a lot of time 2) you’ll get annoyed with the system warnings “Cannot be modified … because it is required by the OS”. The best choice is a Home folder. Therefore, for greater practicality, it is better to just replace code line:
set the_folder to choose folder
with this:
set the_folder to (path to home folder).

By the nature of the problem being solved, the script consumes several minutes for the HOME folder. But you just need to run it once a week, or once a month. This will save your disk from garbage and reduce its fragmentation. Also, now I’m looking for a way to increase the speed of the script. If someone helped supply this script with Swift fragments, I would be grateful. Or, completely rewrite it as Swift. Well, I’ll try to do it myself too.



global cleaned_Alias

set cleaned_Alias to 0
set the_folder to choose folder
clean_alias_files_in_folder(the_folder)
cleaned_Alias

on clean_alias_files_in_folder(the_folder)
	tell application "Finder"
		
			--Check each of the alias files in this disk/folder
			set alias_files_list to alias files of the_folder
			repeat with the_alias_file_ref in alias_files_list
				if not (exists original item of the_alias_file_ref) then
					delete the_alias_file_ref
					set cleaned_Alias to cleaned_Alias + 1
				end if
			end repeat
			--Clean the alias files in each sub-folder
			set sub_folders_list to folders of the_folder
		
		repeat with the_sub_folder_ref in sub_folders_list
			my clean_alias_files_in_folder(the_sub_folder_ref)
		end repeat
	end tell
end clean_alias_files_in_folder


Here’s an ASObjC version, if it’s any help:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set the_folder to choose folder
set cleaned_Alias to clean_alias_files_in_folder(the_folder)

on clean_alias_files_in_folder(the_folder)
	set |⌘| to current application
	
	-- Preset a few variables.
	set folderURL to |⌘|'s class "NSURL"'s fileURLWithPath:(POSIX path of the_folder)
	set fileManager to |⌘|'s class "NSFileManager"'s defaultManager()
	set aliasKey to |⌘|'s NSURLIsAliasFileKey
	set noPackageContents to |⌘|'s NSDirectoryEnumerationSkipsPackageDescendants
	set noHiddenFiles to |⌘|'s NSDirectoryEnumerationSkipsHiddenFiles
	
	-- Get the entire contents of the folder as an array of NSURL, skipping package contents and hidden files and noting whether or not the items are alias files.
	set entireContents to (fileManager's enumeratorAtURL:(folderURL) includingPropertiesForKeys:({aliasKey}) options:(noPackageContents + noHiddenFiles) errorHandler:(missing value))'s allObjects()
	
	-- These two options prevent alias resolution attempts from giving UI feedback or mounting volumes. Change the values to 0 if not wanted.
	set noUIFeedback to |⌘|'s NSURLBookmarkResolutionWithoutUI
	set noVolumeMounting to |⌘|'s NSURLBookmarkResolutionWithoutMounting
	
	-- Work through the URL array.
	set removalCount to 0
	repeat with thisURL in entireContents
		-- Is this item an alias file?
		set {success, isAlias} to (thisURL's getResourceValue:(reference) forKey:(aliasKey) |error|:(missing value))
		-- If it is and its original item can't be located, move it to the trash and increment the removal count.
		if ((isAlias as boolean) and ((|⌘|'s class "NSURL"'s URLByResolvingAliasFileAtURL:(thisURL) options:(noUIFeedback + noVolumeMounting) |error|:(missing value)) is missing value)) then
			tell fileManager to trashItemAtURL:(thisURL) resultingItemURL:(missing value) |error|:(missing value)
			-- Or, use this line instead to zap the alias completely:
			-- tell fileManager to removeItemAtURL:(thisURL)
			set removalCount to removalCount + 1
		end if
	end repeat
	
	return removalCount
end clean_alias_files_in_folder

Thank you very much, Nigel. That’s what I need. While I study Swift, I’ll probably grow old … I’ve found something here: how to combine pure Applescript and Swift using REPL, but for now it’s not clear. As I read, Apple plans to abandon ASObjC and this is a cause for concern.

Where did you read that?

It’s likely to happen the day they abandon AppleScript itself, and not before.

Well, it appears different gossip on the net. “Apple plans to replace with Swift …”. In fact, I also find it difficult to believe that Apple will abandon its successful projects such as AppleScript and
ASObjC. It would simply be an illogical and unwise step.