Skip to content

Commit

Permalink
Mac: change way of hiding command-line arguments from openFile
Browse files Browse the repository at this point in the history
NSApplication treats command-line arguments an application as strings
that should be forwarded to the application object's `openFile`
method, but that's not what we want. It turns out that there has
always (or at least since OS X 10.5) been an undocumented
configuration option "NSTreatUnknownArgumentsAsOpen" to turn that off.
  • Loading branch information
mflatt committed Sep 27, 2024
1 parent d8ee912 commit 7d03dc1
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions gui-lib/mred/private/wx/cocoa/queue.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,27 @@
queue-event
yield)

(import-class NSApplication NSAutoreleasePool NSColor NSProcessInfo NSArray NSMenu NSThread)
(import-class NSApplication NSAutoreleasePool NSColor NSProcessInfo NSArray NSMenu NSThread
NSUserDefaults NSDictionary)

(unless (tell #:type _BOOL NSThread isMainThread)
(error 'racket/gui
"cannot instantiate in a non-main place on Mac OS"))

;; Extreme hackery to hide original arguments from
;; NSApplication, because NSApplication wants to turn
;; the arguments into `application:openFile:' calls.
;; To hide the arguments, we replace the implementation
;; of `arguments' in the NSProcessInfo object.
(define (hack-argument-replacement self method)
(tell NSArray
arrayWithObjects: #:type (_vector i _NSString) (vector (path->string (find-system-path 'exec-file)))
count: #:type _NSUInteger 1))
(let ([m (class_getInstanceMethod NSProcessInfo (selector arguments))])
(void (method_setImplementation m hack-argument-replacement)))
;; Hide original arguments from NSApplication, because NSApplication
;; otherwise turns the arguments into `application:openFile:' calls.
(void
(tell (tell NSUserDefaults standardUserDefaults)
registerDefaults:
(tell NSDictionary
dictionaryWithObjects:
(tell NSArray
arrayWithObjects: #:type (_vector i _NSString) (vector "NO")
count: #:type _NSUInteger 1)
forKeys:
(tell NSArray
arrayWithObjects: #:type (_vector i _NSString) (vector "NSTreatUnknownArgumentsAsOpen")
count: #:type _NSUInteger 1))))

(define app (tell NSApplication sharedApplication))

Expand Down

0 comments on commit 7d03dc1

Please sign in to comment.