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
357
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
696
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