首页 > 解决方案 > Applescript 获取名称异常

问题描述

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

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”作为回报吗?

标签: applescriptgetnameinfo

解决方案


文件名中的冒号和斜杠在 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
    

推荐阅读