Create Alias in Cocoa

  1. ‹ Older
  2. 6 years ago

    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!

  3. 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.

  4. 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

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

  6. 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)

  7. 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.

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

  10. 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
    
  11. 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
  12. 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

  13. Christian S

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

    Sorry. Still on 10.10 here.

  14. 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.

  15. 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

  16. 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?

  17. 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.

  18. @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)

  19. Christian S

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

    console.app is an app in your Applications/Utilities folder.
    It captures debug messages from applications.

  20. @ChristianSchmitz console.app is an app in your Applications/Utilities folder.
    It captures debug messages from applications.

    I can only report this:
    14/09/16 22:11:22,069 lsd[235] LaunchServices: Could not store lsd-identifiers file at /private/var/db/lsd/com.apple.lsdschemes.plist

  21. 7 weeks ago

    Jürg O

    Jul 30 Pre-Release Testers, Xojo Pro

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

    With macOS 10.6+, you can also do this with NSURL+writeBookmarkData:toURL:options:error:
    Just in case someone is interested: Here is an example project: CreateShortcut , which extends FolderItem.CreateShortcut(dest, caption, icon) for Windows/macOS/Linux (without using 3rd party Plugins).

or Sign Up to reply!