This discussion is locked
christopher rigby1

Q: Can AppleScript run a FixAlias script?

I have a large folder of pictures on a memory stick - the original folder on my HD also has a folder with a few hundred aliases for some of the pictures, which originally I didn't copy because I thought (mistakenly) there wouldn't be room.

Later I copied the alias folder to the memory stick, but despite the files having the same name, the Apple OS isn't smart enough to simply do a 'fix alias' automatically by assigning the alias (which points to a file on the HD) to the identical filename on the memory stick.

Is there an AppleScript which will do this? (I don't want to have to Fix Alias of 500 files in a folder of 4000+ items).

iMac G5 17" 1.5Gb RAM, Mac OS X (10.4.7)

Posted on Oct 9, 2006 9:32 AM

Close

Q: Can AppleScript run a FixAlias script?

  • All replies
  • Helpful answers

  • by Hiroto,Helpful

    Hiroto Hiroto Oct 10, 2006 4:19 AM in response to christopher rigby1
    Level 5 (7,286 points)
    Oct 10, 2006 4:19 AM in response to christopher rigby1
    Hello

    Try something like this.
    If the alias files in question are valid aliases (i.e. not broken) just pointing to original items in HD, this sort of script may help.

    Good luck,
    H

    -- SCRIPT
    (*
      redirect alias files
      v0.2
      
      This script will redirect alias files in specified folder such that each original item is reassigend by replacing
       its partial leading path given in property 'path1' with the partial path given in property 'path2'.
      
      (Meanwhile any broken aliases of which original items are not found will be moved to '_broken_' folder and
      any aliases of which assumed reassigned original items are not found will be moved to '_failed_' folder.
      And aliases of which original items' path do not start with path given in property 'path1' will remain as they are.)
      
      Usage:
        Change properties path1 and path2 to suit your needs and run the script, and it will
         let you choose a folder where alias files to be redirected reside.
        
        E.g.
        Provided original folders are:
          (1) HD:Users:[user_name]:Documents:Pictures:
          (2) HD:Users:[user_name]:Documents:Pictures:Aliases:  -- (this is irrelevant here)
        and copied folders are:
          (1a) ExtDisk:Pictures:
          (2a) ExtDisk:Pictures:Aliases:  -- (this is assumed to contain 'wrong' alias files pointing to items in (1))

        Then set:
          property path1 : "HD:Users:[user_name]:Documents:Pictures:"
          property path2 : "ExtDisk:Pictures:"
        and in choose folder prompt, choose folder:
          "ExtDisk:Pictures:Aliases:"
    *)

    main()
    on main()
    script o
    property path1 : "RAM Disk:test1:" -- searching leading partial path of original item of alias file.
    property path2 : "RAM Disk:test2:" -- replacing leading partial path of original item of alias file.
    property aa : {} -- list of original alias files.
    property aa1 : {} -- list of broken alias files of which original items are not found.
    property aa2 : {} -- list of failed alias files of which assumed reassigned original items are not found.
    property aa3 : {} -- list of reassigend alias files.
    property nn : {} -- list of name of original alias files.
    property xx : {} -- list of original items of aa
    property yy : {} -- list of reassigned orignal items of aa
    property brokenFdn : "_broken_" -- folder name for aa1.
    property failedFdn : "_failed_" -- folder name for aa2.
    property mss1 : "Choose folder of alias files to be redirected."
    property mss2 : "Alias redirection done."
    on reassign(a)
    (*
      alias a
      property path1 -- searching leading partial path
      property path2 -- replacing leading partial path
      return either alias or false:
        alias a : original alias if no path substitution applied.
        alias a1 : reassigend alias created by a's leading partial path 'path1' being replaced by 'path2'
        false : reassignment failed, i.e. assumed reassigned object not found
    *)
    local p
    if a's class is not alias then return a
    set p to "" & a
    if p starts with path1 then
    set p to path2 & (p's text ((path1's length) + 1) thru -1)
    try
    return (p as alias)
    on error -- assumed reassigned object not found
    return false
    end try
    else
    return a
    end if
    end reassign

    -- (0) accept folder of alias files
    set fda to choose folder with prompt mss1

    -- (1) get list of alias files, their name and original items
    tell application "Finder"
    tell item fda's alias files
    try
    set aa to it as alias list
    on error
    set aa to it as alias as list
    end try
    set nn to name as list
    try
    if (count my aa) = 1 then
    set xx to original item as alias as list
    else
    set xx to original item as alias list
    end if
    on error number -1700 -- some alias file(s) broken
    repeat with a in my aa
    set a to a's contents
    try
    set end of my xx to a's original item as alias
    on error -- broken alias file
    set end of my xx to null
    end try
    end repeat
    end try
    end tell
    end tell

    -- (2) reassign original items
    repeat with x in my xx
    set end of yy to reassign(x's contents)
    end repeat

    -- (3) sort out original alias files and create new ones if necessary
    repeat with i from 1 to count my aa
    set yi to my yy's item i
    if yi is null then -- broken
    set end of my aa1 to my aa's item i
    else if yi is false then -- failed, i.e. assumed reassigned original item not found
    set end of my aa2 to my aa's item i
    else if yi is not my xx's item i then -- reassigned
    set end of my aa3 to my aa's item i
    end if
    end repeat
    if my aa1 is not {} then
    try
    set fda1 to ("" & fda & brokenFdn & ":") as alias
    on error
    tell application "Finder" to ¬
    set fda1 to (make new folder at fda with properties {name:brokenFdn}) as alias
    end try
    tell application "Finder" to move aa1 to fda1
    end if
    if my aa2 is not {} then
    try
    set fda2 to ("" & fda & failedFdn & ":") as alias
    on error
    tell application "Finder" to ¬
    set fda2 to (make new folder at fda with properties {name:failedFdn}) as alias
    end try
    tell application "Finder" to move aa2 to fda2
    end if
    if my aa3 is not {} then
    tell application "Finder" to delete aa3
    -- recreate alias files
    repeat with i from 1 to count my aa
    if (my aa's item i) is in my aa3 then
    tell application "Finder"
    make new alias file at fda to (my yy's item i) with properties {name:my nn's item i}
    end tell
    end if
    end repeat
    end if

    -- (4) post-processings: empty trash (optional) and decent notice
    tell application "Finder"
    --empty trash
    activate
    display dialog mss2 with icon 1 giving up after 5
    end tell
    end script
    tell o to run
    end main
    -- END OF SCRIPT


      Mac OS 9.1.x  
  • by christopher rigby1,

    christopher rigby1 christopher rigby1 Oct 11, 2006 7:11 AM in response to Hiroto
    Level 4 (2,146 points)
    Oct 11, 2006 7:11 AM in response to Hiroto
    Wow - that is one helluva script, well, I did ask! Now (though I am a novice at this) you have given me something to really have a go at. I am determined to try it out, but as it will be my first ever use of a script, it might be a while before I report back. (Hey, it's a lot different than COBOL...)

    In the meantime, it's probably all the help I will need, so thanks for your time in typing all that out.

    iMac G5 17" 1.5Gb RAM Mac OS X (10.4.7)