Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Replace Alias with Symbolic Link

Hi


We work with thousands of aliases, and we need to change them into "Symbolic Links". Is there a way to automate it via "Automator" or "Apple Script". I think a combinatio of bot would be the best, cause we would like to to that with many folder that include the aliases.


Many thanks


Rob

Posted on Apr 12, 2014 1:54 AM

Reply
11 replies

Apr 12, 2014 11:43 AM in response to geoman

Possible.


This Applescript

tell application "Finder"

if ((file type of file "Filename" of desktop) is "alis") then

get original item of alias file "Filename" of desktop

end if

end tell

would look in your Desktop folder for a file named Filename and if it is an alias get the item it points to, the original file.


So using this idea you could search for the alias files, get the original item it pointed to, then using a do shell script command create a link to the the original item. You could delete the alias if you dont; want both the link and the alias.


BTW these types of questions are best asked in the OS X Technologies community I'll see if a host ill move it.

(it's been moved)

Apr 13, 2014 3:27 AM in response to geoman

Thank you,


This script is a good start, but the main problem is:


we talking about thousands of aliases (only aliases of folders) sorted in different "Parent-Folder's". (it's a very huge soundlibrary - and growing every day). With this scrip, every alias needs to be pointed and moved manually.


The plan ist to tell Automator or a Script "this is the folder which contains the aliases - replace or change all with/to a symboic link to a new location".


The reason for a change to symbolic links is:


1.We need to copy the aliases between Computers with clone's of the Soundlibrary HD. Sometimes something goes wrong and the alias is broken. It always works with symbolic links.

2. Symlinks are so much faster to show up. We are talking about folders with sometimes more that hundred aliases inside.This a issue when you browsing.

3. the alias of a folder is 5,8 mb big, a symlink less than 100k. Right now the Alias-Library has a size of more than 15 Gb.


thanks for your interest


rob

Apr 13, 2014 3:56 AM in response to geoman

Hello


You may try the following applescript script, which is a simple wrapper of rubycocoa script. It requires OSX 10.6 or later for CF API to resolve alias.


It will ask you to choose the directory where the target file system subtree is rooted and then try to replace every alias file found in the tree with symlink. Package contents are not traversed. A log file is created on desktop.


If alias file cannot be replaced, e.g., being locked or without write permission etc, symlink is not created and error is logged. If the volume where target of alias resides is not mounted, the alias is not resolved and error is logged.


Briefly tested under 10.6.8. However, no warranties of any kind. Please make sure you have backup of target directory.


Hope this may help,

H


set d to (choose folder with prompt "Choose start folder")'s POSIX path
replace_alias_with_symlink(d)

on replace_alias_with_symlink(d)
    (*
        string d : POSIX path of directory where target subtree is rooted
    *)
    set ruby to "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby"
    do shell script ruby & " -w <<'EOF' - " & d's quoted form & "
require 'FileUtils'
require 'osx/cocoa'
include OSX

# log file is YYYY-MM-DD_alias_to_symlink_log.txt in ~/desktop
LOGF = File.expand_path(Time.now.strftime('%F_alias_to_symlink_log.txt'), '~/desktop')

def log(s, options = {:stamp => true})
    File.open(LOGF, 'a') do |a|
        a.puts '%-26s%s' % [options[:stamp] ? Time.now.strftime('%F %T%z') : '', s]
    end
end

def resolve_alias(f)
    #     
    #     string f : POSIX path of alias file
    #     return string : POSIX path of target file or nil if failed to resolve alias
    #     
    #     * requires OSX 10.6 or later for
    #         CFURLCreateBookmarkDataFromFile()
    #         CFURLCreateByResolvingBookmarkData()
    #     
    url = NSURL.fileURLWithPath(f)
    bmrk = CFURLCreateBookmarkDataFromFile(KCFAllocatorDefault, url, nil)
    unless bmrk
        log('# Error: unable to get bookmark data from %s' % f)
        return nil
    end
    
    stale = ObjcPtr.new(:char)        # BOOL*
    url1 = CFURLCreateByResolvingBookmarkData(
        KCFAllocatorDefault,
        bmrk,
        KCFBookmarkResolutionWithoutMountingMask | KCFBookmarkResolutionWithoutUIMask,
        nil, 
        nil,
        stale,
        nil)
    unless url1
        log('# Error: unable to resolve alias %s' % f)
        return nil
    end
    if stale.bool != 0        # this should not happen, for bmrk is fresh
        log('# Warning: stale alias %s' % f)
    end
    return url1.path.to_s
end

raise ArgumentError, %Q[Usage: #{File.basename($0)} directory] unless ARGV.length == 1
dir = ARGV[0].chomp('/')
denum = NSFileManager.defaultManager.enumeratorAtPath(dir)
wks = NSWorkspace.sharedWorkspace

# scan aliases
aa = []        # list of aliases under dir
while (f = denum.nextObject) != nil do
    file = dir + '/' + f.to_s
    denum.skipDescendants if wks.isFilePackageAtPath(file)        # ignore package contents
    if denum.fileAttributes.objectForKey(NSFileType) == NSFileTypeRegular
        aa << file if wks.objc_send(:typeOfFile, file, :error, nil) == 'com.apple.alias-file'
    end
end

# replace alias with symlink
aa.each do |a|
    f = resolve_alias(a)
    next unless f
    begin
        FileUtils.ln_sf(f, a)
        log('Created symlink %s  =>  %s' % [a, f])
    rescue => ex
        log('# Error: failed to create symlink %s  =>  %s' % [a, f])
        log('  %s: %s' % [ex.class, ex.message], :stamp => false)
    end
end
EOF"
end replace_alias_with_symlink

Apr 13, 2014 8:22 AM in response to geoman

This script is a good start, but the main problem is:


we talking about thousands of aliases (only aliases of folders) sorted in different "Parent-Folder's". (it's a very huge soundlibrary - and growing every day). With this scrip, every alias needs to be pointed and moved manually.

Just to be clear the script was meant as a starting point.


I assumed you needed help finding the alias and the original file it pointed to, not that you wanted the whole script written for you.


regards

Jul 3, 2015 12:16 AM in response to Toos

Hello


Just for future reference.


I deliberately specified ruby 1.8 interpreter because rubycocoa bundled with standard installation of OS X 10.5 - 10.9 requires it.


As for the symlink:


/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby



it is fine to use it under OS X 10.5 through 10.8, for there's only one version - ruby 1.8 - in System.


However, under OS X 10.9, there're two versions - ruby 2.0 and ruby 1.8 - in System and the Current points to ruby 2.0, with which the rubycocoa won't work.


And under OS X 10.10, there's only one version - ruby 2.0 - in System and there's NO rubycocoa bundled with standard installation. Thus the provided rubycocoa script won't work unless you manually install rubycocoa 1.2.0 which supports ruby 2.0:



http://rubycocoa.sourceforge.net/

http://sourceforge.net/projects/rubycocoa/files/RubyCocoa/1.2.0/



and change ruby interpreter in script to:


/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby



or:


/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby



or even:


/usr/bin/ruby



Regards,

H

Replace Alias with Symbolic Link

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.