|
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
|
|
# 项目信息
|
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
|
|
|
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
|
|
|
|
|
ICON_DIR="$PROJECT_ROOT/assets/icons"
|
|
|
|
|
APP_ICON="$ICON_DIR/app.icns"
|
|
|
|
|
|
|
|
|
|
echo "🎨 Creating application icon..."
|
|
|
|
|
|
|
|
|
|
# 创建图标目录
|
|
|
|
|
mkdir -p "$ICON_DIR"
|
|
|
|
|
|
|
|
|
|
# 创建临时目录
|
|
|
|
|
TEMP_DIR=$(mktemp -d)
|
|
|
|
|
ICONSET_DIR="$TEMP_DIR/app.iconset"
|
|
|
|
|
mkdir -p "$ICONSET_DIR"
|
|
|
|
|
|
|
|
|
|
# 如果没有源图像,创建一个简单的默认图标
|
|
|
|
|
if [ ! -f "$ICON_DIR/icon.png" ]; then
|
|
|
|
|
echo "📝 Creating default icon..."
|
|
|
|
|
|
|
|
|
|
# 使用 sips 创建简单的彩色图标 (macOS 内置工具)
|
|
|
|
|
# 创建一个 1024x1024 的纯色图像作为基础
|
|
|
|
|
cat > "$TEMP_DIR/create_icon.py" << 'EOF'
|
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
import sys
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
def create_icon(size, output_path):
|
|
|
|
|
# 创建图像
|
|
|
|
|
img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
|
|
|
|
|
draw = ImageDraw.Draw(img)
|
|
|
|
|
|
|
|
|
|
# 绘制背景圆形
|
|
|
|
|
margin = size // 8
|
|
|
|
|
draw.ellipse([margin, margin, size-margin, size-margin],
|
|
|
|
|
fill=(58, 134, 255, 255), outline=(30, 100, 200, 255), width=size//50)
|
|
|
|
|
|
|
|
|
|
# 绘制文字
|
|
|
|
|
try:
|
|
|
|
|
font_size = size // 6
|
|
|
|
|
font = ImageFont.truetype("/System/Library/Fonts/Helvetica.ttc", font_size)
|
|
|
|
|
except:
|
|
|
|
|
font = ImageFont.load_default()
|
|
|
|
|
|
|
|
|
|
text = "W"
|
|
|
|
|
bbox = draw.textbbox((0, 0), text, font=font)
|
|
|
|
|
text_width = bbox[2] - bbox[0]
|
|
|
|
|
text_height = bbox[3] - bbox[1]
|
|
|
|
|
|
|
|
|
|
x = (size - text_width) // 2
|
|
|
|
|
y = (size - text_height) // 2 - size // 20
|
|
|
|
|
|
|
|
|
|
draw.text((x, y), text, fill=(255, 255, 255, 255), font=font)
|
|
|
|
|
|
|
|
|
|
# 保存图像
|
|
|
|
|
img.save(output_path, 'PNG')
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
sizes = [16, 32, 64, 128, 256, 512, 1024]
|
|
|
|
|
for size in sizes:
|
|
|
|
|
create_icon(size, f"{sys.argv[1]}/icon_{size}x{size}.png")
|
|
|
|
|
if size <= 512:
|
|
|
|
|
create_icon(size * 2, f"{sys.argv[1]}/icon_{size}x{size}@2x.png")
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
# 检查是否有 Python 和 PIL
|
|
|
|
|
if command -v python3 &> /dev/null && python3 -c "import PIL" 2>/dev/null; then
|
|
|
|
|
echo "✅ Using Python PIL to create icons"
|
|
|
|
|
python3 "$TEMP_DIR/create_icon.py" "$ICONSET_DIR"
|
|
|
|
|
else
|
|
|
|
|
echo "⚠️ Python PIL not available, creating simple icons with sips"
|
|
|
|
|
|
|
|
|
|
# 创建基础图标
|
|
|
|
|
BASE_SIZE=1024
|
|
|
|
|
BASE_ICON="$TEMP_DIR/base_icon.png"
|
|
|
|
|
|
|
|
|
|
# 使用 sips 创建基础图标 (需要一个现有的图像文件)
|
|
|
|
|
# 如果没有图像,我们创建一个简单的文本文件然后转换
|
|
|
|
|
echo "Creating base icon with textutil and sips..."
|
|
|
|
|
|
|
|
|
|
# 创建一个 RTF 文件
|
|
|
|
|
cat > "$TEMP_DIR/icon.rtf" << 'EOF'
|
|
|
|
|
{\rtf1\ansi\deff0 {\fonttbl {\f0 Times New Roman;}}
|
|
|
|
|
\f0\fs200\qc\cf1 W}
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
|
|
# 转换为图像 (这在 macOS 上应该可行)
|
|
|
|
|
if command -v textutil &> /dev/null; then
|
|
|
|
|
textutil -convert html "$TEMP_DIR/icon.rtf" -output "$TEMP_DIR/icon.html"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 手动创建不同尺寸的图标
|
|
|
|
|
sizes=(16 32 64 128 256 512 1024)
|
|
|
|
|
for size in "${sizes[@]}"; do
|
|
|
|
|
# 创建空白图像并着色
|
|
|
|
|
sips -z $size $size /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns --out "$ICONSET_DIR/icon_${size}x${size}.png" 2>/dev/null || {
|
|
|
|
|
# 如果失败,复制系统默认图标
|
|
|
|
|
cp /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns "$ICONSET_DIR/icon_${size}x${size}.png" 2>/dev/null || {
|
|
|
|
|
# 最后的后备方案:创建纯色文件
|
|
|
|
|
printf "\x89PNG\r\n\x1a\n" > "$ICONSET_DIR/icon_${size}x${size}.png"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 创建 @2x 版本
|
|
|
|
|
if [ $size -le 512 ]; then
|
|
|
|
|
cp "$ICONSET_DIR/icon_${size}x${size}.png" "$ICONSET_DIR/icon_${size}x${size}@2x.png"
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 确保所有必需的图标文件存在
|
|
|
|
|
declare -a required_icons=(
|
|
|
|
|
"icon_16x16.png"
|
|
|
|
|
"icon_16x16@2x.png"
|
|
|
|
|
"icon_32x32.png"
|
|
|
|
|
"icon_32x32@2x.png"
|
|
|
|
|
"icon_128x128.png"
|
|
|
|
|
"icon_128x128@2x.png"
|
|
|
|
|
"icon_256x256.png"
|
|
|
|
|
"icon_256x256@2x.png"
|
|
|
|
|
"icon_512x512.png"
|
|
|
|
|
"icon_512x512@2x.png"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
echo "📋 Checking required icon files..."
|
|
|
|
|
missing_icons=false
|
|
|
|
|
for icon in "${required_icons[@]}"; do
|
|
|
|
|
if [ ! -f "$ICONSET_DIR/$icon" ]; then
|
|
|
|
|
echo "⚠️ Missing: $icon"
|
|
|
|
|
missing_icons=true
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
if [ "$missing_icons" = true ]; then
|
|
|
|
|
echo "🔧 Creating missing icons from system template..."
|
|
|
|
|
# 使用系统默认应用图标作为模板
|
|
|
|
|
TEMPLATE_ICON="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns"
|
|
|
|
|
|
|
|
|
|
for icon in "${required_icons[@]}"; do
|
|
|
|
|
if [ ! -f "$ICONSET_DIR/$icon" ]; then
|
|
|
|
|
# 从系统图标创建所需尺寸
|
|
|
|
|
size=$(echo "$icon" | sed 's/icon_\([0-9]*\)x[0-9]*.*\.png/\1/')
|
|
|
|
|
sips -z $size $size "$TEMPLATE_ICON" --out "$ICONSET_DIR/$icon" 2>/dev/null || {
|
|
|
|
|
echo "⚠️ Could not create $icon, using placeholder"
|
|
|
|
|
touch "$ICONSET_DIR/$icon"
|
|
|
|
|
}
|
|
|
|
|
fi
|
|
|
|
|
done
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 创建 .icns 文件
|
|
|
|
|
echo "🔨 Creating .icns file..."
|
|
|
|
|
if iconutil -c icns "$ICONSET_DIR" -o "$APP_ICON"; then
|
|
|
|
|
echo "✅ Icon created successfully: $APP_ICON"
|
|
|
|
|
else
|
|
|
|
|
echo "❌ Failed to create icon, using system default"
|
|
|
|
|
# 复制系统默认图标
|
|
|
|
|
cp "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns" "$APP_ICON" 2>/dev/null || {
|
|
|
|
|
echo "⚠️ Could not copy system icon"
|
|
|
|
|
}
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# 清理临时文件
|
|
|
|
|
rm -rf "$TEMP_DIR"
|
|
|
|
|
|
|
|
|
|
echo "🎉 Icon creation completed!"
|
|
|
|
|
echo "📁 Icon location: $APP_ICON"
|