从 AppleScript 路径中提取文件扩展名

3

我正在编写一份Apple Script,最终将为从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

alias "Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr"

现在我需要提取包含路径和文件前缀000000001613eaa6(使用这个问题),以便我可以在文件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: “.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”

2个回答

1

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的文本项分隔符”,否则这段代码会给我一个错误,我还保存了以前的设置以在更大的代码块中使用:“set delims to AppleScript的文本项分隔符<br> set AppleScript的文本项分隔符为“。”<br> … set AppleScript的文本项分隔符为delims” - stvs

1

迷你评论似乎不允许发布代码,所以这是我对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商业标记脚本的iOS链接。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接