-
-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add .isAccessGranted()
method
#90
Changes from 16 commits
236ae03
00a625c
c083464
b521fe5
7a13e1d
270407a
9d8f650
3c5fa26
0085035
f31a0af
e8583c2
ac3bbe0
e74a431
4a5797a
38bf339
440d4a9
fa2c680
44b2559
c196eaa
1e0b86e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ yarn.lock | |
/.build | ||
/*.xcodeproj | ||
/main | ||
/is-access-granted |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* Executable that will return a JSON with the granted permissions: | ||
* { | ||
* "isAccessibilityGranted": boolean | ||
* "isScreenRecordingGranted": boolean | ||
* } | ||
* | ||
*/ | ||
|
||
import AppKit | ||
|
||
var dictionary: [String: Any] = [ | ||
"isAccessibilityGranted": AXIsProcessTrustedWithOptions( | ||
["AXTrustedCheckOptionPrompt": false] as CFDictionary), | ||
"isScreenRecordingGranted": isScreenRecordingGrantedNoDialog() | ||
] | ||
|
||
print(try! toJson(dictionary)) | ||
|
||
exit(0) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import AppKit | ||
|
||
func toJson<T>(_ data: T) throws -> String { | ||
let json = try JSONSerialization.data(withJSONObject: data) | ||
return String(decoding: json, as: UTF8.self) | ||
} | ||
|
||
/** | ||
* Returns true if the Screen recording is granted by the user for this app. False | ||
* otherwise. | ||
* This method will not prompt the user with a dialog to grant permissions | ||
* CREDITS: https://gist.github.com/soffes/da6ea98be4f56bc7b8e75079a5224b37 | ||
*/ | ||
func isScreenRecordingGrantedNoDialog() -> Bool { | ||
// The screen recording security was introduced in version 10.15 of MAC os | ||
if #available(macOS 10.15, *) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
let runningApplication = NSRunningApplication.current | ||
let processIdentifier = runningApplication.processIdentifier | ||
|
||
guard let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) | ||
as? [[String: AnyObject]] else | ||
{ | ||
assertionFailure("Invalid window info") | ||
return false | ||
} | ||
|
||
for window in windows { | ||
// Get information for each window | ||
guard let windowProcessIdentifier = (window[String(kCGWindowOwnerPID)] as? Int).flatMap(pid_t.init) else { | ||
assertionFailure("Invalid window info") | ||
continue | ||
} | ||
|
||
// Don't check windows owned by this process | ||
if windowProcessIdentifier == processIdentifier { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
continue | ||
} | ||
|
||
// Get process information for each window | ||
guard let windowRunningApplication = NSRunningApplication(processIdentifier: windowProcessIdentifier) else { | ||
// Ignore processes we don't have access to, such as WindowServer, which manages the windows named | ||
// "Menubar" and "Backstop Menubar" | ||
continue | ||
} | ||
|
||
if window[String(kCGWindowName)] as? String != nil { | ||
if windowRunningApplication.executableURL?.lastPathComponent == "Dock" { | ||
// Ignore the Dock, which provides the desktop picture | ||
continue | ||
} else { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use tab-indentation |
||
} else { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,14 @@ Returns an `Object` with the result, or `undefined` if there is no active window | |
- `url` *(string?)* - URL of the active browser tab if the active window is Safari, Chrome, Edge, or Brave *(macOS only)* | ||
- `memoryUsage` *(number)* - Memory usage by the window owner process | ||
|
||
### activeWin.isAccessGranted() | ||
|
||
Check if the package has enough access to get the active window. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not correct. It always has access, just not access to the window title, etc. |
||
|
||
Returns `true` if there is enough access, `false` otherwise. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is outdated. |
||
|
||
To prompt the user with a dialog to give access, call `activeWin()` or `activeWin.sync()`. | ||
|
||
## OS support | ||
|
||
It works on macOS, Linux, and Windows 7+. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not how you write macOS.