Skip to content

Commit

Permalink
Extend public.open.application with hideIfNotAvailable feature. Set t…
Browse files Browse the repository at this point in the history
…his key to YES in the feature settings and the menu item will be automatically hidden if the target app isn't available on the local system.

This close #12
  • Loading branch information
ygini committed Mar 23, 2016
1 parent 5e2c6a8 commit c7f54ba
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
48 changes: 28 additions & 20 deletions Hello IT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -332,26 +332,7 @@
E1D067FC1B51D08E00567172 /* Hello IT */ = {
isa = PBXGroup;
children = (
F8340B781C96CF9000C820D7 /* statusbar-dark.png */,
F8340B791C96CF9000C820D7 /* [email protected] */,
F8340B7A1C96CF9000C820D7 /* statusbar-error-dark.png */,
F8340B7B1C96CF9000C820D7 /* [email protected] */,
F8340B7C1C96CF9000C820D7 /* statusbar-error.png */,
F8340B7D1C96CF9000C820D7 /* [email protected] */,
F8340B7E1C96CF9000C820D7 /* statusbar-ok-dark.png */,
F8340B7F1C96CF9000C820D7 /* [email protected] */,
F8340B801C96CF9000C820D7 /* statusbar-ok.png */,
F8340B811C96CF9000C820D7 /* [email protected] */,
F8340B821C96CF9000C820D7 /* statusbar-unavailable-dark.png */,
F8340B831C96CF9000C820D7 /* [email protected] */,
F8340B841C96CF9000C820D7 /* statusbar-unavailable.png */,
F8340B851C96CF9000C820D7 /* [email protected] */,
F8340B861C96CF9000C820D7 /* statusbar-warning-dark.png */,
F8340B871C96CF9000C820D7 /* [email protected] */,
F8340B881C96CF9000C820D7 /* statusbar-warning.png */,
F8340B891C96CF9000C820D7 /* [email protected] */,
F8340B8A1C96CF9000C820D7 /* statusbar.png */,
F8340B8B1C96CF9000C820D7 /* [email protected] */,
F86928FA1CA28B8800AB8E4C /* statusbar */,
E1291C081BF11947008D7D90 /* Reachability.h */,
E1291C091BF11947008D7D90 /* Reachability.m */,
E1D067FF1B51D08E00567172 /* AppDelegate.h */,
Expand Down Expand Up @@ -391,6 +372,33 @@
name = "Supporting Files";
sourceTree = "<group>";
};
F86928FA1CA28B8800AB8E4C /* statusbar */ = {
isa = PBXGroup;
children = (
F8340B781C96CF9000C820D7 /* statusbar-dark.png */,
F8340B791C96CF9000C820D7 /* [email protected] */,
F8340B7A1C96CF9000C820D7 /* statusbar-error-dark.png */,
F8340B7B1C96CF9000C820D7 /* [email protected] */,
F8340B7C1C96CF9000C820D7 /* statusbar-error.png */,
F8340B7D1C96CF9000C820D7 /* [email protected] */,
F8340B7E1C96CF9000C820D7 /* statusbar-ok-dark.png */,
F8340B7F1C96CF9000C820D7 /* [email protected] */,
F8340B801C96CF9000C820D7 /* statusbar-ok.png */,
F8340B811C96CF9000C820D7 /* [email protected] */,
F8340B821C96CF9000C820D7 /* statusbar-unavailable-dark.png */,
F8340B831C96CF9000C820D7 /* [email protected] */,
F8340B841C96CF9000C820D7 /* statusbar-unavailable.png */,
F8340B851C96CF9000C820D7 /* [email protected] */,
F8340B861C96CF9000C820D7 /* statusbar-warning-dark.png */,
F8340B871C96CF9000C820D7 /* [email protected] */,
F8340B881C96CF9000C820D7 /* statusbar-warning.png */,
F8340B891C96CF9000C820D7 /* [email protected] */,
F8340B8A1C96CF9000C820D7 /* statusbar.png */,
F8340B8B1C96CF9000C820D7 /* [email protected] */,
);
name = statusbar;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down
4 changes: 2 additions & 2 deletions Hello IT/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.1.0</string>
<string>1.1.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>70</string>
<string>71</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down
29 changes: 29 additions & 0 deletions Plugins/OpenApplication/OpenApplication/HITPOpenApplication.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define kHITPOpenApplicationURL @"appURL"
#define kHITPOpenApplicationArgsArray @"args"

#define kHITPOpenApplicationHideIfNotAvailable @"hideIfNotAvailable"

#import <asl.h>

@interface HITPOpenApplication ()
Expand All @@ -22,6 +24,9 @@ @interface HITPOpenApplication ()

@property NSURL *appURL;
@property NSArray *args;

@property BOOL hideIfNotAvailable;

@end

@implementation HITPOpenApplication
Expand All @@ -32,17 +37,41 @@ - (instancetype)initWithSettings:(NSDictionary*)settings
if (self) {
_application = [settings objectForKey:kHITPOpenApplicationName];
_file = [[settings objectForKey:kHITPOpenApplicationFileToOpen] stringByExpandingTildeInPath];
_hideIfNotAvailable = [[settings objectForKey:kHITPOpenApplicationHideIfNotAvailable] boolValue];

NSString *appPath = [settings objectForKey:kHITPOpenApplicationURL];
if (appPath) {
_appURL = [NSURL fileURLWithPath:appPath];
_args = [settings objectForKey:kHITPOpenApplicationArgsArray];

if (!_args) {
_args = @{};
}
}

[self hideItemIfNeeded];
}
return self;
}

- (void)hideItemIfNeeded {
if (self.hideIfNotAvailable) {
if (self.application) {
self.menuItem.hidden = [[NSWorkspace sharedWorkspace] fullPathForApplication:self.application] == nil;
if (self.menuItem.hidden) {
asl_log(NULL, NULL, ASL_LEVEL_INFO, "Menu item %s for %s is hidden, workspace unable to find requested app.", [self.menuItem.title cStringUsingEncoding:NSUTF8StringEncoding], [self.application cStringUsingEncoding:NSUTF8StringEncoding]);
}
} else {
self.menuItem.hidden = ![[NSFileManager defaultManager] fileExistsAtPath:[self.appURL path]];
if (self.menuItem.hidden) {
asl_log(NULL, NULL, ASL_LEVEL_INFO, "Menu item %s for app at path %s is hidden, path does not exist.", [self.menuItem.title cStringUsingEncoding:NSUTF8StringEncoding], [[self.appURL path] cStringUsingEncoding:NSUTF8StringEncoding]);
}
}
} else {
self.menuItem.hidden = NO;
}
}

- (void)mainAction:(id)sender {
if (self.application) {
asl_log(NULL, NULL, ASL_LEVEL_INFO, "User requested to open %s with optional file %s.", [self.application cStringUsingEncoding:NSUTF8StringEncoding], [self.file cStringUsingEncoding:NSUTF8StringEncoding]);
Expand Down

0 comments on commit c7f54ba

Please sign in to comment.