Skip to content

Instantly share code, notes, and snippets.

@hepcat72
Last active September 6, 2018 19:39
Show Gist options
  • Save hepcat72/786c8c3dadfb5699f34c7e7ffc07fec6 to your computer and use it in GitHub Desktop.
Save hepcat72/786c8c3dadfb5699f34c7e7ffc07fec6 to your computer and use it in GitHub Desktop.
--Purpose: Calculate the result of a highlighted equation and append the result.
--How to use it:
-- 1. Highlight an equation in an editable text area.
-- 2. Right click the selection and select Services->Calculate & Append Result
--Author: Robert Leach, Genomics Group, Princeton, [email protected]
on run {input, parameters}
tell application "System Events"
--Bring up spotlight
keystroke " " using {command down}
delay 0.2
--Enter the search term
my keystrokeForMouseKeys(1st item in input)
delay 0.2
--Copy the search result
keystroke "c" using {command down}
--Exit spotlight
key code 53 --escape
key code 53 --escape
delay 0.2
--Deselect text and move cursor to just after the highlight
key code 124 --right arrow
--Append result
keystroke " = "
keystroke "v" using {command down}
end tell
end run
--This is a replacement for the keystroke function, which fails when "Enable Mouse Keys" is checked in System Preferences -> Accessibility -> Mouse & Trackpad. Keystroke will not "type out" any numbers, a dot ('.') or a slash ('/') that are in the supplied string when this option is enabled in System preferences. This method is safe to use whether that setting is checked or not. Call like this: my keystrokeForMouseKeys("your string")
on keystrokeForMouseKeys(theString)
tell application "System Events"
repeat with theChar in (every character of theString)
set theCode to id of theChar
if theCode is 44 then
key code 44
--Not sure why 46 (m) is what you get when you call id on "." but key code 47 types a "." and key code 46 types an 'm'...
else if theCode is 46 then
key code 47
else if ((theCode ≥ 48) and (theCode ≤ 57)) then
key code {item (theCode - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
else
keystroke theChar
end if
end repeat
end tell
end keystrokeForMouseKeys
--What is this: This is an applescript to use in an automator service that allows you to determine the result of a simple mathmatical equation (no variables - just numbers).
--Purpose: Calculate the result of a highlighted equation.
--Installation: Create an Automator service, paste this code into a "Run AppleScript" action, and save as "Calculate.workflow".
--How to use it: 1. Highlight an equation in an editable text area. 2. Right click the selection and select Services->Calculate
--Author: Robert Leach, Genomics Group, Princeton, [email protected]
on run {input, parameters}
tell application "System Events"
keystroke space using {command down}
delay 0.2
my keystrokeForMouseKeys(1st item in input)
end tell
end run
--This is a replacement for the keystroke function, which fails when "Enable Mouse Keys" is checked in System Preferences -> Accessibility -> Mouse & Trackpad. Keystroke will not "type out" any numbers, a dot ('.') or a slash ('/') that are in the supplied string when this option is enabled in System preferences. This method is safe to use whether that setting is checked or not. Call like this: my keystrokeForMouseKeys("your string")
on keystrokeForMouseKeys(theString)
tell application "System Events"
repeat with theChar in (every character of theString)
set theCode to id of theChar
if theCode is 44 then
key code 44
--Not sure why 46 (m) is what you get when you call id on "." but key code 47 types a "." and key code 46 types an 'm'...
else if theCode is 46 then
key code 47
else if ((theCode ≥ 48) and (theCode ≤ 57)) then
key code {item (theCode - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
else
keystroke theChar
end if
end repeat
end tell
end keystrokeForMouseKeys
--What is this: This is an applescript to use in an automator service that allows you to subtract a series of highlighted numbers.
--Purpose: Subtract all subsequent numbers from the first number.
--Installation: Create an Automator service, paste this code into a "Run AppleScript" action, and save as "Subtract.workflow".
--How to use it: 1. Highlight an equation in an editable text area. 2. Right click the selection and select Services->Subtract
--Author: Robert Leach, Genomics Group, Princeton, [email protected]
on run {input, parameters}
set searchString to 1st item of input
--Replace dashes not immediately preceding numbers with a space so we don't accidentally negate a number
set tmpString1 to my replacePattern:"-\\D" inString:searchString usingThis:" "
--Replace spaces, tabs, and carriage and hard returns. We're going to use minus signs followed by spaces to differentiate them from negative numbers.
set tmpString2 to my replacePattern:"[\\s\\n\\t\\r]+" inString:tmpString1 usingThis:"- "
--Replace non-numbers with nothing
set tmpString3 to my replacePattern:"[^\\d\\-\\. ]+" inString:tmpString2 usingThis:""
--Replace any multiple occurrences of "-" with a single occurrence
set tmpString4 to my replacePattern:"- (- )+" inString:tmpString3 usingThis:"- "
--Replace trailing -'s
set tmpString5 to my replacePattern:"-$" inString:tmpString4 usingThis:""
--Replace leading -'s
set tmpString6 to my replacePattern:"^(- )+" inString:tmpString5 usingThis:""
--Now clean up the spaces
set equation to my replacePattern:"\\s" inString:tmpString6 usingThis:""
tell application "System Events"
keystroke space using {command down}
delay 0.2
my keystrokeForMouseKeys(equation)
end tell
end run
--This is a replacement for the keystroke function, which fails when "Enable Mouse Keys" is checked in System Preferences -> Accessibility -> Mouse & Trackpad. Keystroke will not "type out" any numbers, a dot ('.') or a slash ('/') that are in the supplied string when this option is enabled in System preferences. This method is safe to use whether that setting is checked or not. Call like this: my keystrokeForMouseKeys("your string")
on keystrokeForMouseKeys(theString)
tell application "System Events"
repeat with theChar in (every character of theString)
set theCode to id of theChar
if theCode is 44 then
key code 44
--Not sure why 46 (m) is what you get when you call id on "." but key code 47 types a "." and key code 46 types an 'm'...
else if theCode is 46 then
key code 47
else if ((theCode ≥ 48) and (theCode ≤ 57)) then
key code {item (theCode - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
else
keystroke theChar
end if
end repeat
end tell
end keystrokeForMouseKeys
--Call like this: set res to my replacePattern:"\\s+" inString:"1 subtratcing-these: -2 3 4" usingThis:"-"
use framework "Foundation"
use scripting additions
on replacePattern:thePattern inString:theString usingThis:theTemplate
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:0 |error|:(missing value)
set theResult to theRegEx's stringByReplacingMatchesInString:theString options:0 range:{location:0, |length|:length of theString} withTemplate:theTemplate
return theResult as text
end replacePattern:inString:usingThis:
--What is this: This is an applescript to use in an automator service that allows you to sum a series of highlighted numbers.
--Purpose: Calculate a sum.
--Installation: Create an Automator service, paste this code into a "Run AppleScript" action, and save as "Sum.workflow".
--How to use it: 1. Highlight an equation in an editable text area. 2. Right click the selection and select Services->Sum
--Author: Robert Leach, Genomics Group, Princeton, [email protected]
on run {input, parameters}
set searchString to 1st item of input
--Replace dashes not immediately preceding numbers with nothing so we don't accidentally negate a number
set tmpString1 to my replacePattern:"-\\D" inString:searchString usingThis:""
--Replace spaces, tabs, and carriage and hard returns
set tmpString2 to my replacePattern:"[\\s\\n\\t\\r]+" inString:tmpString1 usingThis:"+"
--Replace non-numbers with nothing
set tmpString3 to my replacePattern:"[^\\d\\+\\-\\.]+" inString:tmpString2 usingThis:""
--Replace any multiple occurrences of "+" with a single occurrence
set tmpString4 to my replacePattern:"\\+\\++" inString:tmpString3 usingThis:"+"
--Replace trailing +'s
set tmpString5 to my replacePattern:"\\+$" inString:tmpString4 usingThis:""
--Replace leading +'s
set equation to my replacePattern:"^\\+" inString:tmpString5 usingThis:""
tell application "System Events"
keystroke space using {command down}
delay 0.2
my keystrokeForMouseKeys(equation)
end tell
end run
--This is a replacement for the keystroke function, which fails when "Enable Mouse Keys" is checked in System Preferences -> Accessibility -> Mouse & Trackpad. Keystroke will not "type out" any numbers, a dot ('.') or a slash ('/') that are in the supplied string when this option is enabled in System preferences. This method is safe to use whether that setting is checked or not. Call like this: my keystrokeForMouseKeys("your string")
on keystrokeForMouseKeys(theString)
tell application "System Events"
repeat with theChar in (every character of theString)
set theCode to id of theChar
if theCode is 44 then
key code 44
--Not sure why 46 (m) is what you get when you call id on "." but key code 47 types a "." and key code 46 types an 'm'...
else if theCode is 46 then
key code 47
else if ((theCode ≥ 48) and (theCode ≤ 57)) then
key code {item (theCode - 47) of {29, 18, 19, 20, 21, 23, 22, 26, 28, 25}}
else
keystroke theChar
end if
end repeat
end tell
end keystrokeForMouseKeys
--Call like this: set res to my replacePattern:"\\s+" inString:"1 adding-these: 2 3 4" usingThis:"+"
use framework "Foundation"
use scripting additions
on replacePattern:thePattern inString:theString usingThis:theTemplate
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:thePattern options:0 |error|:(missing value)
set theResult to theRegEx's stringByReplacingMatchesInString:theString options:0 range:{location:0, |length|:length of theString} withTemplate:theTemplate
return theResult as text
end replacePattern:inString:usingThis:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment