Dynamically updating app icon in OSX - macos

When I was using Chrome to download something in Lion, a badge with downloading progress which dynamically updates itself is shown on my dock.
How may one go about achieving that?

I think you are looking for this piece of code:
[[[NSApplication sharedApplication] dockTile] setBadgeLabel:#"My String"];
Here you can find all the information you need (it's the NSDockTile Class Reference).

Related

WKWebView Mac Browser Enable fullscreen html5?

I made a simple browser with Swift last version and xcode 8.3.3.
I want to be able to enter in fullscreen when there is an html5 video (like on youtube).
I get "full screen is unavailable" on youtube right now.
Same problem with the old WebView... on iOS it work.
EDIT. Maybe it's just not possible. I tried to look at JavaFX WebView and WPF WebBrowser and they have the same limitation. Actually one guy was able to allow full screen for a youtube video on WPF WebBrowser but only by creating a full html page:
Playing youtube in full screen in WebBrowser control
And of course I cannot create a webpage for every video in a page (at least I don't know how, if you know please tell me).
ORIGINAL MESSAGE:
I made a simple browser with Swift last version and xcode 8.3.3.
Everything is working but I'm not able to activate plugins like I can do with the old WebView.
Since I'm on a mac I should be able to activate plugins (I understand that it's not possible on iOS) am I wrong?
Also (and here I got the same problem in the old WebView) there is no way to activate fullscreen for html5 videos (at least I don't know how).
#IBOutlet weak var webView: WKWebView!
let urlString = "http://myurl.com/"
self.webView.load(NSURLRequest(url: NSURL(string: urlString)! as URL) as URLRequest!)
self.webView.configuration.preferences.plugInsEnabled = true
This is the really basic code to get a basic browser working. But there is no option to enable plugin in the Interface Builder for WKWebView and I really don't know how to allow fullscreen for html5 videos (like youtube).
EDIT. Ok I finally found an answer for the plugin part:
self.webView.configuration.preferences.plugInsEnabled = true
really easy but it was difficult to understand where to find it I had to go here:
https://developer.apple.com/documentation/webkit/webpreferences
and take a guess...
Update for macOS 12.3
Starting on iOS 15.4 and macOS 12.3 WKPreferences has the new property isElementFullscreenEnabled. Now it is very simple:
myWebView.configuration.preferences.isElementFullscreenEnabled = true
Original answer (pre macOS 12.3)
Instead of using private APIs you can do this by changing the property with KVO in the configuration.
Using Swift 5:
let configuration = WKWebViewConfiguration()
configuration.preferences.setValue(true, forKey: "fullScreenEnabled")
let webView = WKWebView(frame: .zero, configuration: configuration)
Tested on macOS 10.15 and 11
To allow WKWebView to use fullscreen HTML you need to access private API's (see https://github.com/WebKit/webkit/blob/master/Source/WebKit/UIProcess/API/Cocoa/WKPreferences.mm#L232-L240) in WKPreferences. Since you're using Swift in your bridging header add:
#import <WebKit/WebKit.h>
#interface WKPreferences ()
-(void)_setFullScreenEnabled:(BOOL)fullScreenEnabled;
#end
And simply call it:
let webView = WKWebView(frame: view.frame)
webView.configuration.preferences._setFullScreenEnabled(true)
If you notice strange resizing of the web view once you exit fullscreen I found that this fixes the issue for me:
webView.autoresizingMask = [.width, .height]
view.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
webView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
webView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
webView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
Note: Since you're accessing private API this would be rejected on the Mac App Store.
The solution posted by #SoneeJohn was clear, but it involved using a bridging header. I'd never manually worked with Objective-C or modified private Swift frameworks before. I didn't know what keywords to use. And, the resources I found seemed more involved and focused on creating an original framework - not altering an existing framework. I don't know if this is the proper implementation, but it got full-screen mode to work in web view.
Steps
There are photos that reference each of the following steps in the next section.
Click your "Project" in XCode
Click "Add a target"
Click "Cocoa Framework"
Click "Next"
Enter a title into "Product Name"
Click "Finish"
Click "Add items" in "Linked Frameworks and Libraries"
Enter "WebKit" into the "Search"
Click "WebKit.framework"
Click "Add"
Click your "Project.h" file
Add the "Import" code from the solution
#import <WebKit/WebKit.h>
Add the "Full Screen" code from the solution
#interface WKPreferences ()
-(void)_setFullScreenEnabled:(BOOL)fullScreenEnabled;
#end
Import your "Project Framework" into your swift file
Implement the "Full Screen" code into your web view
webView.configuration.preferences._setFullScreenEnabled(true)
I had to change my user agent to Safari 11 for the full-screen mode to work properly.
webView.customUserAgent = """
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13) AppleWebKit/604.1.31 (KHTML, like Gecko) Version/11.0 Safari/604.1.31
"""
The answer is that there is no answer. No API right now on mac to get fullscreen html5. I tried to make the same app on windows and the current WebView of UWP is able to go fullscreen (the old WebBrowser of WPF is not able to go fullscreen).
I think the only way to get it, it's to submit a feedback to apple.

Cocoa - XCode style notification window

Is the semi-transparant notification window which XCode 4 shows on Snow Leopard and on Lion a standard Cocoa control or is it something custom? I've seen more and more apps with similarly styled popups, and don't want to go about reinventing something if a much cleaner implementation exists in Cocoa. If this is a standard control, could someone tell me the name or point me to the documentation for it?
This is the popup window I am referring to:
This is custom but Matt Gemmell has sample code at http://mattgemmell.com/source. It's called RoundedFloatingPanel.

How to implement a UITabBar view in Mac OS X applications?

In my Mac application I'd like to implement a view that simulates UITabBar from iOS.
What's the preferred way to implement this? Use NSOutlineView , NSTableView, or something else?
For a native Mac app, the equivalent of a UITabBar is an NSTabView. If you want to go for the iOS look, you either have to roll your own with some NSView subclass or take a look at UMEKit.
Chameleon may also be an option, but it currently doesn't implement UITabBar, this might change in the future.
If you want to follow a trend of making your Mac app with the UIKit look-and-feel
you might want to take a look at the Chameleon Project - note the status of the project on the developer's blog. However, the project source is on github now, and people are helping out.

How to create a Menubar application for Mac

EDIT: This is a nice ready-made menubar application here (github source) by this answer.
I was wondering how to make a menubar application, what are the requirements for that to do so?
I saw a simple application for the menubar was to open links using your browser, I want to create something similar to that.
This is the application I like to make similar.
NSStatusItem is what you are looking for. Also add LSUIElement with string value of 1 to your Info.plist to hide it from Dock.
I've found Codebox's Popup to be a great starting point. It is ripe for forking on Github.
Though it works nicely, they do note on their site...
P. S. In Lion, Apple is adding a new class for popovers like in iOS.
So, after OS X 10.7 is released, you would better to rely on native
Cocoa classes where it is possible. In other cases, the Popup project
should still be usable.
BitBar is an application on GitHub that can "Put anything in your Mac OS X menu bar".
It runs shell or other executable scripts (which it calls Plugins - see the many examples in the plugins repo) and displays the results in the menu bar. You can write your own plugin and have it run simply by adding it to the 'Plugins folder'. As well as displaying information, it can also run pre-defined bash scripts interactively from the plugin menus you define.
Since I first posted this answer it's popularity has exploded (52 contributors currently) and there is now even a distributable version with which you can package your own plugins.
A very simple (non-interactive) example to show live Bitcoin price:
As Apple added NSStatusBarButton property to NSStatusItem in Yosemite, we can implement menubar app a lot simpler. I just created a sample project on github.
https://github.com/taichino/PopupTest
FlyCut is another nice open source application that does this. (MIT licensed.) Very handy too, I use it several times a day.
Here's some code that seems like it may be relevant:
// Flycut/AppController.h
IBOutlet NSMenu *jcMenu;
// Flycut/AppController.m
statusItem = [[[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setHighlightMode:YES];
if ( [[DBUserDefaults standardUserDefaults] integerForKey:#"menuIcon"] == 1 ) {
[statusItem setTitle:[NSString stringWithFormat:#"%C",0x2704]];
} else if ( [[DBUserDefaults standardUserDefaults] integerForKey:#"menuIcon"] == 2 ) {
[statusItem setTitle:[NSString stringWithFormat:#"%C",0x2702]];
} else {
[statusItem setImage:[NSImage imageNamed:#"com.generalarcade.flycut.16.png"]];
}
[statusItem setMenu:jcMenu];
[statusItem setEnabled:YES];
Mail Notifr is another open source Menubar app. It helped me a bunch, especially when I needed to figure out how to implement the open on login. Also available on the App Store.

how to add a menu into the Safari MenuBar or add a button into it's MainWindow?

I want to develop a plugin with Safari. Now I wan to make a demo for add a button into it's MainWindow or menu into it's MenuBar. I have seen to Document WebKitPluginProgTopic and the sample:/Developer/Examples/Webkit/WebKitMoviePlugIn . But they didn't tell the full process of make a plugin with safari and how to add some menu or button in the safari window . Someone can give me some advice or simple code to resolve this problem, Thank you very much!
The plug-in architecture isn't thought for enhancing the browser but to support special media formats like flash or quicktime. The problem is that the plugin is only loaded when the corresponding media type is found.
Agile Web Solutions have found a way to load the plug-in all sites in 1Password and insert code into Safari. They've documented it in their blog. For other means of injecting code have a look at this question: InputManager plug-ins in Snow Leopard.
I haven't got any experience in building menus programmatically, but it should work like this:
NSMenu *newMenu = [[[NSMenu alloc] initWithTitle:#"MyMenu"] autorelease];
NSMenu *mainMenu = [NSApp mainMenu];
[mainMenu addItem:newMenu];
Changing the windows isn't that easy. Your best guess is probably [NSApp orderedWindows], which exists only for scripting purposes, but should be usefull to your task.

Resources