导入基础框架后,Applescript无法识别路径



我可以使用以下代码在AppleScript中显示自定义图标

set iconPath to "/Users/dutt/myFolder/AppIcon.icns" as POSIX file
set theContent to " hi"
display dialog theContent with icon file iconPath with title "Hello" buttons {"Cancel", "Ok"} default button "Ok"

当我使用下面的代码导入基础时,它抛出了不包含图标的错误文件

use framework "Foundation"
use scripting additions
set iconPath to "/Users/dutt/myFolder/AppIcon.icns" as POSIX file
set theContent to " hi"
display dialog theContent with icon file iconPath with title "Hello" buttons {"Cancel", "Ok"} default button "Ok"

我认为这与路径问题有关,苹果脚本在使用基础框架后无法获得图标路径

我的建议是使用相对的HFS路径,path to home folder指向当前用户的主文件夹。

这避免了POSIX路径-POSIX文件-别名舞蹈,并在使用和不使用Foundation的情况下工作

set iconPath to alias ((path to home folder as text) & "myFolder:AppIcon.icns")
set theContent to " hi"
display dialog theContent with icon iconPath with title "Hello" buttons {"Cancel", "Ok"} default button "Ok"

注意with icon后缺少file关键字

问题是由在AppleScript中开始实现Objective-C框架后解决文件引用的方式引起的。

解决方案是使用强制来构建文件引用。所以,改变:

icon file iconPath

至:

icon (iconPath as alias)

或者甚至可能需要改变:

set iconPath to "/Users/dutt/myFolder/AppIcon.icns" as POSIX file

至:

set iconPath to "/Users/dutt/myFolder/AppIcon.icns"

然后在display dialog命令中构建文件引用,如下所示:

icon (iconPath as POSIX file as alias)

最新更新