Skip to content

Instantly share code, notes, and snippets.

@timurvafin
Last active June 24, 2024 12:15
Show Gist options
  • Save timurvafin/7473741bb0ea62329c6d4c3cfb219fa2 to your computer and use it in GitHub Desktop.
Save timurvafin/7473741bb0ea62329c6d4c3cfb219fa2 to your computer and use it in GitHub Desktop.
Raycast script to switch next-week label with this-week
#!/bin/bash
# Required parameters:
# @raycast.author Timur Vafin
# @raycast.authorURL https://github.com/timurvafin
# @raycast.schemaVersion 1
# @raycast.title Todoist This Week
# @raycast.mode fullOutput
# @raycast.packageName Todoist
# @raycast.description Changes tags from next-week to this-week in Todoist tasks.
# @raycast.needsConfirmation false
# Dependency: requires jq (https://stedolan.github.io/jq/)
# Install via Homebrew: `brew install jq`
# Optional parameters:
# @raycast.icon images/todoist-logo.png
# Get your API Token from: https://todoist.com/prefs/integrations
# Todoist API token
API_TOKEN="XXX"
# Labels
NEXT_WEEK_LABEL="next-week"
THIS_WEEK_LABEL="this-week"
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "jq is required (https://stedolan.github.io/jq/).";
exit 1;
fi
# Check if API token is provided
if [ -z "$API_TOKEN" ]; then
echo "Todoist API token is missing.";
exit 1;
fi
# Fetch all tasks with the next-week label
tasks=$(curl -s -X GET \
"https://api.todoist.com/rest/v2/tasks?label=${NEXT_WEEK_LABEL}" \
-H "Authorization: Bearer $API_TOKEN")
# Check if tasks are retrieved
if [ -z "$tasks" ] || [ "$tasks" == "[]" ]; then
echo "No tasks with the $NEXT_WEEK_LABEL label."
exit 0
fi
# Update tags of tasks
echo "$tasks" | jq -c '.[]' | while read -r task; do
task_id=$(echo "$task" | jq -r '.id')
task_labels=$(echo "$task" | jq -r '.labels')
echo "Updating task $task_id"
echo "Current labels for task $task_id: $task_labels"
# Remove the next-week label and add the this-week label
updated_labels=$(echo "$task_labels" | jq -r --arg next_week "$NEXT_WEEK_LABEL" --arg this_week "$THIS_WEEK_LABEL" 'map(select(. != $next_week)) + [$this_week]')
echo "Updated labels for task $task_id: $updated_labels"
# Update the task with new labels
response=$(curl -s -X POST \
"https://api.todoist.com/rest/v2/tasks/$task_id" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_TOKEN" \
-d "{\"labels\": $updated_labels}")
# echo "Response: $response"
echo "Task $task_id updated"
done
echo "All tasks updated"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment