Saturday, May 25, 2019
  • Index
  •  » Code Exchange
  •  » Remove Dead Aliases from Directory and from it's SubDirectories

#1 2019-04-14 04:22:19 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 126

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.

Applescript:



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

Last edited by KniazidisR (2019-04-14 06:10:07 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#2 2019-05-16 05:36:14 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4870

Re: Remove Dead Aliases from Directory and from it's SubDirectories

KniazidisR wrote:

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.


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

Applescript:


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


NG

Offline

 

#3 2019-05-16 09:39:52 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 126

Re: Remove Dead Aliases from Directory and from it's SubDirectories

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.

Last edited by KniazidisR (2019-05-16 09:41:12 am)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#4 2019-05-16 05:22:18 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5658

Re: Remove Dead Aliases from Directory and from it's SubDirectories

KniazidisR wrote:

As I read, Apple plans to abandon ASObjC



Where did you read that?

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2019-05-17 11:10:02 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 126

Re: Remove Dead Aliases from Directory and from it's SubDirectories

Shane Stanley wrote:



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.


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 
  • Index
  •  » Code Exchange
  •  » Remove Dead Aliases from Directory and from it's SubDirectories

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)