2016-08-21 62 views
1

如何在AppleScript中沒有擴展名的情況下獲取文件名?例如: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 

回答

0

試試這個,而不是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 
+0

謝謝。 Works是好的 –

+0

我們可以得到輸出到Automator的文件名嗎? –

+0

當然,將代碼放在AppleScript動作中並返回'theNames'。但是您可以使用Automator Finder選擇操作。 – vadian