forked from jerrykrinock/ClassesObjC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSSYAppLSUI.m
114 lines (98 loc) · 3.64 KB
/
SSYAppLSUI.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#import "SSYAppLSUI.h"
//#import "NSMenu+Initialize.h"
@implementation SSYAppLSUI
+ (BOOL)isLSUIElement {
ProcessSerialNumber psn = { 0, kCurrentProcess } ;
NSDictionary* info = nil ;
info = (NSDictionary*)ProcessInformationCopyDictionary (&psn, kProcessDictionaryIncludeAllInformationMask) ;
BOOL is = [[info objectForKey:@"LSUIElement"] boolValue] ;
if (info != NULL) {
CFRelease((CFDictionaryRef)info) ;
}
return is ;
}
+ (pid_t)inactivateActiveAppAndReturnNewActiveApp {
//NSLog(@"1000 Hiding Current ActiveApp: %@", [[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationName"]) ;
pid_t activeAppPid = [[[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationProcessIdentifier"] intValue] ;
OSStatus err;
ProcessSerialNumber psn ;
err = GetProcessForPID(activeAppPid, &psn) ;
if (err != noErr) {
NSLog(@"Internal Error 915-9384 %ld", (long)err) ;
}
err = ShowHideProcess(&psn, false) ;
if (err != noErr) {
NSLog(@"Internal Error 915-9385 %ld", (long)err) ;
}
//NSLog(@"2000 New ActiveApp: %@", [[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationName"]) ;
activeAppPid = [[[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationProcessIdentifier"] intValue] ;
return activeAppPid ;
}
+ (void)bringFrontPid:(pid_t)pid {
// Remember that any NSLogs early in the app results in two "Help" menus
//NSLog(@"ActiveApp: %@", [[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationName"]) ;
sleep(2) ;
ProcessSerialNumber psn ;
OSStatus err ;
err = GetProcessForPID(pid, &psn) ;
if (err != noErr) {
NSLog(@"Internal Error 915-9386 %ld", (long)err) ;
}
SetFrontProcess(&psn);
}
+ (void)transformToGui {
if ([self isLSUIElement]) {
#if 0
ProcessSerialNumber psn;
pid_t pid = getpid();
OSStatus err;
err = ShowHideProcess(&psn, true);
err = TransformProcessType(&psn, kProcessTransformToForegroundApplication);
[NSMenu setMenuBarVisible:NO];
SetFrontProcess(&psn);
[NSMenu setMenuBarVisible:YES];
// [NSMenu setMenuBarVisible:YES] ; // causes The Dreaded Two Help Menus if run in app delegate's -init
// More stuff which doesn't work
// At this point, this app will show in the dock, but the menu
// will not show unless you activate some other app and then
// re-activate this app. I tried a bunch of stuff but nothing
// worked reliably
BOOL yes = YES ;
NSInvocation* invocation = [NSInvocation invocationWithTarget:[NSMenu class]
selector:@selector(setMenuBarVisible:)
retainArguments:YES
argumentAddresses:&yes] ;
[invocation performSelector:@selector(invoke)
withObject:nil
afterDelay:1.0] ;
[invocation performSelector:@selector(invoke)
withObject:nil
afterDelay:2.0] ;
[invocation performSelector:@selector(invoke)
withObject:nil
afterDelay:3.0] ;
for (NSWindow* window in [NSApp windows]) {
[window display] ;
usleep(500000) ;
[NSMenu setMenuBarVisible:NO];
[window makeKeyAndOrderFront:self] ;
usleep(500000) ;
[NSMenu setMenuBarVisible:YES];
}
[NSApp activateIgnoringOtherApps:YES] ;
// [[[[[NSApp mainMenu] itemArray] objectAtIndex:0] submenu] performActionForItemAtIndex:0] ;
#else
ProcessSerialNumber psn = { 0, kCurrentProcess } ;
OSStatus err ;
err = TransformProcessType(&psn, kProcessTransformToForegroundApplication) ;
if (err != noErr) {
NSLog(@"Internal Error 915-9387 %ld", (long)err) ;
}
[NSApp activateIgnoringOtherApps:YES] ;
#endif
}
}
+ (void)transformToGui:(id)sender {
[self transformToGui] ;
}
@end