how to fix 100's of alias' on mac after migration?

3.1k views Asked by At

I really hope someone can help me out with this. I recently moved from one mac to another and did a clean install. I have several folders with hundreds of alias (how do you say the plural of alias?)...

The original file path is "/Volumes/Media Drive/Ableton/Warped Tracks/", and the new path needs to be "/users/joel/Music/Ableton Projects/Warped Tracks"

I see how to fix them one at a time, but that would take hours. I tried this applescript, but had no luck: https://apple.stackexchange.com/questions/2656/how-do-i-fix-failed-aliases

Can anyone give me a better applescript or another solution? As I mentioned, I tried this applescript:

tell application "Finder"
    set these_items to the selection
end tell

repeat with i from 1 to the count of these_items
    set this_item to (item i of these_items) as alias
    set this_info to info for this_item

    if class of this_item is alias then
        tell application "Finder"
            set original_file to original item of this_item
            set this_alias_file_name to displayed name of this_item
            set container_folder to container of this_item

            set the_path to the POSIX path of (original_file as alias)
            set new_path to my replaceText("/Volumes/Media Drive/Ableton/Warped Tracks/", "/users/joel/Music/Ableton Projects/Warped Tracks", the_path)

            move this_item to trash
            try
                make new alias file at container_folder to (POSIX file new_path) with properties {name:this_alias_file_name}
            on error errMsg number errorNumber
                if errorNumber is -10000 then -- new original file not found, try relinking to old
                    try
                        make new alias file at container_folder to (POSIX file the_path) with properties {name:this_alias_file_name}
                    on error errMsg number errorNumber
                        if errorNumber is -10000 then -- old original not found. link's dead Jim
                            display dialog "The original file for alias " & this_alias_file_name & " was not found."
                        else
                            display dialog "An unknown error occurred:  " & errorNumber as text
                        end if
                    end try
                else
                    display dialog "An unknown error occurred:  " & errorNumber as text
                end if
            end try
        end tell
    end if
end repeat

on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject

    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs

    return subject
end replaceText

Any help would be greatly appreciated.

Edit: I think the problem with the applescript for me is that I have folders further down the path that all are different, and THOSE each contain the individual alias files. IE: "/users/joel/Music/Ableton Projects/Warped Tracks/Folder A/file.alias",/users/joel/Music/Ableton Projects/Warped Tracks/Folder B/file2.alias", etc, etc

1

There are 1 answers

0
pbell On

I had same issue years ago (transfer between 2 media centers), and I made this program : it asks for folder were all broken alias are and then try to find again original path to rebuild them.

the thing is that any applescript instruction about alias gives error when link is broken, so you must use Finder info window to read original path without error when the link is already broken: (this program assumes that original file name is unique)

-- Select main folder
tell application "Finder" to set Mon_Dossier to ((choose folder with prompt "Select top folder:" without invisibles) as alias) as string

-- repair alias in this folder and all subsequent folders (entire contents)
tell application "Finder" to set Mes_Alias to every file of entire contents of folder Mon_Dossier whose class is alias file
-- loop on each alias of the folder
repeat with Mon_Alias in Mes_Alias
    -- found the original file path of Mon_Alias
    -- try first with standard alias call   
    set F_Source to ""
    try
        tell application "Finder" to set F_Source to original item of Mon_Alias
    end try
    if F_Source is "" then
        -- the link to original file is broken, then use on windows info method
        set F_Source to Origine_Alias(Mon_Alias)
        set F_Original to Decompose(F_Source)
        -- no need to look original file as is, becuase the link is broken (path changed ?)
        -- then directly search for the same file without extension
        set Cible to ((chemin of F_Original) as string) & ((Nom of F_Original) as string)
        tell application "Finder"
            if exists file Cible then
                -- file is found without extension
                -- then delete alias and create new alias with same name and in same folder
                set A_Nom to name of Mon_Alias
                set A_Dossier to folder of Mon_Alias
                delete Mon_Alias
                set Nouvel_Alias to make alias to Cible at A_Dossier
                set name of Nouvel_Alias to A_Nom
            else
                SLog("Alias=" & (Mon_Alias as string) & "       File not found=" & Cible)
            end if
        end tell
    else
        -- the alias link is still valid : nothing to do ! go to next alias..
    end if
end repeat
-- end main program


-- sub routine to find passe of broken link (original path/file can't be found)
-- the result is a unix path like folder/sub_folder/file
on Origine_Alias(F_Alias)
    set R to ""
    tell application "Finder" to open information window of file (F_Alias as text)
    tell application "System Events" to tell process "Finder" to set R to value of static text 19 of scroll area 1 of front window
    tell application "Finder" to close front window
    return R
end Origine_Alias


-- sub routine to extract, from unix file path, the path, the file name and its extension: result is sent back in a record
-- Warning : we can't use "Posix file of" becuase the path and the file are non longer valid ! (then Posix gives error)
on Decompose(Local_F)
    --search the first "." from right to find extension
    set X to length of Local_F
    repeat while (character X of Local_F is not ".") and (X > 0)
        set X to X - 1
    end repeat
    if X > 0 then
        set L_Ext to text (X + 1) thru (length of Local_F) of Local_F
        set Local_F to text 1 thru (X - 1) of Local_F
    else
        L_Ext = "" -- "." not found, then no extension !
    end if
    -- search first "/" from the right to extract file name
    set X to length of Local_F
    repeat while (character X of Local_F is not "/")
        set X to X - 1
    end repeat
    set L_Nom to text (X + 1) thru (length of Local_F) of Local_F
    set Local_F to text 1 thru (X) of Local_F
    try
        set L_Chemin to POSIX file Local_F
    end try
    return {chemin:L_Chemin, Nom:L_Nom, Ext:L_Ext}
 end Decompose

-- sub routine log (text log file on desktop)
on SLog(msg)
    set the my_log to ¬
        ((path to desktop) as text) & "My_logfile.txt"
    try
        -- open file. create it if not yet exist
        open for access file the my_log with write permission
        -- add text at end of file
        write (msg & return) to file the my_log starting at eof
        close access file the my_log
    on error
        try
            close access file the my_log
        end try
    end try
end SLog