1

How to create a desktop shortcut in MAC OSX using C++ only.

in linux i can do by using .desktop file

[Desktop Entry] Version=1.0 Name=Eclipse Exec=/usr/local/bin/eclipse 
5
  • 1
    Desktop shortcuts aren't really a thing on Mac OS X... I never make shortcuts on the desktop. Application shortcuts either go in the dock (or Launchpad), and there's the folder stacks for Documents and Downloads already. Commented Sep 5, 2014 at 11:52
  • But, if you really wanted to make a shortcut on the desktop, you'd want to make an alias. Commented Sep 5, 2014 at 11:53
  • 1
    The close vote is not correct, the OP is asking about how to do it with C++, which makes it on-topic for StackOverflow. Commented Sep 5, 2014 at 11:55
  • Whoever is voting to close is not reading the question! Commented Sep 5, 2014 at 12:28
  • I have provided the code to create an alias in OS X 10.8 or newer. If you need to target 10.7 or earlier, you can use this answer. Commented Sep 7, 2014 at 3:13

3 Answers 3

1

As has been mentioned, by @dreamlax, it's an alias that you would be creating; at least, this is what the UI provides when you right-click on an item: -

enter image description here

If you use the terminal and create a symbolic link to an item on the desktop, you'll see that it creates a similar 'alias'. So what you could do is create a symbolic link to the source item with a call to the ln function, which may be the same as the symlink function call.

Note that the alias created with the UI is not exactly the same as that created by ln, as you can see when linking to a pkg (installer) file. if you use the 'file' command on the two. Using ln, the created link is identified as "xar archive - version 1", whereas the UI alias creates a file which is "alias: data".

However, as far as functionality goes, both should work as a way of placing a link on the desktop to an item in another location.

Finally, take note of this distinction between the POSIX ln and alias before deciding if ln is suitable for your situation: -

[alias] is similar to the Unix symbolic link, but with the distinction of working even if the target file moves to another location on the same disk

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to make an alias, have a look at the Alias Manager reference, or if you are targeting 10.8 or later, you can use CFURLCreateBookmarkData instead.

Edit

In fact, you may even be able to just use the POSIX symlink to create a shortcut. Try that first and if that suffices, don't bother with the more complicated Alias Manager or CFURLCreateBookmarkData.

Edit 2

Here is the C code required to create a bookmark (you'll need to specify -framework CoreFoundation to the command line flags for the compiler if you are compiling from the command line, or otherwise add the CoreFoundation framework to your project in Xcode).

CFErrorRef error = NULL; CFURLRef aliasFile = CFURLCreateWithString(NULL, CFSTR("file:///Users/msl/Desktop/Terminal%20Alias"), NULL); CFURLRef targetFile = CFURLCreateWithString(NULL, CFSTR("file:///Applications/Utilities/Terminal.app"), NULL); CFDataRef bookmark = CFURLCreateBookmarkData(NULL, targetFile, kCFURLBookmarkCreationSuitableForBookmarkFile, NULL, NULL, &error); if (bookmark == NULL) { // something went wrong, check what "error" variable says } Boolean success = CFURLWriteBookmarkDataToFile(bookmark, aliasFile, kCFURLBookmarkCreationSuitableForBookmarkFile, &error); if (!success) { // something went wrong, check what "error" variable says } // make sure to clean up if (bookmark) CFRelease(bookmark); if (aliasFile) CFRelease(aliasFile); if (targetFile) CFRelease(targetFile); 

5 Comments

i am working on 10.9 so i am going to try with CFURLCreateBookmarkData
I don't see what CFURLCreateBookmarkData has anything to do with this?
well i may have to pass an argument to application, so symlink may not be a good option
@trojanfoe: here's the Objective-C equivalent for creating a bookmark. Basically you create the bookmark data with CFURLCreateBookmarkData, which contains the the target to link to, and then write the bookmark data to a file using CFURLWriteBookmarkDataToFile to create the actual alias.
OK I see; I have only encountered this method WRT saving security-scoped bookmarks within a sandboxed app.
-1

Hold Shift key, and drag application to target location. e.g: Desktop.

1 Comment

This is not answering the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.