I am trying to add my own button on top of the window of third party app's windows OR modify the alpha (kCGWindowAlpha I think) of third party app windows in MacOS. I don't need this app to be sandboxed.
I have been able to get details of third party windows using:
#IBAction func printWindows(_ sender: NSButton?) {
let runningApps = NSWorkspace.shared.runningApplications
print("runningApps: \(runningApps)")
if let info = CGWindowListCopyWindowInfo(.optionAll, kCGNullWindowID) as? [[ String : Any]] {
for dict in info {
print("dict: \(dict)")
print("type: \(type(of:dict))")
print("kCGWindowOwnerName: \(dict[kCGWindowOwnerName as String])")
}
}
}
This gives me a lot of details of all the app windows opened. However, I am unable to figure out how I can use that info to modify the third party window. Can you direct me in the right direction on how I can achieve something like this?
So far, I have seen this code which lets me get screenshots of windows:
https://github.com/sassman/son-of-grab/blob/main/Controller.m
Is this something which Services are for? My reading of Services indicates that it's inly meant for clipboard/pasteboard stuff:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/SysServices/Articles/providing.html
Is this something which can be achieved via Accessibility APIs?
I've developed a command palette for macOS. Over the last few days I've figured out a way to show the Notification Center inside my app.
I would like to mimic the behaviour of the real Notification Center when clicking a notification: open the corresponding tab when clicking in a browser notification.
However whenever I cannot figure out a way to do it programmatically, I can only tell macOS to open the link and it automatically opens the default browser and creates a new tab.
let url = URL(string: "https://www.google.com")! // this opens a new tab everytime
if NSWorkspace.shared.open(url) {
print("default browser was successfully opened")
}
As for the notification itself I do get all the info it contains: the app bundle id, the notification payload, and even a link (e.g. n#https://web.whatsapp.com#465718293123#c.us). I've seen some answers that rely on AppleScript but I would rather avoid it if possible. But if not possible... then happy to fallback to it.
Any ideas how to achieve this? Many thanks!
P.D. I've also tried some variations of the link such as: arc://web.whatsapp.com#1234567 or arc://n#web.whatsapp.com#1234567. At most this focuses on the browser but not on the tab.
Is there any way of testing the UIApplication shortcuts within XCUITests?
I know that in order to test 3d shortcuts in a simulator you need a trackpad with force touch, but I was wondering if I could write tests that test my shortcuts.
Looks like yes! You'll need to expose a private API, though. Exported XCUIElement header from Facebook's WebDriverAgent here.
Or, if that's the only thing you need, just expose it ala:
#interface XCUIElement (Private)
- (void)forcePress;
#end
And then to force press your icon by going to the springboard and getting your icon element see my answer here.
class Springboard {
// this is another private method you'll need to expose (see linked other answer)
static let springboard = XCUIApplication(privateWithPath: nil, bundleID: "com.apple.springboard")
/// Open the 3d touch menu
class func open3dTouchMenu() {
XCUIApplication().terminate() // this may or may not be necessary depending on your desired function / app state
// Resolve the query for the springboard rather than launching it
springboard.resolve()
// Force delete the app from the springboard
let icon = springboard.icons["MyAppName"]
if icon.exists {
icon.forcePress()
// do something with the menu that comes up
}
}
}
* I have not tested this yet.
I'm new to swift and osx programming in general, and I need help with one thing that I can't seem to find an answer for. I'm trying to get the frontmost application running on a computer through an app. Right now I'm able to detect when a target application opens, but not when it is in front of all other windows or when it goes into the background.
What I have so far is:
var workspace = NSWorkspace.sharedWorkspace()
var applications = workspace.runningApplications
for app in applications {
let x = "LolClient"
if app.localizedName == x {
println("League is open")
}
}
That will just tell me when a target app opens. I just need to identify what app is in front of all others... basically which one is recieving keystrokes etc. What code would I need to do this? Thank you in advance.
I think you want NSWorkspace.shared.frontmostApplication.
NSWorkspace.shared.frontmostApplication
For the name of the app:
NSWorkspace.shared.frontmostApplication?.localizedName
I'm trying to programmatically return to the home screen in an iOS8 App using Swift. I want to continue the application running in the background though. Any ideas on how to do this?
Thanks in advance for the help.
When an app is launched, the system calls the UIApplicationMain function; among its other tasks, this function creates a singleton UIApplication object. Thereafter you access the object by calling the sharedApplication class method.
To exit gracefully (the iOS developer library explicitly warns you not to use exit(0) because this is logged as a crash ) you can use:
UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
For example, my app exits when the user shakes the device. So, in ViewController.swift:
override func motionEnded(motion: UIEventSubtype,
withEvent event: UIEvent?) {
if motion == .MotionShake{
//Comment: to terminate app, do not use exit(0) bc that is logged as a crash.
UIControl().sendAction(Selector("suspend"), to: UIApplication.sharedApplication(), forEvent: nil)
}}
Swift 4:
UIControl().sendAction(#selector(NSXPCConnection.suspend),
to: UIApplication.shared, for: nil)
Edit: It's worth mentioning that in iOS 12 there's a bug that will prevent network connectivity if the app is brought back from background after sending the suspend action.
For that you should use following code
import Darwin
exit(0)
To force your app into the background, you can legally launch another app, such as Safari, via a URL, into the foreground.
See: how to open an URL in Swift3
UIApplication.shared.open() (and the older openURL) are a documented public APIs.
If you set the exits-on-suspend plist key, opening another app via URL will also kill your app. The use of this key is a documented legal use of app plist keys, available for any app to "legally" use.
Also, if your app, for some impolite reason, continues to allocate and dirty large amounts of memory in the background, without responding to memory warnings, the OS will very likely kill it.
How about setting of info.plist?
You can set "Application does not run in background" true in info.plist with editor.
Or Add this lines with code editor.
<key>UIApplicationExitsOnSuspend</key>
<true/>
There is no way to "programmatically return to the home screen" except for crashing, exiting your program or calling unofficial API. Neither is welcome by Apple. Plus the Human Interface Guidelines are also covering this.