Friday, July 19, 2019

#1 2019-05-07 12:24:23 pm

robleach72
Member
Registered: 2013-01-01
Posts: 26

How to add a custom badge to an app icon in the dock

I just implemented a pair of bash functions to set a custom app icon badge to the slack app in the dock to indicate my slack status, which I intend to call via webhook based on my location (using geofences and iBeacons).  I already update my slack status using this webhook strategy and I can use the same strategy to call these bash functions.  Here's my first version of the bash functions I experimented with:

function slack_regular() {
  cp ~/.slack_icons/slack.icns /Applications/Slack.app/Contents/Resources/slack.icns
  touch /Applications/Slack.app
  rm -f /var/folders/*/*/*/com.apple.dock.iconcache
  killall Dock
}

function slack_inoffice() {
  cp ~/.slack_icons/inoffice.icns /Applications/Slack.app/Contents/Resources/slack.icns
  touch /Applications/Slack.app
  rm -f /var/folders/*/*/*/com.apple.dock.iconcache
  killall Dock
}

The bash functions work on the command line to update the slack app icon, but I don't like how drastic it is to kill the dock every time, so I was wondering about how currently supported app badges work and wondering if I can take advantage of that existing badge update mechanism so that I can apply my custom image-based badges to the top-right corner of an app icon.  ...And if I can do it via applescript or some existing pre-made app.

The reason I'm doing this... I trigger the webhook calls via an iPhone app called Geofency, which can make webhook calls based on geofences and iBeacons, but my cell provider is cdma, so when I'm on a call, as I often am when I leave work, the geofence triggers fine but the webhook fails.  I have it set to notify me upon error, but it never notifies me... so my slack status in those instances (or other unknown errors) doesn't update.  I just want to be able to see my status at a glance without clicking on slack to bring it to the front to inspect my status.

I used the emoji I apply to the status updates for the badge.

Offline

 

#2 2019-05-07 01:09:25 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 222

Re: How to add a custom badge to an app icon in the dock

robleach72 wrote:



The bash functions work on the command line to update the slack app icon, but I don't like how drastic it is to kill the dock every time, so I was wondering about how currently supported app badges work and wondering if I can take advantage of that existing badge update mechanism so that I can apply my custom image-based badges to the top-right corner of an app icon.  ...And if I can do it via applescript or some existing pre-made app.



The apps you see in the Dock are displayed programmatically from data stored in a property list (.plist) at this path:

/Users/<your_user_name>/Library/Preferences/com.apple.Dock.plist

The only exception is the Finder.app. The Dock Tile presence and position for Finder in the Dock are hardcoded within the

/System/Library/CoreServices/Dock.app/Contents/MacOS/Dock

executable and the Dock Tile icons for Finder in the Dock are located in

/System/Library/CoreServices/Dock.app/Contents/Resources

, thus making this just a normal working part of the Dock.app. It is one of two Dock Tiles, the other being the Trash, in which the position on the Dock cannot be changed due to being hard coded within the Dock executable.

System retrieves icons via file-data property group of each item in dock plist. I would draw the following conclusion from this information: in order for the application icon to change, you need to change the reference to application icon in the info plist of the application bundle itself.

Last edited by KniazidisR (2019-05-07 01:36:32 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#3 2019-05-07 05:17:09 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 91

Re: How to add a custom badge to an app icon in the dock

KniazidisR wrote:

you need to change the reference to application icon in the info plist of the application bundle itself.

I would caution against editing the info.plist in any application bundle, as it risks breaking file and value references that allow the application to operate.  Editing the icon file in the Resources folder of the application bundle is a safer option.

Offline

 

#4 2019-05-07 06:27:38 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5759

Re: How to add a custom badge to an app icon in the dock

CK wrote:

I would caution against editing the info.plist in any application bundle, as it risks breaking file and value references that allow the application to operate.  Editing the icon file in the Resources folder of the application bundle is a safer option.



Neither are a good idea. The former is going to invalidate the code signature, and as checks get tighter in the OS, the latter will too. It risks making an app unusable.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#5 2019-05-07 09:04:04 pm

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 91

Re: How to add a custom badge to an app icon in the dock

Shane Stanley wrote:
CK wrote:

I would caution against editing the info.plist in any application bundle, as it risks breaking file and value references that allow the application to operate.  Editing the icon file in the Resources folder of the application bundle is a safer option.

Neither are a good idea. The former is going to invalidate the code signature, and as checks get tighter in the OS, the latter will too. It risks making an app unusable.

Good to know.  I presume this goes for editing any file resource in an app bundle, be it a sound file, a jpeg, or what not ?  Basically, I'm interpreting this to mean "leave the entire contents of an app bundle alone," is that fair to say ?

Offline

 

#6 2019-05-07 09:07:39 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5759

Re: How to add a custom badge to an app icon in the dock

CK wrote:

Basically, I'm interpreting this to mean "leave the entire contents of an app bundle alone," is that fair to say ?



Exactly.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#7 2019-05-07 10:20:56 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 222

Re: How to add a custom badge to an app icon in the dock

CK wrote:
KniazidisR wrote:

you need to change the reference to application icon in the info plist of the application bundle itself.

I would caution against editing the info.plist in any application bundle, as it risks breaking file and value references that allow the application to operate.  Editing the icon file in the Resources folder of the application bundle is a safer option.



Yes, but we will not edit the original icon itself. We will edit 1 dictionary key value of Info.plist. That is, we change only the link to the icon that we need. And that is temporary. Is it really that dangerous? After all, each application is an object with settings, and not some untouchable stone from Mecca

I'm not 100% sure, but logically, we could add another icon to the existing ones in the Resources folder and refer to it from the Info.plist if necessary.

Last edited by KniazidisR (2019-05-07 10:39:16 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#8 2019-05-07 10:37:55 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5759

Re: How to add a custom badge to an app icon in the dock

KniazidisR wrote:

Is it really that dangerous?



One of the uses an app's Info.plist file these days is to declare up-front several privacy related values. Allowing the file to be modified undermines that.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#9 2019-05-07 10:47:05 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 222

Re: How to add a custom badge to an app icon in the dock

Shane Stanley wrote:
KniazidisR wrote:

Is it really that dangerous?



One of the uses an app's Info.plist file these days is to declare up-front several privacy related values. Allowing the file to be modified undermines that.



I know that there are applications that change the icons of files and folders to more beautiful ones. I do not know if there are any applications that change application icons ... If they exist, how do they do it?

HERE is example of doing that

Last edited by KniazidisR (2019-05-07 10:54:20 pm)


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#10 2019-05-07 11:11:28 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5759

Re: How to add a custom badge to an app icon in the dock

KniazidisR wrote:

HERE is example of doing that



That is not actually changing the app's icon -- it's just changing how it appears on your Mac. You can do it AppleScript:

Applescript:

use AppleScript version "2.5" -- 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set picFile to choose file of type {"public.image"} with prompt "Choose an image file"
set theApp to choose file of type {"app"} with prompt "Choose the app to apply it to"

set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:picFile
set ws to current application's NSWorkspace's sharedWorkspace()
ws's setIcon:theImage forFile:(POSIX path of theApp) options:0

The app itself will be completely unchanged.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#11 2019-05-07 11:31:26 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 222

Re: How to add a custom badge to an app icon in the dock

Shane Stanley wrote:
KniazidisR wrote:

HERE is example of doing that



That is not actually changing the app's icon -- it's just changing how it appears on your Mac. You can do it AppleScript:

Applescript:

use AppleScript version "2.5" -- 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set picFile to choose file of type {"public.image"} with prompt "Choose an image file"
set theApp to choose file of type {"app"} with prompt "Choose the app to apply it to"

set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:picFile
set ws to current application's NSWorkspace's sharedWorkspace()
ws's setIcon:theImage forFile:(POSIX path of theApp) options:0

The app itself will be completely unchanged.



I think this is what is needed in this case.


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#12 2019-05-10 09:26:10 am

robleach72
Member
Registered: 2013-01-01
Posts: 26

Re: How to add a custom badge to an app icon in the dock

Will this update the appearance of the app in the dock (whether it's currently running or not)? My experience with changing icons using the get info window is that you have to remove the app from the dock and re-add it.

Offline

 

#13 2019-05-10 10:24:35 am

robleach72
Member
Registered: 2013-01-01
Posts: 26

Re: How to add a custom badge to an app icon in the dock

OK, I just tried this out, but without the prompts (which defeat my purpose).

Applescript:

use AppleScript version "2.5" -- 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set picFile to alias "Macintosh HD:Users:<my_username>:.slack_icons:slack.icns"
set theApp to alias "Macintosh HD:Applications:Slack.app"

set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:picFile
set ws to current application's NSWorkspace's sharedWorkspace()
ws's setIcon:theImage forFile:(POSIX path of theApp) options:0

I encountered 2 problems.

1. As I suspected, the app's icon update does not get reflected in the dock (until I do something like "right-click-Options...->Show in Finder").
2. Script Editor won't let me save the script with the error "The Document "Untitled.scpt" could not be saved as "clearSlackStatus.scpt". C and Objective-C pointers cannot be saved in scripts. Compiling the script will reset property values and may resolve this issue."

I'm sure you would have a ready solution to item 2, b ut I'm not familiar enough with AppleScript to know how to do that.

Regarding item 1, I still have to perform:

rm -f /var/folders/*/*/*/com.apple.dock.iconcache
killall Dock

To make the change effective.

Last edited by robleach72 (2019-05-10 10:25:44 am)

Offline

 

#14 2019-05-10 10:51:08 am

robleach72
Member
Registered: 2013-01-01
Posts: 26

Re: How to add a custom badge to an app icon in the dock

What a ridiculous work-around.  I was able to save the script by creating a new empty script, saving it, pasting the code over from the script I couldn't save, and then save it again.  WTH?

Offline

 

#15 2019-05-10 12:20:01 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 222

Re: How to add a custom badge to an app icon in the dock

You can save script with pointers using Script Debugger instead of Script Editor


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#16 2019-05-10 12:27:07 pm

robleach72
Member
Registered: 2013-01-01
Posts: 26

Re: How to add a custom badge to an app icon in the dock

I've noted that:

rm -f /var/folders/*/*/*/com.apple.dock.iconcache

Removes the unread mail badge... so I'd still like to know how this update can be reflected in the icon in the dock...

Offline

 

#17 2019-05-10 06:25:51 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5759

Re: How to add a custom badge to an app icon in the dock

robleach72 wrote:

1. As I suspected, the app's icon update does not get reflected in the dock



That's correct.


2. Script Editor won't let me save the script with the error "The Document "Untitled.scpt" could not be saved as "clearSlackStatus.scpt". C and Objective-C pointers cannot be saved in scripts. Compiling the script will reset property values and may resolve this issue."

I'm sure you would have a ready solution to item 2



You've just pasted the solution: "Compiling the script will reset property values".

I'd still like to know how this update can be reflected in the icon in the dock...



I doubt that it can. The dock icon belongs to the app, not you.

Last edited by Shane Stanley (2019-05-10 06:29:21 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#18 2019-05-10 06:28:29 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5759

Re: How to add a custom badge to an app icon in the dock

KniazidisR wrote:

You can save script with pointers using Script Debugger instead of Script Editor



No you can't -- scripts can't be saved when top-level variables contain pointers, regardless of the editor. The solution is to re-compile, so the saved variables have no value.

But you can avoid the problem using Script Debugger, by turning off Persistent Properties.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#19 2019-05-10 09:39:24 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 222

Re: How to add a custom badge to an app icon in the dock

Shane Stanley wrote:
KniazidisR wrote:

You can save script with pointers using Script Debugger instead of Script Editor



No you can't -- scripts can't be saved when top-level variables contain pointers, regardless of the editor. The solution is to re-compile, so the saved variables have no value.



If so, can I assign the missing value to the properties at the end of the script and achieve the same effect as recompilation?


macOS Mojave -- version 10.14.4
Safari -- version 12.1

Offline

 

#20 2019-05-10 09:41:58 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5759

Re: How to add a custom badge to an app icon in the dock

KniazidisR wrote:

can I assign the missing value to the properties at the end of the script and achieve the same effect as recompilation?



Yes. But it's not just properties -- it's all top-level variables that contain pointers.

Last edited by Shane Stanley (2019-05-10 09:43:08 pm)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#21 2019-05-11 02:22:47 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4908

Re: How to add a custom badge to an app icon in the dock

KniazidisR wrote:
Shane Stanley wrote:

The solution is to re-compile, so the saved variables have no value.



If so, can I assign the missing value to the properties at the end of the script and achieve the same effect as recompilation?


Another solution, unless properties and/or globals are absolutely necessary in a running script (which I find they rarely are), is to make all the variables local, and therefore non-persistent, by putting the top-level run code into an ordinary handler:

Applescript:

use AppleScript version "2.5" -- 10.11 or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

main() -- The sole instruction in the implicit run handler.

on main()
   set picFile to alias "Macintosh HD:Users:<my_username>:.slack_icons:slack.icns"
   set theApp to alias "Macintosh HD:Applications:Slack.app"
   
   set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:picFile
   set ws to current application's NSWorkspace's sharedWorkspace()
   ws's setIcon:theImage forFile:(POSIX path of theApp) options:0
end main


NG

Offline

 

#22 2019-05-13 06:48:56 am

robleach72
Member
Registered: 2013-01-01
Posts: 26

Re: How to add a custom badge to an app icon in the dock

2. Script Editor won't let me save the script with the error "The Document "Untitled.scpt" could not be saved as "clearSlackStatus.scpt". C and Objective-C pointers cannot be saved in scripts. Compiling the script will reset property values and may resolve this issue."

I'm sure you would have a ready solution to item 2



You've just pasted the solution: "Compiling the script will reset property values".



I assumed I was implying that by pasting so obvious a fix, that it didn't work.  However, given my interactions with others online (and my parents), I realize I should have explicitly stated that I did compile and I still could not save and got the same error.

Last edited by robleach72 (2019-05-13 06:50:24 am)

Offline

 

#23 2019-05-13 07:22:21 am

robleach72
Member
Registered: 2013-01-01
Posts: 26

Re: How to add a custom badge to an app icon in the dock

Shane Stanley wrote:
robleach72 wrote:

I'd still like to know how this update can be reflected in the icon in the dock...



I doubt that it can. The dock icon belongs to the app, not you.



Be that as it may, a user is allowed to change an icon of any app (even though it doesn't belong to the user) via copy/paste in the get-info window.  Thus, I can think of a dead-simple method just off the top of my head of changing the appearance in the dock: quit and re-launch the app via applescript. So apparently, it can.

I know it's a compromise solution and is not technically what I asked, but if you consider what I've indicated thus far, it effectively solves the problem:

1. I'm running the code to change the dock icon "via webhook based on my location (using geofences and iBeacons)" - so I'm not sitting at my desk and using the app when it runs, thus I'm not actively using the app and a re-launch would not likely interrupt anything important.
2. I already have a (currently) working method that involves killing the dock with the unfortunate side-effect of eliminating other app badges - so I'm willing to consider somewhat drastic solutions.
3. I've complained that my current solution clears other apps' badges.

Thus, quitting and restarting the Slack app, while still drastic and not as satisfying a solution programmatically, is an acceptable solution.  I didn't think of it until now, but the discussion here gave me the inspiration.  Thanks guys.

I appreciate the help you have offered.  I definitely would not have figured out the portion of the solution you guys provided on my own.

I would still be interested if anyone were to discover a better solution of updating a dock icon's appearance without re-launching it.

Offline

 

#24 2019-05-13 07:24:00 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5759

Re: How to add a custom badge to an app icon in the dock

robleach72 wrote:

I assumed I was implying that by pasting so obvious a fix, that it didn't work.  However, given my interactions with others online (and my parents), I realize I should have explicitly stated that I did compile and I still could not save and got the same error.



Sorry, I wasn't trying to be smart -- it's just that I've never seen it not work. But I have seen Script Editor's autosave get itself tied in a knot because of pointers, so perhaps that's what happened.

IAC, that really is the fix. Did you make a small change to force re-compiling?


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Online

 

#25 2019-05-13 07:31:29 am

robleach72
Member
Registered: 2013-01-01
Posts: 26

Re: How to add a custom badge to an app icon in the dock

Shane Stanley wrote:

Did you make a small change to force re-compiling?



I created a new script, saved it (empty), then pasted the code from the script that wouldn't save.  Then I could save it.  I don't recall whether I compiled it first.  I probably did.  I usually do.  I'm not aware of anything being different between the 2 scripts.

Sometimes, Text Edit won't let me save documents, but the error in those cases is a permissions issue (even though there doesn't appear to be a problem with permissions, so I'm rather used to non-sensical errors from macOS).  In those cases, I have to copy out the text and paste it somewhere, then restart Text Edit.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)