-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewscript
More file actions
executable file
·61 lines (51 loc) · 1.62 KB
/
newscript
File metadata and controls
executable file
·61 lines (51 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/zsh
#
# Script Name: newscript
# Description: Create a new script from ~/.local/bin/script_template.sh, fill in name/date/author,
# make it executable, and open it in Cursor for editing.
# Author: B Chahal
# Created: 2025-08-09
# Version: 1.1
set -euo pipefail
IFS=$'\n\t'
log_info() { echo "ℹ️ [INFO] $*"; }
log_success() { echo "✅ [SUCCESS] $*"; }
log_error() { echo "❌ [ERROR] $*" >&2; }
main() {
if [[ $# -ne 1 ]]; then
log_error "Usage: newscript <script_name_without_extension>"
exit 1
fi
local name_raw="$1"
# Strip any trailing .sh if user supplied it
local script_name="${name_raw%.sh}"
local script_file="$HOME/.local/bin/${script_name}.sh"
local template="$HOME/.local/bin/script_template.sh"
local today_date
today_date=$(date +'%Y-%m-%d')
local author="B Chahal"
if [[ ! -f "$template" ]]; then
log_error "Template not found at $template"
exit 1
fi
if [[ -e "$script_file" ]]; then
log_error "Refusing to overwrite existing file: $script_file"
exit 1
fi
cp "$template" "$script_file"
# macOS/BSD sed needs -i ''
sed -i '' "s|<script_name>|$script_name|g" "$script_file"
sed -i '' "s|<YYYY-MM-DD>|$today_date|g" "$script_file"
sed -i '' "s|Your Name|$author|g" "$script_file"
chmod +x "$script_file"
log_success "New script created: $script_file"
# Open in Cursor; fall back to nano if Cursor isn't available
if open -Ra "Cursor.app" >/dev/null 2>&1; then
log_info "Opening in Cursor…"
open -a "Cursor" "$script_file"
else
log_warn "Cursor not found. Opening in nano instead."
nano "$script_file"
fi
}
main "$@"