Improving this AppleScript to swap aliases with their original?

Discussion in 'Mac Programming' started by ElectricMan5, Nov 12, 2016.

  1. ElectricMan5 macrumors regular

    Joined:
    Jul 22, 2008
    #1
    Hey y’all, I wrote this AppleScript to swap alias files with their original (I have a huge sample library for Logic Pro X, and I’m often relocating individual files; LPX doesn’t do well with aliased samples, but I like keeping them organized!).


    Code:
    on run {input, parameters}
    
        tell application "Finder"
            set theSelection to selection
            repeat with theAlias in theSelection
                if class of theAlias is alias file then
                    try
                        set originalFile to original item of theAlias
                        set originalName to name of originalFile
                        set originalFolder to container of originalFile
                        set aliasName to name of theAlias
                        set aliasFolder to container of theAlias
                        set newName to aliasName & " _"
                        set newName2 to originalName & " _1"
                        set name of theAlias to newName
                        set name of originalFile to newName2
                        move item newName of aliasFolder to originalFolder
                        move item newName2 of originalFolder to aliasFolder
                        set name of item newName of originalFolder to originalName
                        set name of item newName2 of aliasFolder to aliasName
                    end try
                end if
            end repeat
        end tell
    return input
    end run 



    The script works well, however, there are several things I would like to do that I have zero idea how to even begin implementing.
    1. Multithreading. As it is, each file is renamed and moved one after the other, and the “dropped file” sound is spammed for ages. I’ve found that running multiple instances of the script on small groups of files is faster than one instance on a single group. How can I have the script build a list of all the files selected, then swap them all at once or split the selection into smaller selections and have the script run multiple instances of itself?
    2. The AppleScript monitor reads 0% when running, then jumps to 100% once finished. Is there a way see an accurate %?
    3. Is there a way to have each “alias swap” be recorded into a log file?

    The biggest, fattest, juiciest question of them all: Is there a more efficient way to accomplish batch swapping aliases with the originals?


    Thanks a ton guys :)
     
  2. Zazoh macrumors 6502a

    Zazoh

    Joined:
    Jan 4, 2009
    Location:
    San Antonio, Texas
    #2
    I think the answer would be in the writing of a log. So to write to log, modify this information to become part of your routine.

    https://www.macosxautomation.com/applescript/sbrt/sbrt-09.html

    I'm not even sure what you are really doing, but you do so that doesn't matter, but what if you went and got all alias names, wrote those to a file, then as part of the routine, read the file list and made the change en mass from what was read from the file, then write back that those items were found and changed when done.

    There may be a script way to suppress the sound as well, and that may be an easier work around. I really need to dive back into AppleScripting, I've created a few over the years but I forgot how powerful they can be.
     

Share This Page