0

在我的一个脚本中,我需要文件名:

set input to choose file multiple selections allowed yes
tell application "System Events"
    set {nameInfo, sourcePath} to {name of item 1 of input, POSIX path of container of item 1 of input}
end tell

但是如果文件名中有一个“/”,例如“soso/lala.txt”,则名称返回为“soso:lala.txt”。我也试过:

do shell script "basename " & item 1 of input

但随后只有“lala.txt”到达。我可以以某种方式欺骗它以得到“soso/lala.txt”作为回报吗?

4

1 回答 1

1

文件名中的冒号和斜杠在 AppleScript 中容易出错,因为冒号是 HFS 路径(默认 AppleScript 类型)中的路径分隔符,而斜杠是 POSIX 路径中的路径分隔符

两个建议:

  1. 利用displayed name

    set input to choose file with multiple selections allowed
    tell application "System Events"
        set {nameInfo, sourcePath} to {displayed name of item 1 of input, POSIX path of container of item 1 of input}
    end tell
    
  2. 使用Finder

    set input to choose file with multiple selections allowed
    tell application "Finder"
        set {nameInfo, sourcePath} to {name of item 1 of input, POSIX path of (container of item 1 of input as text)}
    end tell
    
于 2019-12-19T12:04:32.727 回答