如何更改使用 applescript 创建的别名图标?

How to change icon of alias created using applescript?

我有一个 applescript,它在桌面上为文件系统上的可执行文件创建快捷方式。可执行文件具有标准的可执行图标 。是否可以将图标更改为指向 icns 文件?

我了解到您可以使用中提到的第三方程序来完成 Change icon of folder with AppleScript?

但是否可以不使用外部程序来执行此操作?

这是我的脚本

set source_file to (POSIX file "path to my exectuable")
tell application "Finder"
make new alias file at desktop to source_file
set name result to "My Shortcut"
end tell

注意:我也可以使用 ln -s 命令创建相同的快捷方式,但我没有得到任何图标,它只是一个空白页符号快捷方式

处理这样的别名文件有点痛苦,因为在您重命名别名后,Finder 似乎无法跟踪别名的别名文件(即使它是别名)。一种解决方案是在重命名别名文件之前使用一些 AppleScriptObj-C 设置图标,例如 (Mojave):

use framework "Foundation"
use scripting additions

set sourceFile to (choose file)
tell application "Finder"
  set newAlias to (make new alias file at desktop to sourceFile) as alias
  my setIcon(newAlias)
  set name of newAlias to "My Shortcut"
end tell

to setIcon(fileRef)
  set iconImage to current application's NSImage's alloc's initWithContentsOfFile:"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertCautionIcon.icns" -- example image file
  current application's NSWorkspace's sharedWorkspace's setIcon:iconImage forFile:(POSIX path of fileRef) options:0
end setIcon