-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d44cc5a
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/** | ||
* Temperature conversion script | ||
* | ||
* Converts a provided value from Celsius to Fahrenheit, or vice versa. | ||
*/ | ||
|
||
// Collect user input and current default temperature scale (stored by Alfred) | ||
$query = $argv[1]; | ||
$default_scale = getenv( 'default_scale' ); | ||
|
||
// Initialize items array | ||
$items = array( | ||
'items' => array(), | ||
); | ||
|
||
// If no scale is provided, and a default is set, use the default | ||
if ( ! preg_match( "/[f,c]{1}$/i", $query ) && false != $default_scale ) { | ||
if ( 'Fahrenheit' === $default_scale ) { | ||
$temp = $query . 'f'; | ||
} elseif ( 'Celsius' === $default_scale ) { | ||
$temp = $query . 'c'; | ||
} | ||
// Otherwise, save user input as-is | ||
} else { | ||
$temp = $query; | ||
} | ||
|
||
// Verify the temp is valid: | ||
// Regex pattern: | ||
// Optional `-` to support negative integers | ||
// Any number of numeric digits | ||
// A single 'f' or 'c' (case insensitive) to declare the current scale being entered | ||
if ( ! preg_match( "/^-?[0-9]+[f,c]{1}$/i", $temp ) ) { | ||
// Prepare prompt for display when invalid temp is detected | ||
$title = "Enter a temperature to convert"; | ||
$subtitle = "Be sure to use 'C' or 'F' (or set a default scale below)"; | ||
$result = ''; | ||
$validity = 'false'; | ||
} else { | ||
// Strip scale from string so the numeric value can be processed | ||
$input_temp = substr( $temp, 0, -1 ); | ||
|
||
// Prep values if the input scale is Fahrenheit | ||
if ( preg_match( "/f/i", $temp ) ) { | ||
$output_temp = intval( ( 5 / 9 ) * ( $input_temp - 32 ) ); | ||
$converted_scale = 'Celsius'; | ||
$input_abbr = '°F'; | ||
$output_abbr = '°C'; | ||
// Prep values if the the input scale is Celsius | ||
} elseif ( preg_match( "/c/i", $temp ) ) { | ||
$output_temp = intval( ( 9 / 5 ) * $input_temp + 32 ); | ||
$converted_scale = "Fahrenheit"; | ||
$input_abbr = '°C'; | ||
$output_abbr = '°F'; | ||
} | ||
|
||
// Save the various values to the relevant strings | ||
$full_conversion = $input_temp . $input_abbr . "/" . $output_temp . $output_abbr; | ||
$converted_only = $output_temp . $output_abbr; | ||
$title = "Convert $input_temp" . "$input_abbr to $converted_scale"; | ||
$subtitle = "Output full conversion: $full_conversion"; | ||
$validity = 'true'; | ||
} | ||
|
||
// Build the temperature conversion list item | ||
$items['items'][] = array( | ||
'title' => $title, | ||
'subtitle' => $subtitle, | ||
'arg' => $full_conversion, | ||
'valid' => $validity, | ||
'mods' => array( | ||
'cmd' => array( | ||
'subtitle' => "Output $converted_scale only: $converted_only", | ||
'arg' => $converted_only, | ||
), | ||
), | ||
); | ||
|
||
// Build the temp. scale setting list item | ||
$items['items'][] = array( | ||
'title' => 'Set default temperature scale', | ||
'subtitle' => 'CMD for Celsius or OPTION for Fahrenheit', | ||
'valid' => 'false', | ||
'mods' => array( | ||
'cmd' => array( | ||
'subtitle' => 'Set default temperature scale to Celsius', | ||
'arg' => 'Celsius', | ||
'valid' => 'true', | ||
'variables' => array( | ||
'set_default' => 'true', | ||
), | ||
), | ||
'option' => array( | ||
'subtitle' => 'Set default temperature scale to Fahrenheit', | ||
'arg' => 'Fahrenheit', | ||
'valid' => 'true', | ||
'variables' => array( | ||
'set_default' => 'true', | ||
), | ||
), | ||
), | ||
); | ||
|
||
// Output script filter list items | ||
echo json_encode( $items ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Alfred Temperature Converter | ||
|
||
This workflow will output temperatures converted between Fahrenheit and Celsius. | ||
|
||
## Converting Temperatures | ||
The workflow can be activated by entering the keyword `tc` (short for Temperature Converter) into Alfred. | ||
|
||
Once the workflow is activated, input your temperature in whole numbers only (no decimals). | ||
|
||
If you have set up a default temperature scale, it will be applied automatically. | ||
|
||
If you haven't yet set a default, or if you would like to convert in the opposite direction, add an 'f' for Fahrenheit or a 'c' for Celsius. Be sure to add the letter corresponding the the temperature you are converting *from*. | ||
|
||
## Setting a Default Scale | ||
|
||
You can select either Fahrenheit or Celsius as the default scale that you usually enter temperatures in. | ||
|
||
Launch the workflow with the usual keyword, and arrow down to the `Set default temperature scale` option. | ||
|
||
Use `CMD + RETURN` to set your default to Celsius. Use `OPTION + RETURN` to set your default to Fahrenheit. | ||
|
||
Please allow 10-15 seconds for Alfred to update the Workflow configuration files before relaunching the workflow. | ||
|
||
You may change this default at any time. | ||
|
||
## Conversion Formatting | ||
|
||
By default, the workflow will output the original temperature along with the converted temperature, like so: | ||
|
||
*32°F/0°C* | ||
|
||
However, if you hold the `CMD` key while processing your conversion, it will output just the converted temperature, like so: | ||
`0°C` |