How to change icon of alias created using applescript?

前端 未结 1 1861
生来不讨喜
生来不讨喜 2021-01-16 09:33

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

相关标签:
1条回答
  • 2021-01-16 10:15

    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
    
    0 讨论(0)
提交回复
热议问题