在El Capitan下,AppleScript无法直接设置图标图像。然而,可以通过使用AppleScript与Shell脚本相结合的方法来实现此功能。
以下是一个示例代码,演示如何使用AppleScript和Shell脚本来设置图标图像:
set imagePath to POSIX path of "path/to/image.png" -- 替换为实际的图像路径
-- 创建一个Shell脚本命令,用于设置图标图像
set setIconScript to "sips -i " & quoted form of imagePath & "; DeRez -only icns " & quoted form of imagePath & " > " & quoted form of (path to temporary items folder as text) & "icon.rsrc; SetFile -a C " & quoted form of (path to applications folder as text) & "YourApp.app; Rez -append " & quoted form of (path to temporary items folder as text) & "icon.rsrc -o " & quoted form of (path to applications folder as text) & "YourApp.app/Icon\r"
-- 运行Shell脚本命令
do shell script setIconScript
-- 重新加载应用程序图标
tell application "Finder"
tell application file "YourApp.app" of folder (path to applications folder)
activate
end tell
end tell
请注意,上述代码中的path/to/image.png
应替换为实际的图像路径,YourApp.app
应替换为要设置图标的应用程序的名称。
此代码的工作原理如下:
POSIX path of
将图像路径转换为Shell脚本所需的POSIX路径格式。sips
命令用于将图像转换为.icns格式,DeRez
命令用于将.icns文件转换为资源文件,SetFile
命令用于将应用程序标记为包含自定义图标,Rez
命令用于将自定义图标附加到应用程序。do shell script
运行Shell脚本命令。请注意,此方法可能需要管理员权限才能成功设置图标图像。