Create Alias in Cocoa

  1. 6 years ago

    Bob K

    6 Jan 2014 Pre-Release Testers, Xojo Pro, Third Party Store Kansas City

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

    @Bob K 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!

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

  2. Kem T

    6 Jan 2014 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

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

  3. Joe R

    6 Jan 2014 Xojo Inc

    @Bob K Is there a (proper) way to create alias' in Cocoa apps?

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

  4. Bob K

    6 Jan 2014 Pre-Release Testers, Xojo Pro, Third Party Store Kansas City

    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!

  5. Joe R

    6 Jan 2014 Xojo Inc Answer

    @Bob K 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!

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

  6. Christian S

    6 Jan 2014 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

  7. Michel B

    7 Jan 2014 Pre-Release Testers, Xojo Pro RubberViews.com

    @Bob K Is there a (proper) way to create alias' in Cocoa apps?

    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 :)

  8. Christian S

    7 Jan 2014 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

  9. 3 years ago

    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.

  10. Christian S

    13 Sep 2016 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    Here is one:

    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
  11. Thanks Christian. I have done a quick test and It doesn't worked...but sincerely I have to investigate further
    probably is my fault.

  12. 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
    
  13. Christian S

    14 Sep 2016 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    For me it works with this 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(p)
      
      //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
  14. 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

  15. Christian S

    14 Sep 2016 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    Sorry. Still on 10.10 here.

  16. Christian S

    14 Sep 2016 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

  17. Edited 3 years ago

    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 -image-
    El Captain 64bit compiled -image-

    Thanks

  18. Christian S

    14 Sep 2016 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

  19. Christian S

    14 Sep 2016 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

  20. @ChristianSchmitz And log message in console.app?
    Function returns OK?

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

  21. Newer ›

or Sign Up to reply!