OSX/Swift Get Frontmost Running Application - swift

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

Related

SwiftUI - How to know if a new app window has been opened after clicking the X button (Mac)?

I know there are functions in the AppDelegate like applicationDidFinishLaunching, but I haven't found a way to know when the app has been reopened after closing it (without quitting it) using the X button.
I'm trying to know which function (if any) is going to help me with that.
I'm trying to corroborate which one works by printing something, for example:
In applicationDidFinishLaunching I use print("App Launched").
In applicationShouldTerminateAfterLastWindowClosed I use print("Last Window Closed")
So I need one that theoretically prints App Reopened.
I've tried so many, including:
awakeFromNib
applicationDidBecomeActive
applicationDidUnhide
But none of them have work for me.
Thanks in advance.
I think the callback you're looking for is NSApplicaitonDelegate.applicationShouldHandleReopen(_:hasVisibleWindows:)
These events are sent whenever the Finder reactivates an already running application because someone double-clicked it again or used the dock to activate it.

Programmatically minimize all windows or besides my Cocoa application?

I haven't been able to find anything about this in Swift. Is there a way to programmatically make my application minimize all other windows open in the background, or even just minimize Safari? I basically want my application to run against the desktop, without any clutter in the background. Is there a way to programmatically do this for a Cocoa app? I'm pretty new to swift, so any help would be appreciated.
You can use api on NSWorkspace which allows you to hide all visible app in the background.
You can find more about NSWorkspace here: link
Hides all applications other than the sender. This method must be called from your app’s main thread.
NSWorkspace.shared().hideOtherApplications()
If you only want to hide Safari,
let appPath = NSWorkspace.shared().fullPath(forApplication: "Safari")
let identifier = Bundle.init(path: appPath!)?.bundleIdentifier
if let bundleID = identifier {
let runningApps = NSRunningApplication.runningApplications(withBundleIdentifier:bundleID )
if !runningApps.isEmpty {
runningApps.first?.hide()
}
}

How to simulate the very first start of an app in Xcode 6

I need to test some behaviors of the iOS 8 at the very first start of my app. Is it possible to simulate this in Xcode 6? If yes, then how?
Deleting the app will do it but note that certain pieces of information will be cached for a while like your permissions settings (notification, calendar, etc.). You can go to settings.app and reset settings to clear those out if that matters in your use case.
If you mean the FIRST start of the app, then what I did to achive this is, on start (viewDidLoad) check in the NSUserDefaults for example ,if the value "hasAlreadyStarted" exists (NSUserDefaults.objectForKey(..) ), if not, then its the first start of the app, and then i would set the value to true, so when you close the app, and open again, the value will exist.
Dareon I'm not sure what exactly you want to achieve. In Xcode 6 yes you can simulate your app from start. If you want to test behaviours I think you are looking for instruments. Right Click on the Xcode icon in your dock select option and choose instruments. You can add several instruments your phone or emulator support like connectivity or gps or memory to see exactly the behaviour of your app. Hope that helps
Well if by very start of the app cycle you mean before the app loads, there is a way.
In your ViewController call the ViewWillLoad function:
class ViewController
{
override func viewWillAppear(animated: Bool)
{
// your code
}
}
This event will be called before the view loads or appears.
Hope it helps :)
As Shanti K said in the comment, if you delete your application from the simulator and then run it again, you will be simulating the first run. To delete the application from the simulator, you mimic the same behavior on a device.
Click and hold on the icon, until they start shaking. Click the close X next to your application, and verify that you want to delete it if it asks. Then Shift + Command + H to simulate hitting the home button.

Make/detect the iPhone/iPad post back on double tap

In an asp.net web application - without writing a mobile specific site, is there anyway of stopping double tapping on an iPhone, from zooming? The default behaviour should be to produce a postback (and is on my Android, and in normal web browsers) - but the iPhone appears to take this as a zoom command.
So my query is, how do I get the iPhone/iPad to recognise that I actually want to do a postback?
Thanks for any help,
Mark
if anyone is interested, my specific issue was, I had a popup on each cell of a grid (onmouseover...) - however, to start the postback, you have to actually click on a cell in the the grid, rather than hover over it. However, "hovering" on an iPhone is basically "clicking" on everything else - so when people were clicking, all that was happening, was the popup was showing again, and no postback was carried out - meaning people could not progress.
Solution was to detect for iPhone/Pad/Touch, and not output the "onmouseover" if those devices were detected - then the underlying click was available, and would do as designed.
Code to check for iPhone etc:
Dim iphone as Boolean = False
Dim userAgent = HttpContext.Current.Request.UserAgent.ToLower()
' iPhone is there
If userAgent.Contains("iphone") Then
iphone = True
ElseIf userAgent.Contains("ipad") Then
iphone = True
ElseIf userAgent.Contains("itouch") Then
iphone = True
End If
Hope this helps anyone else with this specific issue,

Eclipse - Blackberry SDK - Debugging on device: "details unavailable - not supported by VM"

I'm having a strange problem while debugging my Blackberry Application on a real device (BB Bold 9700). When I debug the same application within the BB emulator, the app runs fine, but when I run it on the real device, the app behaves differently (custom painting goes completely wrong). What's even worse is that my Eclipse environment seems to be unable to view live objects correctly while being at a break point (debug time).
I've added a screenshot to illustrate the strange behaviour:
As you can see, the app stops at the breakpoint within the IF statement, but the Variables pane says that the variable "methodName" equals null. Moreover, when I want to look at the variable "methodArguments" which is of type org.json.me.JSONArray, it says "details unavailable - not supported by VM".
Does anyone know what's going on here? My app works great on the emulator, but it's currently useless on the real device.
Thanks in advance!
I think I fixed it:
The problem was that I was laying out Fields on a manager that wasn't yet added to the viewstack.
What did the trick for me was overriding onDisplay() in the manager that contained the Fields that were displayed wrong:
protected void onDisplay()
{
//Make sure superclass is called
super.onDisplay();
/*You have to call "this.setDirty(true)" when you perform layout on a
*manager that isn't added to the viewstack. Then you can use
*"this.isDirty()" to determine whether you need to re-layout the fields
*when the manager becomes visible.*/
if(this.isDirty())
{
//I'm not sure if I need to use "invokeAndWait" and not "invokeLater"
UiApplication.getUiApplication().invokeAndWait(new Runnable()
{
public void run()
{
for (int i = 0; i < getFieldCount(); i++)
{
/*This (custom) function makes sure the Field gets its
*size and position*/
layoutItem(getField(i));
}
}
});
//Make sure you set "dirty" to false, to make sure this only happens once
this.setDirty(false);
}
}
If anyone has a better solution, I'd be glad to hear it (and maybe improve my app).
org.json.me.JSONArray, it says "details unavailable - not supported by VM".
the JSON related stuff is not available on device running 4.5 and 4.6 BB OS. Import it into the code.
Download it from here.
https://github.com/douglascrockford/JSON-java
it is available as Open Source and then use it into your applications.