How to change icon of alias created using applescript?

本秂侑毒 提交于 2019-12-01 10:50:42

问题


I have an applescript which creates a shortcut on a desktop to an executable on the file system. The excutable has the standard exec icon . Is it possible to change the icon to point to say an icns file ?

I've read you can do it using a third party program as mentioned in Change icon of folder with AppleScript?

but is it possible without using an external program to do this ?

This is my script

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

Note: I can also create the same shortcut using ln -s command but in that I don't get any icon, its just a blank page symbol shortcut


回答1:


Dealing with alias files like that is a bit of a pain, since the Finder seems to lose track of an alias file of an alias after you rename it (even though it is an alias). One solution would be to use some AppleScriptObj-C to set the icon before renaming the alias file, for example (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


来源:https://stackoverflow.com/questions/54898756/how-to-change-icon-of-alias-created-using-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!