Skip to content

Commit

Permalink
Added script to export/import raycast configs
Browse files Browse the repository at this point in the history
Code contributed by/adapted from @arunvelsriram's gist
  • Loading branch information
vraravam committed Jan 11, 2025
1 parent 955693e commit 3dd70b7
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ As documented in the README's [adopting](README.md#how-to-adoptcustomize-the-scr

For those who follow this repo, here's the changelog for ease of adoption:

### 1.0-45

* *[capture-raycast-configs.sh]* Added script to export/import raycast configs. More details can be found [here](Extras.md#capture-raycast-configssh). Code contributed by/adapted from @arunvelsriram's gist.
* Reuse utility functions defined in `.shellrc`

### 1.0-44

* *[recreate-repo.sh]* Fix an issue where a trailing slash would not properly process the repo in `${PERSONAL_PROFILES_DIR}` (ie would not force-squash)
Expand Down
19 changes: 19 additions & 0 deletions Extras.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ Note:

This script is useful to capture the preferences of the known applications (both system-installed and custom-installed applications) using the `defaults read` command. It can be used to both export the preferences/settings (from the old system) or import them (into the new system)

## capture-raycast-configs.sh

This script is useful to capture the raycast preferences/configurations. It can be used to both export the preferences/settings (from the old system) or import them (into the new system)

```bash
export RAYCAST_SETTINGS_PASSWORD='my-passoword'
capture-raycast-configs.sh e "${PERSONAL_PROFILES_DIR}/extension-backups"
capture-raycast-configs.sh i "${PERSONAL_PROFILES_DIR}/extension-backups"
```

*Please note:*

Since this script uses applescript internally, it needs to be granted the following permissions:

* `Privacy & Security > Accessibility` - need to enable/approve for iTerm and Terminal apps.
* `Privacy & Security > Automation` - need to enable/approve for "System Events" for iTerm and Terminal apps.
* Also, since this mimics keystrokes from the user, while this script is running, you should not move the mouse or type anything else using the keyboard or mouse.
* The above manual steps have to be performed after installing Raycast and running it at least once (so one has to click through the setup wizard). Due to this reason, this script has NOT been incorporated into the `fresh-install-of-osx.sh` script.

## cleanup-browser-profiles.sh

This script is used to cleanup browser profiles folders (delete cache, session and other files that will anyways be recreated when you restart that browser). It can be safely invoked even if that browser is running (in which case it will skip processing after printing a warning to quit that application)
Expand Down
5 changes: 3 additions & 2 deletions scripts/capture-defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
# A trick to find the name of the app:
# Run `defaults read` in an empty window of a terminal app, then use the search functionality to search for a known word related to that app (like eg app visible name, author, some setting that's unique to that app, etc). Once you find this, trace back to the left-most child (1st of the top-level parent) in the printed JSON to then get the real unique name of the app where its settings are stored. Please note that one app might have multiple such groups / names at the top-level (for eg zoom). If this is the case, you will need to capture each name individually.

type warn &> /dev/null 2>&1 || source "${HOME}/.shellrc"
type red &> /dev/null 2>&1 || source "${HOME}/.shellrc"
type is_non_zero_string &> /dev/null 2>&1 || source "${HOME}/.shellrc"

usage() {
echo "$(red 'Usage'): $(yellow "${1} <e/i>")"
Expand All @@ -23,7 +24,7 @@ usage() {
! is_non_zero_string "${PERSONAL_CONFIGS_DIR}" && warn "Env var 'PERSONAL_CONFIGS_DIR' is not defined; Aborting!!!" && return

local target_dir="${PERSONAL_CONFIGS_DIR}/defaults"
mkdir -p "${target_dir}"
ensure_dir_exists "${target_dir}"

case "${1}" in
"e" )
Expand Down
96 changes: 96 additions & 0 deletions scripts/capture-raycast-configs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env zsh

# vim:filetype=zsh syntax=zsh tabstop=2 shiftwidth=2 softtabstop=2 expandtab autoindent fileencoding=utf-8

# file location: <anywhere; but advisable in the PATH>

# This script will export/import the raycast configs to/from the location specified in the target directory.
# Reference for keystrokes/keycodes: https://eastmanreference.com/complete-list-of-applescript-key-codes

type red &> /dev/null 2>&1 || source "${HOME}/.shellrc"

usage() {
echo "$(red "Usage"): $(yellow "${1} <e/i> <target-dir-location>")"
echo " $(yellow 'e') --> Export from system"
echo " $(yellow 'i') --> Import into system"
echo " $(yellow 'target-dir-location') --> Directory name where the config has to be exported to/imported from"
exit 1
}

[ $# -ne 2 ] && usage "${0}"

[[ "${1}" != 'e' && "${1}" != 'i' ]] && echo "$(red 'Unknown value entered') for first argument: '${1}'" && usage "${0}"

local target_dir="${2}"
local target_file="${target_dir}/Raycast.rayconfig"
ensure_dir_exists "${target_dir}"

if [[ "${1}" == 'e' ]]; then
is_file "${target_dir}/Raycast.rayconfig" && rm -rf "${target_dir}/Raycast.rayconfig"

open raycast://extensions/raycast/raycast/export-settings-data

osascript <<EOF
tell application "System Events"
key code 36
delay 0.3
key code 5 using {command down, shift down}
delay 0.3
keystroke "${target_dir}"
delay 0.3
key code 36
delay 0.3
key code 36
delay 0.5
key code 53
end tell
EOF

mv "${target_dir}"/Raycast*.rayconfig "${target_file}"
success "Exported raycast configs to: $(yellow "${target_file}")"
elif [[ "${1}" == 'i' ]]; then
! is_file "${target_file}" && error "Couldn't find file: '$(yellow "${target_file}")' for import operation; Aborting!!!"

! is_non_zero_string "${RAYCAST_SETTINGS_PASSWORD}" && error "Cannot proceed without the 'RAYCAST_SETTINGS_PASSWORD' env var set; Aborting!!!"

open raycast://extensions/raycast/raycast/import-settings-data

osascript <<EOF
tell application "System Events"
key code 36
delay 0.3
key code 5 using {command down, shift down}
delay 0.3
keystroke "${target_dir}/Raycast.rayconfig"
delay 0.3
key code 36
delay 0.5
key code 36
delay 0.3
keystroke "${RAYCAST_SETTINGS_PASSWORD}"
key code 36
delay 0.3
key code 36
delay 0.3
key code 36
delay 1
key code 53
key code 53
end tell
EOF

success "Imported raycast configs from: $(yellow "${target_file}")"
fi
1 change: 1 addition & 0 deletions scripts/fresh-install-of-osx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ fi

echo "\n"
success '** Finished auto installation process: MANUALLY QUIT AND RESTART iTerm2 and Terminal apps **'
yellow "Remember to set the 'RAYCAST_SETTINGS_PASSWORD' env var, and then run the 'capture-raycast-configs.sh' script to import your Raycast configuration into the new machine."

script_end_time=$(date +%s)
echo "==> Script completed at: $(date)"
Expand Down
1 change: 0 additions & 1 deletion scripts/osx-defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ defaults write com.apple.systemuiserver "NSStatusItem Visible com.apple.menuextr
defaults write com.apple.menuextra.clock DateFormat -string "EEE d MMM h:mm:ss a"
defaults write com.apple.menuextra.clock FlashDateSeparators -bool true
defaults write com.apple.menuextra.clock IsAnalog -bool true # Since I am using `The Clocker` app, turning this to analog
defaults write com.apple.menuextra.clock IsAnalog -bool true
defaults write com.apple.menuextra.clock Show24Hour -bool false
defaults write com.apple.menuextra.clock ShowAMPM -bool true
defaults write com.apple.menuextra.clock ShowDate -bool false
Expand Down

0 comments on commit 3dd70b7

Please sign in to comment.