Skip to content

Commit 27d9b84

Browse files
committed
refactor: Streamline Cursor app modification with simplified file handling
- Simplified file copying process using direct `cp` command - Reduced complexity of signature removal for app and helper components - Improved error handling and logging for file modification steps - Optimized file permission and ownership management - Removed LaunchServices database rebuild step - Enhanced file content modification with more precise string manipulation - Simplified app replacement and backup mechanism
1 parent e59b113 commit 27d9b84

File tree

1 file changed

+62
-81
lines changed

1 file changed

+62
-81
lines changed

scripts/run/cursor_mac_id_modifier.sh

Lines changed: 62 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,18 @@ modify_cursor_app_files() {
301301
if [ ! -d "$CURSOR_APP_PATH" ]; then
302302
log_error "未找到 Cursor.app,请确认安装路径: $CURSOR_APP_PATH"
303303
return 1
304-
fi
304+
}
305305

306306
# 创建临时工作目录
307307
local timestamp=$(date +%Y%m%d_%H%M%S)
308-
local temp_dir="/tmp/cursor_mod_${timestamp}"
308+
local temp_dir="/tmp/cursor_reset_${timestamp}"
309309
local temp_app="${temp_dir}/Cursor.app"
310310

311311
# 清理可能存在的旧临时目录
312-
[ -d "$temp_dir" ] && rm -rf "$temp_dir"
312+
if [ -d "$temp_dir" ]; then
313+
log_info "清理已存在的临时目录..."
314+
rm -rf "$temp_dir"
315+
fi
313316

314317
# 创建新的临时目录
315318
mkdir -p "$temp_dir" || {
@@ -319,55 +322,38 @@ modify_cursor_app_files() {
319322

320323
# 复制应用到临时目录
321324
log_info "创建临时工作副本..."
322-
{
323-
# 使用 pv 显示进度(如果可用)
324-
if command -v pv >/dev/null 2>&1; then
325-
tar cf - -C "$(dirname "$CURSOR_APP_PATH")" "$(basename "$CURSOR_APP_PATH")" | pv -s $(du -sb "$CURSOR_APP_PATH" | awk '{print $1}') | tar xf - -C "$temp_dir"
326-
else
327-
# 使用 rsync 显示进度
328-
rsync -av --progress "$CURSOR_APP_PATH/" "$temp_app/"
329-
fi
330-
} || {
331-
log_error "复制应用失败"
325+
cp -R "$CURSOR_APP_PATH" "$temp_dir" || {
326+
log_error "无法复制应用到临时目录"
332327
rm -rf "$temp_dir"
333328
return 1
334329
}
335330

336-
# 验证复制完整性
337-
local orig_size=$(du -s "$CURSOR_APP_PATH" | cut -f1)
338-
local temp_size=$(du -s "$temp_app" | cut -f1)
339-
if [ "$orig_size" != "$temp_size" ]; then
340-
log_error "文件复制不完整,大小不匹配"
341-
rm -rf "$temp_dir"
342-
return 1
343-
fi
344-
331+
# 确保临时目录的权限正确
332+
chown -R "$CURRENT_USER:staff" "$temp_dir"
333+
chmod -R 755 "$temp_dir"
334+
345335
# 移除签名(增强兼容性)
346336
log_info "移除应用签名..."
347-
{
348-
# 设置文件权限
349-
find "$temp_app" -type f -exec chmod 644 {} \;
350-
find "$temp_app" -type d -exec chmod 755 {} \;
351-
352-
# 移除主应用签名
353-
if ! timeout 60 codesign --remove-signature "$temp_app" 2>/dev/null; then
354-
log_warn "标准签名移除超时,尝试快速模式..."
355-
codesign --remove-signature --force "$temp_app" 2>/dev/null || true
356-
fi
357-
358-
# 处理所有Helper进程
359-
local helper_count=0
360-
while IFS= read -r helper; do
361-
((helper_count++))
362-
log_debug "处理Helper进程 ($helper_count): $helper"
363-
timeout 30 codesign --remove-signature "$helper" 2>/dev/null || {
364-
log_warn "移除Helper签名失败: $(basename "$helper")"
365-
codesign --remove-signature --force "$helper" 2>/dev/null || true
366-
}
367-
done < <(find "${temp_app}/Contents/Frameworks" -name "*Helper*.app")
368-
369-
log_info "已处理 $helper_count 个Helper进程"
337+
codesign --remove-signature "$temp_app" || {
338+
log_warn "移除应用签名失败"
370339
}
340+
341+
# 移除所有相关组件的签名
342+
local components=(
343+
"$temp_app/Contents/Frameworks/Cursor Helper.app"
344+
"$temp_app/Contents/Frameworks/Cursor Helper (GPU).app"
345+
"$temp_app/Contents/Frameworks/Cursor Helper (Plugin).app"
346+
"$temp_app/Contents/Frameworks/Cursor Helper (Renderer).app"
347+
)
348+
349+
for component in "${components[@]}"; do
350+
if [ -e "$component" ]; then
351+
log_info "正在移除签名: $component"
352+
codesign --remove-signature "$component" || {
353+
log_warn "移除组件签名失败: $component"
354+
}
355+
fi
356+
done
371357

372358
# 修改目标文件
373359
local modified_count=0
@@ -380,7 +366,7 @@ modify_cursor_app_files() {
380366
if [ ! -f "$file" ]; then
381367
log_warn "文件不存在: ${file/$temp_dir\//}"
382368
continue
383-
fi
369+
}
384370

385371
log_debug "处理文件: ${file/$temp_dir\//}"
386372

@@ -389,27 +375,27 @@ modify_cursor_app_files() {
389375
log_error "无法创建文件备份: ${file/$temp_dir\//}"
390376
continue
391377
}
378+
379+
# 读取文件内容
380+
local content=$(cat "$file")
392381

393-
# 使用精确位置替换
394-
local content=$(<"$file")
395-
local uuid_pos=$(grep -b -o "IOPlatformUUID" <<< "$content" | cut -d: -f1)
396-
382+
# 查找 IOPlatformUUID 的位置
383+
local uuid_pos=$(printf "%s" "$content" | grep -b -o "IOPlatformUUID" | cut -d: -f1)
397384
if [ -z "$uuid_pos" ]; then
398-
log_warn "未找到IOPlatformUUID标记: ${file/$temp_dir\//}"
385+
log_warn "$file 中未找到 IOPlatformUUID"
399386
continue
400-
fi
401-
402-
# 定位最近的switch语句
403-
local switch_pos=$(grep -b -o "switch" <<< "${content:0:$uuid_pos}" | tail -1 | cut -d: -f1)
404-
387+
}
388+
389+
# 从 UUID 位置向前查找 switch
390+
local before_uuid=${content:0:$uuid_pos}
391+
local switch_pos=$(printf "%s" "$before_uuid" | grep -b -o "switch" | tail -n1 | cut -d: -f1)
405392
if [ -z "$switch_pos" ]; then
406-
log_warn "未找到switch语句: ${file/$temp_dir\//}"
393+
log_warn "$file 中未找到 switch 关键字"
407394
continue
408-
fi
409-
410-
# 精确修改内容
411-
local new_content="${content:0:$switch_pos}return crypto.randomUUID();\n${content:$switch_pos}"
412-
if printf "%b" "$new_content" > "$file"; then
395+
}
396+
397+
# 构建新的文件内容
398+
if printf "%sreturn crypto.randomUUID();\n%s" "${content:0:$switch_pos}" "${content:$switch_pos}" > "$file"; then
413399
((modified_count++))
414400
log_info "成功修改文件: ${file/$temp_dir\//}"
415401
else
@@ -425,22 +411,18 @@ modify_cursor_app_files() {
425411
log_error "未能成功修改任何文件"
426412
rm -rf "$temp_dir"
427413
return 1
428-
fi
414+
}
429415

430416
# 重新签名应用
431417
log_info "重新签名应用..."
432-
{
433-
# 清除扩展属性
434-
xattr -cr "$temp_app"
435-
436-
# 尝试重新签名
437-
if ! codesign --force --deep --sign - --preserve-metadata=entitlements,identifier,flags "$temp_app" 2>/dev/null; then
438-
log_warn "应用重新签名失败,您可能需要:"
439-
log_warn "1. 前往 系统设置 -> 隐私与安全性"
440-
log_warn "2. 在『安全性』中找到 Cursor 相关提示"
441-
log_warn "3. 点击『仍要打开』"
442-
fi
418+
codesign --sign - "$temp_app" --force --deep || {
419+
log_warn "应用重新签名失败"
443420
}
421+
422+
# 关闭原应用
423+
log_info "正在关闭 Cursor..."
424+
osascript -e 'tell application "Cursor" to quit' || true
425+
sleep 2
444426

445427
# 创建应用备份
446428
local backup_app="/Applications/Cursor.backup.${timestamp}.app"
@@ -449,28 +431,27 @@ modify_cursor_app_files() {
449431
log_error "创建备份失败"
450432
rm -rf "$temp_dir"
451433
return 1
452-
fi
434+
}
453435

454436
# 替换原应用
455437
log_info "安装修改版应用..."
456-
if ! ditto "$temp_app" "$CURSOR_APP_PATH"; then
438+
if ! mv "$temp_app" "/Applications/"; then
457439
log_error "应用替换失败,正在恢复..."
458440
mv "$backup_app" "$CURSOR_APP_PATH"
459441
rm -rf "$temp_dir"
460442
return 1
461-
fi
443+
}
462444

463445
# 清理临时文件
464446
rm -rf "$temp_dir"
465447

466448
# 设置权限
467449
chown -R "$CURRENT_USER:staff" "$CURSOR_APP_PATH"
468-
find "$CURSOR_APP_PATH" -type d -exec chmod 755 {} \;
469-
find "$CURSOR_APP_PATH" -type f -exec chmod 644 {} \;
450+
chmod -R 755 "$CURSOR_APP_PATH"
470451

471452
# 重建 LaunchServices 数据库
472-
log_info "正在重建 LaunchServices 数据库..."
473-
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f "$CURSOR_APP_PATH"
453+
# log_info "正在重建 LaunchServices 数据库..."
454+
# /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -f "$CURSOR_APP_PATH"
474455

475456
log_info "Cursor 主程序文件修改完成!原版备份在: ${backup_app/$HOME/\~}"
476457
return 0

0 commit comments

Comments
 (0)