Create Alias in Cocoa

Is there a (proper) way to create alias’ in Cocoa apps?

MacOSLib has some code for this, but what are you trying to do exactly?

Are you looking to create alias files (on disk like Finder) or just something you can resolve back to the original file?

It’s creating an alias file.

I had carbon code to this and just after I posted the original message I looked at MacOSLib and found some stuff. I’m upgrading a client project and the function might just be part of a ‘package’ so I’m not sure it’s actually getting called so this might have all been for naught.

Thanks for the replies!

[quote=57533:@Bob Keeney]It’s creating an alias file.

I had carbon code to this and just after I posted the original message I looked at MacOSLib and found some stuff. I’m upgrading a client project and the function might just be part of a ‘package’ so I’m not sure it’s actually getting called so this might have all been for naught.

Thanks for the replies![/quote]

One modern way to do this is create a CFURL and then call CFURLCreateBookmarkData followed by CFURLWriteBookmarkDataToFile.

With MBS plugins, it’s all in the CFBookmarkMBS module.
http://www.monkeybreadsoftware.net/module-cfbookmarkmbs.shtml

I know it is not exactly the same, but creating a symbolic link through shell could do the trick :

ln -s path+realfilename path+symboliclinkname

It looks the same as an alias to the user :slight_smile:

and the link is much smaller in size :slight_smile:
(but can easier break if you move it)

I can’t figure out a way to create an Alias file for a folder or file using cfbookmarkmbs…
Please Christian can you post a sample code ?
Thanks
C.

Here is one:

[code]dim file as FolderItem = SpecialFolder.Desktop.TrueChild(“test.rtf”)
dim Bookmark as string = CFBookmarkMBS.CreateBookmarkData(file, CFBookmarkMBS.kCreationSuitableForBookmarkFile)

dim AliasFile as FolderItem = SpecialFolder.Desktop.TrueChild(“test.alias”)
if CFBookmarkMBS.WriteBookmarkDataToFile(Bookmark, AliasFile, 0) then
MsgBox “OK”
else
dim e as CFErrorMBS = CFBookmarkMBS.lasterror
MsgBox "Failed: "+e.Description
end if[/code]

Thanks Christian. I have done a quick test and It doesn’t worked…but sincerely I have to investigate further
probably is my fault.

I was able to create an alias file in cocoa as Christina suggested. Now I want to change the Icon for the Alias. I tried without success.
Anyone can have a workaround ?

  dim file as FolderItem = SpecialFolder.Desktop.TrueChild("32bit")
  dim Bookmark as string = CFBookmarkMBS.CreateBookmarkData(file, CFBookmarkMBS.kCreationSuitableForBookmarkFile)
  Dim s as string
  
  dim AliasFile as FolderItem = SpecialFolder.Desktop.TrueChild("32bit.alias")
  
   
  if CFBookmarkMBS.WriteBookmarkDataToFile(Bookmark, AliasFile, 0) then
    MsgBox "OK"
  else
    dim e as CFErrorMBS = CFBookmarkMBS.lasterror
    MsgBox "Failed: "+e.Description
  end if
  
  // set the file's type and creator codes
  if file.Directory = true then
    AliasFile.MacType = "fribox"
    AliasFile.MacCreator = "MACS"
  else
    AliasFile.MacType =AliasFile.MacType
    AliasFile.MacCreator = AliasFile.MacCreator
  end
  
  //apply custom icon to Alias
  Dim img as NSImageMBS
  Dim p as Picture
  
  p= New Picture(1024,1024)
  
  p.Graphics.DrawPicture fribox1024,0,0
  
  
  img=new NSImageMBS(p)

  //Icon set works for folders
  //if NSWorkspaceMBS.setIcon(img,file,0) then
  //// make the newly created file known to the Finder immediately
  //UpdateItemInFinder file
  //s = "ALIAS_CREATED_SUCCESSFULLY"
  //else
  //s = "ERROR_ALIAS_CREATION_FOR" 
  //
  //end if
  
  //don't works for Aliases
  if NSWorkspaceMBS.setIcon(img,AliasFile,0) then
    // make the newly created file known to the Finder immediately
    UpdateItemInFinder AliasFile
    s = "ALIAS_CREATED_SUCCESSFULLY"
  else
    s = "ERROR_ALIAS_CREATION_FOR" + " " + AliasFile.NativePath
    
  end if
  
  MsgBox s

For me it works with this code:

[code] dim file as FolderItem = SpecialFolder.Desktop.TrueChild(“32bit”)
file.CreateAsFolder

dim Bookmark as string = CFBookmarkMBS.CreateBookmarkData(file, CFBookmarkMBS.kCreationSuitableForBookmarkFile)
Dim s as string

dim AliasFile as FolderItem = SpecialFolder.Desktop.TrueChild(“32bit.alias”)

if CFBookmarkMBS.WriteBookmarkDataToFile(Bookmark, AliasFile, 0) then
MsgBox “OK”
else
dim e as CFErrorMBS = CFBookmarkMBS.lasterror
MsgBox "Failed: "+e.Description
end if

// set the file’s type and creator codes
if file.Directory = true then
AliasFile.MacType = “fribox”
AliasFile.MacCreator = “MACS”
else
AliasFile.MacType =AliasFile.MacType
AliasFile.MacCreator = AliasFile.MacCreator
end

//apply custom icon to Alias
Dim img as NSImageMBS
Dim p as Picture

dim Logo as Picture = LogoMBS(1024)

p= New Picture(1024,1024)

p.Graphics.DrawPicture Logo,0,0

img=new NSImageMBS§

//don’t works for Aliases
if NSWorkspaceMBS.setIcon(img,AliasFile,0) then
// make the newly created file known to the Finder immediately
'UpdateItemInFinder AliasFile
s = “ALIAS_CREATED_SUCCESSFULLY”
else
s = “ERROR_ALIAS_CREATION_FOR” + " " + AliasFile.NativePath
end if[/code]

Yes Christian It works in OSX Yosemite. I’m testing on a Virtual machine with El Captain installed…and I’m banging the head to make It work.
DO you have tested on El Captain or newer ?
Thanks

Sorry. Still on 10.10 here.

You can always include a 32-bit helper app to do it the old way.

It is a little bit complex to include helper apps … there are other issues to consider in order to submit on Apple Store…

I have tried to find a workaround. I created a new ‘dummy’ folder (not visible), applied the new icon and get Bookmark data.

So i create the Alias using the bookmarkdata of the ‘dummy’ folder…the only issue is I haven’t find a way (studing your CFBookmark module) to change in the created Alias the target directory (from ‘dummy’ folder to ‘32bit’ folder).

There is a way to edit in some way the bookmark datas to overwite in the Alias the path to original folder ?

Yosemite 64bit compiled
El Captain 64bit compiled

Thanks

And log message in console.app?
Function returns OK?

This could be a bug in Apple’s stuff of course and outside my capabilities to fix.

[quote=286988:@Christian Schmitz]And log message in console.app?
Function returns OK?[/quote]

I don’t know about console and how to grab logs.
In any case the code return OK (El Captain or Yosemite)