diff options
Diffstat (limited to 'Source/SPAppController.m')
-rw-r--r-- | Source/SPAppController.m | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/Source/SPAppController.m b/Source/SPAppController.m index 7278f5db..841a4d69 100644 --- a/Source/SPAppController.m +++ b/Source/SPAppController.m @@ -37,6 +37,7 @@ #import "SPBundleHTMLOutputController.h" #import "SPAlertSheets.h" #import "SPChooseMenuItemDialog.h" +#import "SPCustomQuery.h" #import <PSMTabBar/PSMTabBarControl.h> #import <Sparkle/Sparkle.h> @@ -769,7 +770,7 @@ NSMutableDictionary *env = [NSMutableDictionary dictionary]; [env setObject:[infoPath stringByDeletingLastPathComponent] forKey:SPBundleShellVariableBundlePath]; [env setObject:bundleInputFilePath forKey:SPBundleShellVariableInputFilePath]; - [env setObject:SPBundleScopeGeneral forKey:SPBundleShellVariableScope]; + [env setObject:SPBundleScopeGeneral forKey:SPBundleShellVariableBundleScope]; NSString *input = @""; NSError *inputFileError = nil; @@ -884,6 +885,69 @@ } +/** + * Return of certain shell variables mainly for usage in JavaScript support inside the + * HTML output window to allow to ask on run-time + */ +- (NSDictionary*)shellEnvironment +{ + NSMutableDictionary *env = [NSMutableDictionary dictionary]; + SPDatabaseDocument *doc = [self frontDocument]; + if(doc) [env addEntriesFromDictionary:[doc shellVariables]]; + id firstResponder = [[NSApp keyWindow] firstResponder]; + if([firstResponder respondsToSelector:@selector(executeBundleItemForInputField:)]) { + BOOL selfIsQueryEditor = ([[[firstResponder class] description] isEqualToString:@"SPTextView"]) ; + NSRange currentWordRange, currentSelectionRange, currentLineRange, currentQueryRange; + currentSelectionRange = [firstResponder selectedRange]; + currentWordRange = [firstResponder getRangeForCurrentWord]; + currentLineRange = [[firstResponder string] lineRangeForRange:NSMakeRange([firstResponder selectedRange].location, 0)]; + + if(selfIsQueryEditor) { + currentQueryRange = [[firstResponder delegate] currentQueryRange]; + } else { + currentQueryRange = currentLineRange; + } + if(!currentQueryRange.length) + currentQueryRange = currentSelectionRange; + + [env setObject:SPBundleScopeInputField forKey:SPBundleShellVariableBundleScope]; + + if(selfIsQueryEditor && [[firstResponder delegate] currentQueryRange].length) + [env setObject:[[firstResponder string] substringWithRange:[[firstResponder delegate] currentQueryRange]] forKey:SPBundleShellVariableCurrentQuery]; + + if(currentSelectionRange.length) + [env setObject:[[firstResponder string] substringWithRange:currentSelectionRange] forKey:SPBundleShellVariableSelectedText]; + + if(currentWordRange.length) + [env setObject:[[firstResponder string] substringWithRange:currentWordRange] forKey:SPBundleShellVariableCurrentWord]; + + if(currentLineRange.length) + [env setObject:[[firstResponder string] substringWithRange:currentLineRange] forKey:SPBundleShellVariableCurrentLine]; + } + else if([firstResponder respondsToSelector:@selector(executeBundleItemForDataTable:)]) { + + if([[firstResponder delegate] respondsToSelector:@selector(usedQuery)] && [[firstResponder delegate] usedQuery]) + [env setObject:[[firstResponder delegate] usedQuery] forKey:SPBundleShellVariableUsedQueryForTable]; + + if([firstResponder numberOfSelectedRows]) { + NSMutableArray *sel = [NSMutableArray array]; + NSIndexSet *selectedRows = [firstResponder selectedRowIndexes]; + NSUInteger rowIndex = [selectedRows firstIndex]; + while ( rowIndex != NSNotFound ) { + [sel addObject:[NSString stringWithFormat:@"%ld", rowIndex]]; + rowIndex = [selectedRows indexGreaterThanIndex:rowIndex]; + } + [env setObject:[sel componentsJoinedByString:@"\t"] forKey:SPBundleShellVariableSelectedRowIndices]; + } + + [env setObject:SPBundleScopeDataTable forKey:SPBundleShellVariableBundleScope]; + + } else { + [env setObject:SPBundleScopeGeneral forKey:SPBundleShellVariableBundleScope]; + } + return env; +} + - (void)registerActivity:(NSDictionary*)commandDict { [runningActivitiesArray addObject:commandDict]; |