Skip to content

Commit

Permalink
Adds parsing a second JSON file applescript changes
Browse files Browse the repository at this point in the history
Adds the ability to parse a second JSON file. The first JSON file will use the global settings. The second JSON file ignores the global settings.
  • Loading branch information
Matt Turner committed Nov 16, 2018
1 parent d4742b3 commit 7142655
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Shuttle.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Shuttle/Shuttle-Prefix.pch";
INFOPLIST_FILE = "Shuttle/Shuttle-Info.plist";
MACOSX_DEPLOYMENT_TARGET = 10.8;
MACOSX_DEPLOYMENT_TARGET = 10.9;
PRODUCT_BUNDLE_IDENTIFIER = "shuttle.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
Expand All @@ -470,7 +470,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Shuttle/Shuttle-Prefix.pch";
INFOPLIST_FILE = "Shuttle/Shuttle-Info.plist";
MACOSX_DEPLOYMENT_TARGET = 10.8;
MACOSX_DEPLOYMENT_TARGET = 10.9;
PRODUCT_BUNDLE_IDENTIFIER = "shuttle.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
Expand Down
6 changes: 6 additions & 0 deletions Shuttle/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,29 @@

//This is for the JSON File
NSDate *configModified;
NSDate *configModified2;
NSDate *sshConfigUser;
NSDate *sshConfigSystem;

//Global settings Pref in the JSON file.
NSString *shuttleJSONPathPref; //Alternate path the JSON file
NSString *shuttleJSONPathAlt; //alternate path to the second JSON file
NSString *shuttleAltConfigFile; //second shuttle JSON file
NSString *terminalPref; //Which terminal will we be using iTerm or Terminal.app
NSString *editorPref; //What app opens the JSON file vi, nano...
NSString *iTermVersionPref; //Which version of iTerm nightly or stable
NSString *openInPref; //By default are commands opened in tabs or new windows.
NSString *themePref; //The global theme.

BOOL parseAltJSON; //Are we parsing a second JSON file

//Sort and separator
NSString *menuName; //Menu name after removing the sort [aaa] and separator [---] syntax.
BOOL addSeparator; //Are we adding a separator in the menu.

//Used to gather ssh config settings
NSMutableArray* shuttleHosts;
NSMutableArray* shuttleHostsAlt;
NSMutableArray* ignoreHosts;
NSMutableArray* ignoreKeywords;

Expand Down
38 changes: 38 additions & 0 deletions Shuttle/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ - (void) awakeFromNib {

// The location for the JSON path file. This is a simple file that contains the hard path to the *.json settings file.
shuttleJSONPathPref = [NSHomeDirectory() stringByAppendingPathComponent:@".shuttle.path"];
shuttleJSONPathAlt = [NSHomeDirectory() stringByAppendingPathComponent:@".shuttle-alt.path"];

//if file shuttle.path exists in ~/.shuttle.path then read this file as it should contain the custom path to *.json
if( [[NSFileManager defaultManager] fileExistsAtPath:shuttleJSONPathPref] ) {
Expand All @@ -34,6 +35,33 @@ - (void) awakeFromNib {
}
}

// if the custom alternate json file exists then read the file and use set the output as the alt path.
if ( [[NSFileManager defaultManager] fileExistsAtPath:shuttleJSONPathAlt] ) {

//Read shuttle-alt.path file which contains the custom path to the alternate json file
NSString *jsonConfigAltPath = [NSString stringWithContentsOfFile:shuttleJSONPathAlt encoding:NSUTF8StringEncoding error:NULL];

//Remove whitespace if any
jsonConfigAltPath = [ jsonConfigAltPath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

//set the global var that contains the alternate path
shuttleAltConfigFile = jsonConfigAltPath;

//flag the bool for later parsing
parseAltJSON = YES;
}else{
//the custom alt path does not exist. Assume the default for alt path; if existing flag for later parsing
shuttleAltConfigFile = [NSHomeDirectory() stringByAppendingPathComponent:@".shuttle-alt.json"];

if ( [[NSFileManager defaultManager] fileExistsAtPath:shuttleAltConfigFile] ){
//the default path exists. Flag for later parsing
parseAltJSON = YES;
}else{
//The user does not want to parse an additional json file.
parseAltJSON = NO;
}
}

// Define Icons
//only regular icon is needed for 10.10 and higher. OS X changes the icon for us.
regularIcon = [NSImage imageNamed:@"StatusIcon"];
Expand Down Expand Up @@ -84,10 +112,12 @@ - (NSDate*) getMTimeFor: (NSString*) file {
- (void)menuWillOpen:(NSMenu *)menu {
// Check when the config was last modified
if ( [self needUpdateFor:shuttleConfigFile with:configModified] ||
[self needUpdateFor:shuttleAltConfigFile with:configModified2] ||
[self needUpdateFor: @"/etc/ssh/ssh_config" with:sshConfigSystem] ||
[self needUpdateFor: @"~/.ssh/config" with:sshConfigUser]) {

configModified = [self getMTimeFor:shuttleConfigFile];
configModified2 = [self getMTimeFor:shuttleAltConfigFile];
sshConfigSystem = [self getMTimeFor: @"/etc/ssh_config"];
sshConfigUser = [self getMTimeFor: @"~/.ssh/config"];

Expand Down Expand Up @@ -218,6 +248,14 @@ - (void) loadMenu {
ignoreHosts = json[@"ssh_config_ignore_hosts"];
ignoreKeywords = json[@"ssh_config_ignore_keywords"];

//add hosts from the alternate json config
if (parseAltJSON) {
NSData *dataAlt = [NSData dataWithContentsOfFile:shuttleAltConfigFile];
id jsonAlt = [NSJSONSerialization JSONObjectWithData:dataAlt options:NSJSONReadingMutableContainers error:nil];
shuttleHostsAlt = jsonAlt[@"hosts"];
[shuttleHosts addObjectsFromArray:shuttleHostsAlt];
}

// Should we merge ssh config hosts?
BOOL showSshConfigHosts = YES;
if ([[json allKeys] containsObject:(@"show_ssh_config_hosts")] && [json[@"show_ssh_config_hosts"] boolValue] == NO) {
Expand Down
4 changes: 2 additions & 2 deletions Shuttle/Shuttle-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.3.0</string>
<key>NSAppleEventsUsageDescription</key>
<string>NSAppleEventsUsageDescription</string>
<key>CFBundleVersion</key>
<string>1.3.0</string>
<key>LSApplicationCategoryType</key>
Expand All @@ -28,6 +26,8 @@
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>LSUIElement</key>
<true/>
<key>NSAppleEventsUsageDescription</key>
<string>Shuttle needs Automation privileges in Security &amp; Privacy to run applescripts</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2016 Trevor Fitzgerald. All rights reserved.</string>
<key>NSMainNibFile</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ on CommandRun(withCmd)
activate
tell the current window
tell the current session
--set name to theTitle
write text withCmd
end tell
end tell
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ on CommandRun(withCmd, withTheme, theTitle)
if it is not running then
activate
if (count windows) is 0 then
NewWin(withTheme) of me
my NewWin(withTheme)
end if
SetWinParam(theTitle, withCmd) of me
my SetWinParam(theTitle, withCmd)
else if (count windows) is 0 then
NewWin(withTheme) of me
SetWinParam(theTitle, withCmd) of me
my NewWin(withTheme)
my SetWinParam(theTitle, withCmd)
else
NewTab(withTheme) of me
SetTabParam(theTitle, withCmd) of me
my NewTab(withTheme)
my SetTabParam(theTitle, withCmd)
end if
end tell
end CommandRun
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ on CommandRun(withCmd, withTheme, theTitle)
if it is not running then
activate
if (count windows) is 0 then
NewWin(withTheme) of me
my NewWin(withTheme)
end if
else
NewWin(withTheme) of me
my NewWin(withTheme)
end if
tell the current window
tell the current session
Expand Down
8 changes: 4 additions & 4 deletions apple-scripts/terminal/terminal-new-tab-default.applescript
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--for testing uncomment the "on run" block
--on run
-- set argsCmd to "ps aux | grep [s]sh"
-- set argsCmd to "ps aux | grep [s]creen"
-- set argsTheme to "Homebrew"
-- set argsTitle to "Custom title"
-- scriptRun(argsCmd, argsTheme, argsTitle)
Expand Down Expand Up @@ -30,19 +30,19 @@ on CommandRun(withCmd, withTheme, theTitle)
--run our script in a new window
reopen
activate
do script withCmd in window 1
do script withCmd in selected tab of window 1
else
--Terminal is running and we have a window run in a new tab
reopen
activate
tell application "System Events"
tell process "Terminal"
delay 0.2
delay 0.3
keystroke "t" using {command down}
end tell
end tell
activate
do script withCmd in front window
do script withCmd in selected tab of front window
end if
set current settings of selected tab of front window to settings set withTheme
set title displays custom title of front window to true
Expand Down

0 comments on commit 7142655

Please sign in to comment.