How can I make an OS X application unfocusable while still receiving click events? - swift

I'm developing an on screen keyboard application for OS X, similar to the one that's built in to the operating system (Keyboard Viewer). I seem to have hit a wall as I'm not sure how I can accept click events from buttons and not steal focus from the currently activated application. I know this is possible since there are apps that already do this, e.g. AssistiveWere's KeyStrokes.
So my question is this: How can I make my window receive mouse events and handle them without getting activated?
P.S. I'm not very experienced in OS X development and this is my first Swift project, so excuses if this is a trivial problem.

You need to make your window an instance of NSPanel (or a subclass), include NSNonactivatingPanelMask in its styleMask, and set becomesKeyOnlyIfNeeded to true. (The style mask can be controlled in IB.) You probably also want it to be floating so it's always above normal windows, so set floatingPanel to true, too.

Related

How to close System dialogs that appears on app crash?

I'm using xcuitest framework to automate mac application. I get system dialogs when the app is opened again after it crashes. I want to handle the dialog programmatically. But the dialog appears under the process `UserNotificationCenter' instead of the application under test. How can I handle the alert in such case?
You have two options:
Use InterruptionMonitor (documentation, use-case). This
approach is however kinda old and I found, that it does not work for
all dialogs and situations.
Create a method, which will wait for some regular app's button. If the app's button (or tab bar or other such XCUIElement) is visible and hittable after your app started, you can proceed with your test and if it's not, you can wait for the UserNotificationCenter dialog's button and identify&tap it by its string/position.
I'm using the second approach and its working much better, than the InterruptionMonitor. But it really depends on your app layout and use-case.
You should be able to revent it from appearing in the first place. Something like:
defaults write com.apple.CrashReporter DialogType none

Is it possible to write a custom TouchBar app for an existing application?

I have tried looking for an answer to this but can't seem to see anything about it.
I have a piece of software on my laptop (not written by me), that I want to customise. Some of the tasks I do are very repetitive, and could be simplified by just pressing a button on the Touch Bar that executes a number of commands.
Is it possible to write a Touch Bar app that either:
1) Customises the application's original TouchBar app (for which there is none)
2) Runs only when that application is running, and hides when the application is out of focus
Thanks
I believe BetterTouchTool can do that.
Also in the "App Specific" button you can customise when to hide system touch bar and when to show it again

Create a fixed position window in Mac Application development

I'm new in Mac Application development. I want to create a small tool which having a small window fixed position at the right bottom of the screen like this picture:
Is it possible to do that?
It's possible - but I wouldn't recommend it, as it could be a bad user experience.
The solution is simple, though: set the .isMovable property of the window to false.
For example, for a window IBOutlet, setting:
window.isMovable = false
in applicationDidFinishLaunching works well.
You can also, for example, use window.setFrameOrigin(_ point: NSPoint) to first set it at a fixed position.

Dock "Turn Hiding On" programmatically from within an app

If you secondary-click on the Dock you can click the Turn Hiding On option to automatically hide the Dock. Alternatively, you can go to System Preferences > Dock and click the Automatically hide and show the Dock.
I want to mimic that functionality from within an app I am making (which is basically a status bar icon app) and preferably in Swift.
The code I have written so far to turn on the Dock Automatic Hiding functionality is the following:
// Update the value for key "autohide" in com.apple.dock.plist, located in ~/Library/Preferences/.
var dict = NSUserDefaults.standardUserDefaults().persistentDomainForName("com.apple.dock")
dict.updateValue(true, forKey: "autohide")
NSUserDefaults.standardUserDefaults().setPersistentDomain(dict, forName: "com.apple.dock")
// Send notification to the OS.
dispatch_async(dispatch_get_main_queue()) {
CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(), "com.apple.dock.prefchanged", nil, nil, true)
}
The first part of the code updates a value in a plist file and I have confirmed that that is working. The second part sends a notification to the OS to tell it that a value has been changed in that plist, which I have also confirmed to be working.
However, these two things are not making the Dock hide, making me believe I need to do something else. Or made my approach to the problem is wrong? How do I make the Dock start hiding?
PS: I have read something about a private, undocumented API called CoreDock, but I would like to avoid going that way, as it may cause many problems...
Almost certainly better to use AppleScript or the Scripting Bridge to do this. The following script turns Dock autohiding on:
tell application "System Events"
set autohide of dock preferences to true
end tell
You can run that using NSAppleScript.

How to implement iPhone TitleBar in Android

You know in iPhone, The four components of a typical iPhone application are
a title bar,
a navigation list,
a destination page,
and a button bar.
for The Title Bar
The title bar includes the following elements:
Back button:
Screen title:
Command button:
My question comes, although i can use UI framework tool, such as:
Phonegap,
iui,
jtquery
to develop web app, but i can't display the TitleBar effect in Android,
i can't use below code in Android:
meta content="yes" name="apple-mobile-web-app-capable"
Actually, the above can work in Apple Safari Browser, but I can't use Safari to display our Android Project, which is i don't want to see.
Does anyone know how to fulfill this effect in Android, please help me, your help will be great appreciated.
to
"It's unclear if you developing a native android application or a webapp.
– alexanderblom yesterday"
sorry that i forget to mention, i want to develop a iphone-webapp-style application in Android. Since it's too difficult, i want to use iWebkit, but it seems only work in apple iPhone OS or Safari which contain the specific engine to display the beautiful layout, like below:( -_-! i am new so i can't post images here)
(source: appshopper.com)
If I'm not mistaken, if you want to custom in any way the title bar at the top of your screen, you have to do several things :
First, read this thread about titlebar customisation, as it's quite detailled.
Then, you have to add a few more things to it to match your requirements, all in the same xml file which will be your titlebar.xml:
-a button, on which listener you assign the same keyEvent as the normal back key of every android phone.
-a textview for whatever title you want to put in.
-a button for the command button, for whatever it does in Iphone (if you tell me what it does, maybe i'll be able to give more details)
Then, as I take it, you want to have it available on all your pages, without having to request titlebar customization on every activity, I would recommend you to use the same trick as I did : Define a superclass, which all your activities will extend.
then you you requestwindowfeatures in this class, and do all your customization in this one.Finnaly, You just have to call the variable of your titlebar textview in the oncreate of all your subclasses to assign the string you want on each page. If your string is generated dynamically from, lets say a file name you load from the internetv in a background thread, you will need to define a handler to the main thread as you can't update a view from another thread than the one that has created it.
Hopes that helps. If you have any question about that, feel free to ask, i'll keep an eye on your topic, because I had quite a pain to make my titlebar work properly.
good luck