Skip to content

Instantly share code, notes, and snippets.

@jiserra
Last active August 31, 2021 20:32
Show Gist options
  • Save jiserra/a77a34a35d77beced95b7b71e8424241 to your computer and use it in GitHub Desktop.
Save jiserra/a77a34a35d77beced95b7b71e8424241 to your computer and use it in GitHub Desktop.
Install Script for Raycast
#!/bin/bash
# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Install Script Command
# @raycast.mode compact
# Optional parameters:
# @raycast.icon 🛠️
# @raycast.packageName Raycast
# Documentation:
# @raycast.author Juan I. Serra
# @raycast.authorURL https://github.com/jiserra
# Original author: Thomas Paul Mann - https://github.com/thomaspaulmann
#
# How to use: Navigate with Safari to a script in the Raycast command repo and invoke this script in Raycast.
# Configuration:
INSTALLATION_DIRECTORY="$HOME/Documents/Raycast/script-commands/_enabled-commands"
# Constants:
SCRIPT_COMMANDS_REPOSITORY="https://github.com/raycast/script-commands"
SCRIPT_COMMANDS_RAW_REPOSITORY="https://raw.githubusercontent.com/raycast/script-commands"
# Utils:
download_script() {
mkdir -p $INSTALLATION_DIRECTORY
cd $INSTALLATION_DIRECTORY
curl -O $1
}
download_icon() {
if [[ $2 =~ "." ]]; then
local ICON_NAME=${2#"."}
ICON_NAME=${ICON_NAME#"/"}
local ICON_REMOTE_URL="$1/$ICON_NAME"
local ICON_LOCAL_PATH="$INSTALLATION_DIRECTORY/$ICON_NAME"
mkdir -p $(dirname $ICON_LOCAL_PATH)
curl -o $ICON_LOCAL_PATH $ICON_REMOTE_URL
fi
}
get_url_of_frontmost_application() {
osascript -e '
tell application "Safari"
return URL of current tab of window 1
end tell
'
}
# Main:
# Extract URL to script command
CLIPBOARD_CONTENTS=$(pbpaste)
if [[ $CLIPBOARD_CONTENTS =~ ^$SCRIPT_COMMANDS_REPOSITORY ]]; then
COMMAND_URL=$CLIPBOARD_CONTENTS
else
FRONTMOST_BROWSER_URL=$(get_url_of_frontmost_application)
if [[ $FRONTMOST_BROWSER_URL =~ ^$SCRIPT_COMMANDS_REPOSITORY ]]; then
COMMAND_URL=$FRONTMOST_BROWSER_URL
else
echo "Not a valid link to a script command"
exit 1
fi
fi
# Construct pathes
RAW_COMMAND_URL="$SCRIPT_COMMANDS_RAW_REPOSITORY/${COMMAND_URL#*"/blob/"}"
FILE_NAME=${RAW_COMMAND_URL##*/}
FILE_PATH="$INSTALLATION_DIRECTORY/$FILE_NAME"
PARENT_REMOTE_DIRECTORY=${RAW_COMMAND_URL%"/$FILE_NAME"}
# Download script
download_script $RAW_COMMAND_URL
# Download icons
ICON=$(sed -n -e 's/^.*raycast.icon //p' $FILE_PATH)
download_icon $PARENT_REMOTE_DIRECTORY $ICON
ICON_DARK=$(sed -n -e 's/^.*raycast.iconDark //p' $FILE_PATH)
download_icon $PARENT_REMOTE_DIRECTORY $ICON_DARK
# Show message
TITLE=$(sed -n -e 's/^.*raycast.title //p' $FILE_PATH)
echo "Installed '$TITLE'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment