如何更改使用applescript创建的别名图标? [英] How to change icon of alias created using applescript?

查看:227
本文介绍了如何更改使用applescript创建的别名图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

我读过您可以使用
使用AppleScript更改文件夹的图标吗?

但是否可以在不使用外部程序的情况下进行为此?

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

这是我的脚本

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):

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

这篇关于如何更改使用applescript创建的别名图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆