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

查看:27
本文介绍了如何更改使用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 更改文件夹图标?

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 ?

这是我的剧本

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天全站免登陆