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?