Post

Replies

Boosts

Views

Activity

updating CommandLine.arguments on repeated 'open -a' commands
Wanted to add some basic scripting capabilities for an AppKit Swift application on macOS. I looked at adding AppleScript support (i.e. Cocoa Scripting) and it, frankly, hurts my head. I then discovered that Swift provides for CommandLine.argc and Commandline.arguments. So, from Terminal, I can do open -a myApp --args arg1 arg2 arg3 and sure enough, MyApp starts and CommandLine.arguments contain arg1, arg2, arg3 along w/ the first entry which is the fully resolved path of the executable. Great! However, if myApp is already running when I execute open -a myApp --args arg4 CommandLine.arguments are not updated and I get the same values as when I originally started the program (i.e. arg1, arg2, arg3 and not arg4). So I added func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { parseCommandLine(); return true} to AppDelegate and while parseCommandLine is called for my second open -a command, it still points to the old CommandLine.arguments. Even tried returning false, but no difference. Is there a way for CommandLine to be refreshed if myApp is already running? Or a different way to get the same effect?
0
0
347
Mar ’24
Auto-Update macOS app outside of MacAppStore
I have a sandboxed/hardened app that is distributed outside of the MacAppStore. I want to allow the app to auto-update itself. I currently have the ability in the app to figure out if there is a newer version which then informs the user and gives them an option to download the app (currently it downloads to ~/Downloads in the app container). The app comes as a simple zip file that only includes the app itself. Once downloaded, then NSWorkspace.shared.open(URLtoDownloadedUpdate) will expand the zip and place the app in ~/Downloads. What I want is it to replace the current version of the app w/ this newly downloaded version. Is this possible? First, how do I find out where the old app is stored? And then how do I replace it with the new version when the old app is still running? I realize many people use Sparkle to do this instead of rolling their own. I simply do not want to depend on a third party.
3
0
1.1k
Sep ’23
only menu item Mac app
Prior to Ventura, my app that only has a Menu Item does not have a Storyboard but does have a custom main.swift as import Foundation import Cocoa let app = NSApplication.shared let delegate = AppDelegate() app.delegate = delegate _ = NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv) And then in Info.plist, I added Application is agent (UIElement) YES so the app isn't displayed in the dock. Occasionally, the app now crashes on start. Is there documentation for creating a Menu item only macOS app without Storyboard? It's typically crashing in _NSCGSDisplayConfigurationUpdateAndInvokeObservers or in _NSCGSDockMesageReceive. Thanks,
3
0
679
Jan ’23
CGWindowListCreateImage doesn't *always* return the correct image
Doing a guard let cgImage = CGWindowListCreateImage(.null, [.optionIncludingWindow], cgID, [.nominalResolution]) else { print ("problem!"); continue } where cgID is a CGWindowID for a Desktop background image almost always returns an CGImage of the Desktop (minus any icons on the Desktop, of course). However, under Monterey, there is a finite possibility of the returned image to be simply gray or some chopped up version of the actual Desktop. This usually happens when Spaces are changed and code is triggered to update the image from a NSWorkspace.shared.notificationCenter notification named: NSWorkspace.activeSpaceDidChangeNotification. Is there a way to detect when the image returned is not correct? The else in the guard is never triggered and the cgImage is the correct size, just the wrong content. In fact comparing a good cgImage to a bad cgImage, there doesn't appear to be any difference. Documentation for .optionIncludingWindow says You must combine this option with the optionOnScreenAboveWindow or optionOnScreenBelowWindow option to retrieve meaningful results. However, including either option (e.g. [.optionOnScreenBelowWindow, .optionIncludingWindow]) can still result in an incorrect image. As an aside, https://developer.apple.com/videos/play/wwdc2019/701/ at the 15:49 mark shows using only the optionIncludingWindow, so not sure which documentation is correct.
3
0
1.1k
Dec ’21
simultaneous collisions in scenekit
trying to make a simple simulation of a Newton's Cradle in Scenekit.if i have two balls starting in equilibrium and then give one ball an impulse, the dynamics are correct. the first ball comes back, hits the second ball. stops and all the momentum goes to the second ball. repeat. great!now three balls are placed in the cradle. again, take one of the end balls, give it an impulse so it flies up. comes back and collides. the two other balls move and the original ball bounces back. that's not physics. the correct behavior is the first ball after colliding, stops and the ball on the other end moves up. the center ball should not move. ever. apparently collisions are handled serially.playing w/ the timestep of the simulation doesn't appear to help (which also suggests collisions are handled serially).physically (for identical balls), during a collisin, the velocities should simply be swapped between pairs (assuming elastic collision). so i turned off collisions and used SCNPhysicsContactDelegate to be called when any two balls contact w/ func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) and then simply swapped the velocities between the two nodes or gave each an impulse that would do the same thing. Doesn't really work since, some balls overlap and appear to be stuck for a bit.i think what i need is a way to find *all* contacts in a timestep so i can handle simultaneous collisions correctly. alas, don't see how to do that.any brillant ideas?this also implies scenekit simulation of billards can't possibly work at least according to Newtonian physics. 😟
2
0
892
Nov ’19