【问题标题】:How to change icon of alias created using applescript?如何更改使用applescript创建的别名图标?
【发布时间】:2019-07-20 18:41:03
【问题描述】:

我有一个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 命令创建相同的快捷方式,但我没有得到任何图标,它只是一个空白页符号快捷方式

【问题讨论】:

    标签: icons applescript alias icns


    【解决方案1】:

    处理这样的别名文件有点痛苦,因为在您重命名别名文件后(即使它是别名),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
    

    【讨论】:

    • 此解决方案适用于 HighSierra,但对于 El Capitan,图标未更改。知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 2011-07-13
    相关资源
    最近更新 更多