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

Applescript is unable to recognise path after importing foundation framework

我可以使用以下代码在 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"

我认为是路径问题,apple脚本在使用foundation framework后无法获取图标路径

此问题是由您开始在 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)

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

这避免了 POSIX 路径 - POSIX 文件 - 别名 dance 并在有和没有 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 关键字