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

时间:2019-02-27 05:41:25

标签: icons applescript alias icns

我有一个applescript,可在桌面上为文件系统上的可执行文件创建快捷方式。可执行文件具有标准的exec图标。是否可以将图标更改为指向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命令创建相同的快捷方式,但是我没有得到任何图标,它只是空白页面符号快捷方式

1 个答案:

答案 0 :(得分:2)

使用这样的别名文件进行处理有点麻烦,因为在您重命名后,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
相关问题