【问题标题】:Extracting File Extensions from AppleScript Paths从 AppleScript 路径中提取文件扩展名
【发布时间】:2012-10-06 03:05:45
【问题描述】:

我正在编写一个 Apple 脚本,该脚本最终将标记从 EyeTV 导出的所有 iTunes 的广告。但是我遇到了 AppleScript 路径的一个简单问题,EyeTV 应用程序将其作为录制位置返回。这是上下文:

set recordingID to 370404006
set myid to recordingID as integer
tell application "EyeTV"
    set eyetvr_file to get the location of recording id myid as alias
end tell
return eyetvr_file

别名“Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr”

现在我需要提取包含路径和文件前缀 000000001613eaa6(使用 this question),以便在文件 000000001613eaa6.edl 中查找相应的商业标记。这是打嗝:

tell application "Finder"
    set eyetv_path to container of eyetvr_file
    set root_name to name of eyetvr_file
    set fext to name extension of eyetvr_file
end tell

结果是,

eyetv_path:应用程序“Finder”的磁盘“Macintosh HD2”的文件夹“Documents”文件夹“EyeTV Archive”的文档文件“South Park.eyetv”

root_name: "000000001613eaa6.eyetvr"

fext: ""

fext 应该是“.eyetvr”,而不是空字符串。如何从 eyetvr_file 或 root_name 中正确提取“.eyetvr”?我已经尝试了一堆像

set fext to name extension of (eyetvr_file as document)

但这会产生类似的错误,

错误“无法将别名 \"Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr\" 转换为类型文档。”编号 -1700 从别名“Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr”到文档

【问题讨论】:

    标签: directory applescript filenames filepath file-extension


    【解决方案1】:

    Mini-cmets 显然不允许发布代码,所以这里是我对 Lauri Ranta 不错的解决方案的修改:

    do shell script "touch /tmp/some.file.eyetvr"
    set delims to AppleScript's text item delimiters
    set AppleScript's text item delimiters to "."
    tell application "Finder"
        set n to name of file (POSIX file "/tmp/some.file.eyetvr")
        if n contains "." then set n to (text items 1 thru -2 of n) as text
        n -- some.file
    end tell
    set AppleScript's text item delimiters to delims
    

    当我完成该项目后,我会发布一个指向 EyeTV-commercial-marking-script-for-iOS 的链接。

    【讨论】:

    【解决方案2】:

    Finder 无法识别所有文件扩展名。不过,您可以只使用文本项分隔符来删除最后一部分。

    do shell script "touch /tmp/some.file.eyetvr"
    set text item delimiters to "."
    tell application "Finder"
        set n to name of file (POSIX file "/tmp/some.file.eyetvr")
        if n contains "." then set n to (text items 1 thru -2 of n) as text
        n -- some.file
    end tell
    

    【讨论】:

    • 谢谢!这与下面的编辑完美配合,并提供了比我链接到的更好的解决方案。除非我指定“AppleScript 的文本项分隔符”,否则此代码给我一个错误,并且我还保存了以前的设置以在更大的代码块中使用:“将分隔符设置为 AppleScript 的文本项分隔符
      将 AppleScript 的文本项分隔符设置为”。 "
      … 将 AppleScript 的文本项分隔符设置为 delims"
    猜你喜欢
    • 2011-12-08
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2011-03-10
    • 2022-06-23
    • 1970-01-01
    • 2010-10-01
    相关资源
    最近更新 更多