Skip to content

Instantly share code, notes, and snippets.

@melalj
Created February 4, 2024 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melalj/0fcb4486c26582d1c0a7d2b73ed169ee to your computer and use it in GitHub Desktop.
Save melalj/0fcb4486c26582d1c0a7d2b73ed169ee to your computer and use it in GitHub Desktop.
Screen grab any items with its context to Notion. Linked to the Notion template: http://go.tonoid.com/saved-clips-notion-template
#!/bin/bash
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Save Clip
# @raycast.mode silent
# @raycast.packageName Screen grab any items with its context to Notion
#
# Optional parameters:
# @raycast.icon ✂️
#
# Documentation:
# @raycast.description Opens default screenshot interface, request keywords, uploads it cloudinary, and save it to Notion with context
# @raycast.author Simo Elalj
# @raycast.authorURL https://2sync.com
########################
## Config
########################
# Cloudinary's API credentials: https://cloudinary.com/
CLOUD_NAME="xxxx"
API_KEY="xxxxxxxxxx"
API_SECRET="xxxxxxxxxxxxxxxx"
# Notion template: http://go.tonoid.com/saved-clips-notion-template
# Notion's API credentials: https://www.notion.so/my-integrations
NOTION_API_KEY="secret_xxxxx"
NOTION_DATABASE_ID="xxxxxxx"
########################
# Script below
########################
escape_json() {
echo -n "$1" | awk '{ gsub(/\\/, "\\\\"); gsub(/"/, "\\\""); printf "%s\\n", $0 }' | sed '$ s/\\n$//'
}
# Get current default browser: url, title
DEFAULT_BROWSER=$(defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist | awk -F'\"' '/http;/{print window[(NR)-1]}{window[NR]=$2}')
get_url_asset () {
ASSET_NAME=$1
if [ "$DEFAULT_BROWSER" = "com.google.chrome" ]; then
echo $(osascript -e 'tell application "Chrome" to '$ASSET_NAME' of active tab of front window as text')
fi
if [ "$DEFAULT_BROWSER" = "com.brave.browser" ]; then
echo $(osascript -e 'tell application "Brave" to '$ASSET_NAME' of active tab of front window as text')
fi
if [ "$DEFAULT_BROWSER" = "company.thebrowser.browser" ]; then
echo $(osascript -e 'tell application "Arc" to '$ASSET_NAME' of active tab of front window as text')
fi
if [ "$DEFAULT_BROWSER" = "com.apple.safari" ]; then
echo $(osascript -e 'tell application "Safari" to return '$ASSET_NAME' of front document')
fi
echo ""
}
CURRENT_URL=$(get_url_asset 'URL')
CURRENT_TITLE_RAW=$(get_url_asset 'title')
CURRENT_TITLE=${CURRENT_TITLE_RAW/\"/\\\"/g}
if [ -n "$TAGS_VALUES" ]; then
CURRENT_URL_JSON="\"Page URL\": { \"url\": \"$CURRENT_URL\" },"
fi
# Opens screenshot interface
TEMP_FILE=$(mktemp)
screencapture -i "$TEMP_FILE"
read -r -d '' APPLESCRIPT_CODE <<EOF
set dialogText to text returned of (display dialog "Keywords" default answer "$CURRENT_TITLE")
return dialogText
EOF
# Prompt keyword
KEYWORDS=$(osascript -e "$APPLESCRIPT_CODE");
if [ -z "$KEYWORDS" ]; then
echo "Missing Keyword"
exit 1
fi
KEYWORDS_SLUG=$(echo $KEYWORDS | iconv -t ascii//TRANSLIT | sed -E -e 's/[^[:alnum:]]+/-/g' -e 's/^-+|-+$//g' | tr '[:upper:]' '[:lower:]')
# Extract tags
HASHTAGS=$(echo "$KEYWORDS" | grep -o '#\w\+')
HASHTAGS_LINES=$(echo $HASHTAGS | tr " " "\n")
TAGS_VALUES=''
while read -r TAG; do
TAGS_VALUES+="{\"name\": \"$(escape_json "${TAG:1}")\"},"
done <<< "$HASHTAGS_LINES"
TAGS_VALUES="${TAGS_VALUES%?}"
if [ -n "$TAGS_VALUES" ]; then
TAGS_JSON="\"Tags\": { \"multi_select\": [$TAGS_VALUES] },"
fi
# Cloudinary's API endpoint
UPLOAD_URL="https://api.cloudinary.com/v1_1/$CLOUD_NAME/image/upload"
# Generate a timestamp
TIMESTAMP=$(date +%s)
NEW_IMAGE_NAME=$KEYWORDS_SLUG
# Generate a signature
# The signature is a SHA-1 hash of your parameters including your API Secret
# Note: The signature should include any additional parameters you use in alphabetical order
SIGNATURE=$(echo -n "public_id=$NEW_IMAGE_NAME&timestamp=$TIMESTAMP$API_SECRET" | sha1sum | awk '{print $1}')
# Make the POST request
IMAGE_URL_RESPONSE=$(curl -X POST $UPLOAD_URL \
-F file=@"$TEMP_FILE" \
-F api_key="$API_KEY" \
-F timestamp="$TIMESTAMP" \
-F signature="$SIGNATURE" \
-F public_id="$NEW_IMAGE_NAME")
IMAGE_URL_DIRECT=$(echo $IMAGE_URL_RESPONSE | sed -n 's/.*"secure_url":"\([^"]*\)".*/\1/p')
# Save to Notion
curl -s -X POST "https://api.notion.com/v1/pages" \
-H "Authorization: Bearer $NOTION_API_KEY" \
-H "Notion-Version: 2022-06-28" \
-H 'Content-Type: application/json; charset=utf-8' \
--data-binary @- > /tmp/notion_response.txt << EOF
{
"parent": { "database_id": "$NOTION_DATABASE_ID" },
"properties": {
"Name": {
"title": [
{
"text": {
"content": "$(escape_json "$KEYWORDS")"
}
}
]
},
"Page Title": {
"rich_text": [
{
"text": {
"content": "$(escape_json "$CURRENT_TITLE")"
}
}
]
},
"Screenshot": {
"files": [{
"name": "$(escape_json "$KEYWORDS_SLUG")",
"type": "external",
"external": {
"url": "$IMAGE_URL_DIRECT"
}
}]
},
"Screenshot URL": { "url": "$IMAGE_URL_DIRECT"}
}
}
EOF
echo "Saved to Notion!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment