【问题标题】:Filename without extension in AppleScriptAppleScript中没有扩展名的文件名
【发布时间】:2016-08-21 10:28:55
【问题描述】:

如何在 AppleScript 中获取不带扩展名的文件名?例如:

Penny.Dreadful.S03E01.720p.HDTV.x264-BATV.mp4

Penny.Dreadful.S03E01.720p.HDTV.x264-BATV

一个文件对我来说已经足够了,但可以放入更多文件。 以下代码采用文件名但带有扩展名。我不想要扩展名。感谢您的帮助。

try
set theNames to {}
tell application "Finder"
   repeat with i in (get selection)
       set end of theNames to name of i
   end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set the clipboard to theNames as text
set text item delimiters to TID
end try

【问题讨论】:

    标签: applescript rename file-rename


    【解决方案1】:

    试试这个,而不是 try 块,脚本检查空选择,如果有扩展名,则通过减去扩展名来提取文件名。

    set theNames to {}
    tell application "Finder"
        set theSelection to selection
        if theSelection is {} then return
        repeat with anItem in theSelection
            set {name:fileName, name extension:fileExtension} to anItem
            set end of theNames to my removeExtension(fileName, fileExtension)
        end repeat
    end tell
    set {TID, text item delimiters} to {text item delimiters, return}
    set the clipboard to theNames as text
    set text item delimiters to TID
    
    on removeExtension(Nm, Ex)
        if Ex is missing value or Ex is "" then return Nm
        return text 1 thru ((count Nm) - (count Ex) - 1) of Nm
    end removeExtension
    

    【讨论】:

    • 谢谢。效果很好??
    • 我们可以在 Automator 中获得文件名的输出吗?
    • 当然,将代码放入 AppleScript 操作并返回 theNames。但是您可以使用 Automator Finder 选择操作。
    猜你喜欢
    • 2011-05-15
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2011-12-28
    • 2018-08-06
    • 1970-01-01
    • 2017-06-27
    相关资源
    最近更新 更多