Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Last active January 12, 2024 19:19
Show Gist options
  • Save pythoninthegrass/386cf3da505938acd59a7b9a9172caa9 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/386cf3da505938acd59a7b9a9172caa9 to your computer and use it in GitHub Desktop.
Alfred workflow to convert time between ms/secs/min
#!/usr/bin/env python3
import sys
def minutes_to_seconds(minutes):
"""Function to convert minutes to seconds"""
seconds = minutes * 60
return seconds
def seconds_to_milliseconds(seconds):
"""Function to convert seconds to milliseconds"""
milliseconds = seconds * 1000
return milliseconds
def milliseconds_to_seconds(milliseconds):
"""Function to convert milliseconds to seconds"""
seconds = milliseconds // 1000
return seconds
def seconds_to_minutes(seconds):
"""Function to convert seconds to minutes"""
minutes = seconds // 60
return minutes
def generate_xml(result, message, function_name):
"""Function to generate XML"""
unit = "ms"
if function_name == "minutes_to_seconds":
unit = "secs"
elif function_name == "seconds_to_minutes":
unit = "minutes"
xml = f"""
<?xml version="1.0"?>
<items>
<item uid="{unit}" arg="{result}">
<title>Result: {result} {unit}</title>
<subtitle>Press Enter to paste, or Cmd+C to copy</subtitle>
<icon>icon.png</icon>
</item>
</items>
"""
return xml
# TODO: debug argv input with alfred
def main():
if len(sys.argv) > 1:
arg = sys.argv[1]
value = int(sys.argv[2])
result = None
message = None
match arg:
case "minutes_to_seconds":
result = minutes_to_seconds(value)
message = f"{result} seconds"
case "seconds_to_milliseconds":
result = seconds_to_milliseconds(value)
message = f"{result} milliseconds"
case "milliseconds_to_seconds":
result = milliseconds_to_seconds(value)
message = f"{result} seconds"
case "seconds_to_minutes":
result = seconds_to_minutes(value)
message = f"{result} minutes"
case _:
message = "Invalid argument"
xml = generate_xml(result, message)
print(xml)
else:
print("No argument provided")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment