Programmatically Change Icon

  • Hi everyone:

    I am wondering if there is some way to change another applications icon
    programmatically.  The problem that I see if that every application's icon
    name is different and I haven't found a way to read that application's icon
    location or will just return a NSImage that is the icon.

    Thanks for any help.
  • On Mar 22, 2009, at 12:27 PM, Pierce Freeman wrote:

    > I am wondering if there is some way to change another applications
    > icon
    > programmatically.  The problem that I see if that every
    > application's icon
    > name is different and I haven't found a way to read that
    > application's icon
    > location or will just return a NSImage that is the icon.

    Problems:

    1 - Not all users are administrators of their computer, and so a user-
    space application may not be able to modify the application bundles in
    question. You'll need to authenticate/authorize the user to perform
    this change if the application is in the /Applications folder, or the
    user has no write permissions for the app bundles otherwise.

    2 - Some applications may be digitally signed, in which case modifying
    the application bundles in question may at least cause disconcerting
    warnings for the user launching the modified applications, and at
    worst break the application(s).

    3 - This may be technically in violation of the EULA of the
    applications in question. IANAL, but I certainly would look into this
    before trying it, especially with the very loosely-defined DMCA laws.

    4 - All the above aside, make sure you have your users' express
    permission to perform this action ... it is a pretty big deal and I
    would personally not use any application that does this, and you can
    count on others feeling the same way, so make sure you ask in very
    clear, plain language.

      That said, to get the application's icon, you can do what the
    Finder does and use the app bundle's Info.plist and ask for the value
    for the CFBundleIconFile key. Then ask the application bundle for the
    resource with the returned name.

      See the documentation for the permissions issue and for how to work
    with the icon file format.

    --
    I.S.
  • On 22.03.2009, at 17:27, Pierce Freeman wrote:
    > The problem that I see if that every application's icon
    > name is different and I haven't found a way to read that
    > application's icon
    > location or will just return a NSImage that is the icon.

      Sure you can retrieve NSImages for application icons. Look at
    NSWorkspace's iconForFile: method and similar ones. Now, changing
    them, that's a different matter. This used to be able with Carbon's
    Icon Services APIs, though. There you were able to override an icon
    with one you provided, e.g. to animate the progress bar on a
    downloaded file icon. Might want to look into those. Last time I did
    that was on OS 9 though, so no guarantee whether that's still system-
    wide these days.

    > I am wondering if there is some way to change another applications
    > icon
    > programmatically.

      What are you trying to do that you need to do this? Are you trying
    to change the dock icons of running applications, or what? For that,
    it might be a better idea to use Accessibility APIs or AppleScript (or
    maybe even CGWindow APIs work?) to determine the rectangle of the
    requisite dock tile, and then show your own, transparent window on top?

      Have you considered the repercussions of your changes on the
    developers of the other applications? Consider that, not only would
    you break any code signing on those apps, and any other checksums they
    may be using to make sure patches or incremental updates work, you
    could also trigger other sorts of tamper alerts, or through a bug
    damage the application whose icon you wanted to change.

      Trouble is, the user might not notice until they next launch that
    other app. And the people who would then have to deal with what your
    app caused are likely the ones who developed your 'victim' app. After
    all, many users won't understand that just changing an icon can damage
    an application, not to mention that some might have forgotten they
    used your application to change the icon.

      We used to get lots of bug reports from people who had used third-
    party tools to strip Intel code from their universal binary
    applications to save disk space. They'd done that a while ago, and
    when they used the migration assistant to move their apps to their new
    Intel Mac months later, some of them refused to work, and many of them
    were dog-slow because of issues with Rosetta they wouldn't even have
    had if they had just run the intel-native code we put in there.

      I'm not saying you shouldn't do that, but I just thought I'd make
    you aware of the backlash that applications that patch others in
    whatever way can cause. Talk to the guys from Unsanity if you want to
    know how even people who did a very good job of writing a robust
    patching engine got hissed at, I'm sure they have a few stories to
    tell...

      Oh, one more thing: If you really just want to change the icon, you
    may want to look into how Finder applies custom icons to folders.
    You'd still have to authenticate, but at least you'd be doing a
    modification that the user can do using the Finder, so it's something
    application developers can expect. I believe how it's basically done
    is creating a file with the name "Icon\r" (\r is 0x0D, i.e. a return
    character, not a newline as in \n or 0x0A) at the top level of the
    application package, next to the "Contents" folder, and set the
    kHasCustomIcon flag on the folder. The "Icon\r"-file contains an
    'icns' resource with ID -16455 containing the contents of a .icns file.

    Cheers,
    -- Uli Kusterer
    "The Witnesses of TeachText are everywhere..."
    http://www.zathras.de
  • Well, that's what I get for typing this when I was still half asleep.  I
    meant to simply view instead of changing the icon.  Would there be any
    problems (legally or otherwise) with doing this?  You seem to know about
    these issues a lot better them myself...

    I definitely agree with all the problems you outlined, and never wanted to
    cause problems.  If you would  mind saying, though, how would changing the
    Application's icon involve legal issues?  Since the users of the computers
    do it all the time.  Also I agree that using an application that would
    change them without the user's permission would be very disconcerting and I
    would probably trash that application right away.

    Thanks for your help.

    On 3/22/09 10:55 AM, "I. Savant" <idiotsavant2005...> wrote:

    > On Mar 22, 2009, at 12:27 PM, Pierce Freeman wrote:
    >
    >> I am wondering if there is some way to change another applications
    >> icon
    >> programmatically.  The problem that I see if that every
    >> application's icon
    >> name is different and I haven't found a way to read that
    >> application's icon
    >> location or will just return a NSImage that is the icon.
    >
    > Problems:
    >
    > 1 - Not all users are administrators of their computer, and so a user-
    > space application may not be able to modify the application bundles in
    > question. You'll need to authenticate/authorize the user to perform
    > this change if the application is in the /Applications folder, or the
    > user has no write permissions for the app bundles otherwise.
    >
    > 2 - Some applications may be digitally signed, in which case modifying
    > the application bundles in question may at least cause disconcerting
    > warnings for the user launching the modified applications, and at
    > worst break the application(s).
    >
    > 3 - This may be technically in violation of the EULA of the
    > applications in question. IANAL, but I certainly would look into this
    > before trying it, especially with the very loosely-defined DMCA laws.
    >
    > 4 - All the above aside, make sure you have your users' express
    > permission to perform this action ... it is a pretty big deal and I
    > would personally not use any application that does this, and you can
    > count on others feeling the same way, so make sure you ask in very
    > clear, plain language.
    >
    > That said, to get the application's icon, you can do what the
    > Finder does and use the app bundle's Info.plist and ask for the value
    > for the CFBundleIconFile key. Then ask the application bundle for the
    > resource with the returned name.
    >
    > See the documentation for the permissions issue and for how to work
    > with the icon file format.
    >
    > --
    > I.S.
    >
    >
  • On Mar 22, 2009, at 3:45 PM, Pierce Freeman wrote:

    > Well, that's what I get for typing this when I was still half
    > asleep.  I
    > meant to simply view instead of changing the icon.

      Heh - "... some way to change another applications icon ..."
    Changing has all those issues attached. Viewing on the other hand is
    easy.

      As Uli mentioned, NSWorkspace should be all you need simply to get
    a copy of the icon. The bundle stuff is only necessary if you want to
    get at the file itself (say, to modify it :-)).

    > Would there be any problems (legally or otherwise) with doing this?
    > You seem to know about these issues a lot better them myself...

      Best answer: I don't know for sure. Note the use of "IANAL" (I Am
    Not A Lawyer) and "might be". :-)

      In the US, distributing software that modifies third-party software
    is uncomfortably close to the fence of a number of copyright laws and
    regulations. It's something any independent software business owner
    should be aware of. Again, this is from the perspective of a US
    citizen; it may be a non-issue for you, but you should definitely
    consult a lawyer before distributing such an application. That's all I
    meant to say.

      This discussion, however, is off-topic for cocoa-dev, so I'll leave
    it at that. I suggest the "macsb" group on Yahoo Groups.

    --
    I.S.
  • > Heh - "... some way to change another applications icon ..."
    > Changing has all those issues attached. Viewing on the other hand is
    > easy.

    My point exactly. ;)

    > As Uli mentioned, NSWorkspace should be all you need simply to get
    > a copy of the icon. The bundle stuff is only necessary if you want to
    > get at the file itself (say, to modify it :-)).

    Would NSWorkspace also work if the app was not running, or only if it is?

    > Best answer: I don't know for sure. Note the use of "IANAL" (I Am
    > Not A Lawyer) and "might be". :-)
    >
    > In the US, distributing software that modifies third-party software
    > is uncomfortably close to the fence of a number of copyright laws and
    > regulations. It's something any independent software business owner
    > should be aware of. Again, this is from the perspective of a US
    > citizen; it may be a non-issue for you, but you should definitely
    > consult a lawyer before distributing such an application. That's all I
    > meant to say.

    While it doesn't have to do with me, it's good advice to know.  However, as
    you said, I assume these problems aren't true with just showing the icon
    (versus changing it).

    > This discussion, however, is off-topic for cocoa-dev, so I'll leave
    > it at that. I suggest the "macsb" group on Yahoo Groups.

    True, I'll look there if I have any more questions.

    Thanks for all your help.
  • On Mar 22, 2009, at 5:29 PM, Pierce Freeman wrote:

    > Would NSWorkspace also work if the app was not running, or only if
    > it is?
    >

      Slightly-challenging-answer: Try it and see.

      Non-smarmy-answer: Yes. So would the other, more complicated
    approach.

    > While it doesn't have to do with me, it's good advice to know.
    > However, as
    > you said, I assume these problems aren't true with just showing the
    > icon
    > (versus changing it).

      I would assume this as well ... there'd be a number of software
    companies in hot water otherwise. Especially OS vendors like Apple and
    Microsoft, since Finder and Windows Explorer both show other
    applications' icons. :-)

    --
    I.S.
  • > Slightly-challenging-answer: Try it and see.
    >
    > Non-smarmy-answer: Yes. So would the other, more complicated
    > approach.

    Lol, just wanted to make sure.

    > I would assume this as well ... there'd be a number of software
    > companies in hot water otherwise. Especially OS vendors like Apple and
    > Microsoft, since Finder and Windows Explorer both show other
    > applications' icons. :-)

    Yeah, this is more or less what I figured... Application icons are more or
    less made to be viewed. ;)
  • > Slightly-challenging-answer: Try it and see.
    >
    > Non-smarmy-answer: Yes. So would the other, more complicated
    > approach.

    Just tried it, and it works great... Thanks!  Just two questions so far:

    1. How can I get different sizes of the icon, or are they just linked to the
    size of the outlet?
    2. If you link to a non-existent application, it still returns an image - Is
    there some way to set it to return an error if this happens?

    Thanks for all your help.
  • On Mar 22, 2009, at 6:09 PM, Pierce Freeman wrote:

    >> Slightly-challenging-answer: Try it and see.
    >>
    >> Non-smarmy-answer: Yes. So would the other, more complicated
    >> approach.
    >
    > Just tried it, and it works great... Thanks!  Just two questions so
    > far:
    >
    > 1. How can I get different sizes of the icon, or are they just
    > linked to the
    > size of the outlet?

      I suggest some reading:

    http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/nsimage_Class/Reference/Reference.html


    http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaDrawingGuide
    /Images/Images.html


    http://developer.apple.com/documentation/Cocoa/Conceptual/ImageView/ImageVi
    ew.html


    > 2. If you link to a non-existent application, it still returns an
    > image - Is
    > there some way to set it to return an error if this happens?

      This is what I'd expect ... if an application has no custom icon,
    it gets the standard, generic application icon. In this case, you'd
    probably want to go with the "get the app icon file name, then ask the
    app bundle for the resource of that name" route. This way, if there's
    nothing set for the app icon key, you'll know there's no custom icon.
    If so, you can just use the NSWorkspace call to get that custom icon.

    --
    I.S.

  • Thanks for the links, I'll look them over when I get a chance.

    > This is what I'd expect ... if an application has no custom icon,
    > it gets the standard, generic application icon. In this case, you'd
    > probably want to go with the "get the app icon file name, then ask the
    > app bundle for the resource of that name" route. This way, if there's
    > nothing set for the app icon key, you'll know there's no custom icon.
    > If so, you can just use the NSWorkspace call to get that custom icon.

    I think I may just go this route, especially if it doesn't break any
    copyright/patent/etc. Laws. ;)  But the odd thing is, if I set the path to a
    file that I know doesn't exist (ex.
    /Applications/soidaoidaiodsaoidiasoadsoidoiaadiosoaidiodaoidasoidaoidasoiado
    iasdoidaoidadasoidiaosasidoosdiaiodsiodsa.app) it will still return some
    weird page icon.
  • On Mar 22, 2009, at 6:29 PM, Pierce Freeman wrote:

    > But the odd thing is, if I set the path to a
    > file that I know doesn't exist (ex.
    > /Applications/
    > soidaoidaiodsaoidiasoadsoidoiaadiosoaidiodaoidasoidaoidasoiado
    > iasdoidaoidadasoidiaosasidoosdiaiodsiodsa.app) it will still return
    > some
    > weird page icon.

      Come to think of it, it's strange that you get an icon for a non-
    existent file, but if it *does* exist, I'd always expect some sort of
    icon. I've never run into this situation personally. Which specific
    method are you using? There are:

    – iconForFile:
    – iconForFileType:
    – iconForFiles:

      Just curious.

      In your case, though, if you're going after applications, you'd
    presumably already have a list of app bundle paths, so it shouldn't be
    a problem, right?

    --
    I.S.
  • > Come to think of it, it's strange that you get an icon for a non-
    > existent file, but if it *does* exist, I'd always expect some sort of
    > icon. I've never run into this situation personally. Which specific
    > method are you using? There are:
    >
    > ­ iconForFile:
    > ­ iconForFileType:
    > ­ iconForFiles:

    I kind of figured that it was a bit odd.  I am using iconForFile and then
    the path.

    > In your case, though, if you're going after applications, you'd
    > presumably already have a list of app bundle paths, so it shouldn't be
    > a problem, right?

    Presumably, but if the user deleted the application it would be nice to
    alert the user saying something instead of just showing some empty page
    icon. ;)
previous month march 2009 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today