查看更多代码真的很有帮助。特别感兴趣的是您正在使用的值以及它是如何派生的。查看您收到的确切错误消息也很有用(如果您从 Script Editor/ 运行程序,您应该能够从 AppleScript 错误对话框表中复制文本AppleScript 编辑器)。
file track 类的字典条目显示其location 属性是可写的alias 值。您可能遇到的问题是您没有使用该值的别名。
以下代码显示了如何使用来自choose file(返回alias)的交互式提示来更改轨道的位置:
set m to path to music folder
tell application "iTunes"
set trk to first item of selection
set l to location of trk
if class of l is alias then
set m to l
end if
set {d, a, n} to {database ID, artist, name} of trk
choose file with prompt "Choose the file to use for " & d & ": " & a & "—" & n default location m
set location of trk to result
end tell
choose file 方法不是您想要的,因为您正在执行某种基于字符串的自动路径名转换。
在 AppleScript 中使用路径名时,您可能会使用两种路径名:POSIX 和 HFS。 POSIX 路径名具有斜杠分隔的组件(并允许在任何组件内使用冒号)。 HFS 路径名有冒号分隔的组件(并且允许在任何组件内使用斜杠),并且它们通常以卷名组件开头。
要将存储在变量 str 中的 POSIX 路径名转换为 AppleScript alias,请使用以下表达式:
POSIX file str as alias
要将存储在变量 str 中的 HFS 路径名转换为 AppleScript alias,请使用以下表达式:
alias str
例如:
tell application "iTunes"
set trk to first item of selection
set l to location of trk
set newPath to my computeNewPath(POSIX path of l)
set location of trk to POSIX file newPath as alias
end tell
to computeNewPath(pp)
-- presumably something interesting happens here
return pp
end computeNewPath