Extracting File Extensions from AppleScript Paths

白昼怎懂夜的黑 提交于 2019-12-10 10:59:49

问题


I'm writing an Apple Script that ultimately will mark commercials for all iTunes exports from EyeTV. But I'm running into a simple problem with AppleScript paths, which the EyeTV app returns as recording locations. Here's the context:

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"

Now I need to extract the containing path and the file prefix 000000001613eaa6 (using this question) so I can look for the corresponding commercial markings in the file 000000001613eaa6.edl. Here's the hiccup:

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

The results are,

eyetv_path: document file "South Park.eyetv" of folder "EyeTV Archive" of folder "Documents" of disk "Macintosh HD2" of application "Finder"

root_name: "000000001613eaa6.eyetvr"

fext: ""

fext should be ".eyetvr", not the empty string. How do I correctly extract ".eyetvr" from either eyetvr_file or root_name? I've tried a bunch of hack like

set fext to name extension of (eyetvr_file as document)

But this gives error like,

error "Can’t make alias \"Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr\" into type document." number -1700 from alias "Macintosh HD2:Documents:EyeTV Archive:South Park.eyetv:000000001613eaa6.eyetvr" to document


回答1:


Finder doesn't recognize all filename extensions. You could just use text item delimiters to remove the last part though.

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



回答2:


Mini-comments apparently do not allow posting code, so here are my edits to Lauri Ranta's nice solution:

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

I'll post a link to the EyeTV-commercial-marking-script-for-iOS when I've finished that project.



来源:https://stackoverflow.com/questions/12907517/extracting-file-extensions-from-applescript-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!