Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
Background story: I'm on mac OS 11.6 and would like to access an identifier for the current space/desktop from the command line. I found a helpful article at https://ianyh.com/blog/identifying-spac...
#2: Post edited
Background story: I'm on mac OS 11.6 and would like to access an identifier for the current space/desktop from the command line. I found a helpful article at https://ianyh.com/blog/identifying-spaces-in-mac-os-x/ which describes a strategy to compare the output of `defaults read com.apple.spaces` with the list of windows on the current space. I don't really know how to puzzle together all the code fragments from this site, so I kept searching and found this implementation: https://gist.github.com/camillol/1129406- I tried to compile it with `clang++ spaceutil.c`, but get the following error:
- ```
- clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
- spaceutil.c:20:25: error: cannot initialize a variable of type 'CFDictionaryRef' (aka 'const __CFDictionary *') with an rvalue of type 'const void *'
- CFDictionaryRef windict = CFArrayGetValueAtIndex(windows, i);
- ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- spaceutil.c:21:21: error: cannot initialize a variable of type 'CFNumberRef' (aka 'const __CFNumber *') with an rvalue of type 'const void *'
- CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace);
- ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- spaceutil.c:21:62: warning: 'kCGWindowWorkspace' is deprecated: first deprecated in macOS 10.8 - No longer supported [-Wdeprecated-declarations]
- CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace);
- ^
- /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreGraphics.framework/Headers/CGWindow.h:106:30: note: 'kCGWindowWorkspace' has been explicitly marked deprecated here
- CG_EXTERN const CFStringRef kCGWindowWorkspace
- ^
- 1 warning and 2 errors generated.
- ```
- My clang++ version is
- ```
- clang++ --version
- Apple clang version 13.0.0 (clang-1300.0.29.30)
- Target: arm64-apple-darwin20.6.0
- Thread model: posix
- InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
- ```
- Any pointers how to fix this error?
- Background story: I'm on mac OS 11.6 and would like to access an identifier for the current space/desktop from the command line. I found a helpful article at https://ianyh.com/blog/identifying-spaces-in-mac-os-x/ which describes a strategy to compare the output of `defaults read com.apple.spaces` with the list of windows on the current space. I don't really know how to puzzle together all the code fragments from this site, so I kept searching and found this implementation: https://gist.github.com/camillol/1129406 :
- ```
- #include <unistd.h>
- #include <CoreServices/CoreServices.h>
- #include <ApplicationServices/ApplicationServices.h>
- typedef int CGSConnection;
- extern OSStatus CGSGetWorkspace(const CGSConnection cid, int *workspace);
- extern OSStatus CGSSetWorkspace(const CGSConnection cid, int workspace);
- extern CGSConnection _CGSDefaultConnection(void);
- int get_space_id(void);
- void set_space_by_index(int space);
- int get_space_id(void)
- {
- int space;
- CFArrayRef windows = CGWindowListCopyWindowInfo( kCGWindowListOptionOnScreenOnly, kCGNullWindowID );
- CFIndex i, n;
- for (i = 0, n = CFArrayGetCount(windows); i < n; i++) {
- CFDictionaryRef windict = CFArrayGetValueAtIndex(windows, i);
- CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace);
- if (spacenum) {
- CFNumberGetValue(spacenum, kCFNumberIntType, &space);
- return space;
- }
- }
- return -1;
- }
- void set_space_by_index(int space)
- {
- CFNotificationCenterRef nc = CFNotificationCenterGetDistributedCenter();
- CFStringRef numstr = CFStringCreateWithFormat(NULL, nil, CFSTR("%d"), space);
- CFNotificationCenterPostNotification(nc, CFSTR("com.apple.switchSpaces"), numstr, NULL, TRUE);
- }
- int main (int argc, char * const argv[])
- {
- int useCGS = 0, ch, space;
- while ((ch = getopt(argc, argv, "-c")) != -1) {
- switch (ch) {
- case 'c':
- useCGS = 1;
- break;
- }
- }
- argc -= optind;
- argv += optind;
- if (argc > 0) {
- space = atoi(argv[0]);
- if (useCGS) CGSSetWorkspace(_CGSDefaultConnection(), space);
- else set_space_by_index(space);
- } else {
- if (useCGS) CGSGetWorkspace(_CGSDefaultConnection(), &space);
- else space = get_space_id();
- printf("%d\n", space);
- }
- return 0;
- }
- ```
- I tried to compile it with `clang++ spaceutil.c`, but get the following error:
- ```
- clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated]
- spaceutil.c:20:25: error: cannot initialize a variable of type 'CFDictionaryRef' (aka 'const __CFDictionary *') with an rvalue of type 'const void *'
- CFDictionaryRef windict = CFArrayGetValueAtIndex(windows, i);
- ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- spaceutil.c:21:21: error: cannot initialize a variable of type 'CFNumberRef' (aka 'const __CFNumber *') with an rvalue of type 'const void *'
- CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace);
- ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- spaceutil.c:21:62: warning: 'kCGWindowWorkspace' is deprecated: first deprecated in macOS 10.8 - No longer supported [-Wdeprecated-declarations]
- CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace);
- ^
- /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreGraphics.framework/Headers/CGWindow.h:106:30: note: 'kCGWindowWorkspace' has been explicitly marked deprecated here
- CG_EXTERN const CFStringRef kCGWindowWorkspace
- ^
- 1 warning and 2 errors generated.
- ```
- My clang++ version is
- ```
- clang++ --version
- Apple clang version 13.0.0 (clang-1300.0.29.30)
- Target: arm64-apple-darwin20.6.0
- Thread model: posix
- InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
- ```
- Any pointers how to fix this error?
#1: Initial revision
cannot initialize a variable of type 'CFDictionaryRef'
Background story: I'm on mac OS 11.6 and would like to access an identifier for the current space/desktop from the command line. I found a helpful article at https://ianyh.com/blog/identifying-spaces-in-mac-os-x/ which describes a strategy to compare the output of `defaults read com.apple.spaces` with the list of windows on the current space. I don't really know how to puzzle together all the code fragments from this site, so I kept searching and found this implementation: https://gist.github.com/camillol/1129406 I tried to compile it with `clang++ spaceutil.c`, but get the following error: ``` clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated [-Wdeprecated] spaceutil.c:20:25: error: cannot initialize a variable of type 'CFDictionaryRef' (aka 'const __CFDictionary *') with an rvalue of type 'const void *' CFDictionaryRef windict = CFArrayGetValueAtIndex(windows, i); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ spaceutil.c:21:21: error: cannot initialize a variable of type 'CFNumberRef' (aka 'const __CFNumber *') with an rvalue of type 'const void *' CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ spaceutil.c:21:62: warning: 'kCGWindowWorkspace' is deprecated: first deprecated in macOS 10.8 - No longer supported [-Wdeprecated-declarations] CFNumberRef spacenum = CFDictionaryGetValue(windict, kCGWindowWorkspace); ^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreGraphics.framework/Headers/CGWindow.h:106:30: note: 'kCGWindowWorkspace' has been explicitly marked deprecated here CG_EXTERN const CFStringRef kCGWindowWorkspace ^ 1 warning and 2 errors generated. ``` My clang++ version is ``` clang++ --version Apple clang version 13.0.0 (clang-1300.0.29.30) Target: arm64-apple-darwin20.6.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin ``` Any pointers how to fix this error?