2011-11-28 85 views
0

我的AppleScript中有choose file。當我運行腳本並選擇一個文件時,輸出始終是文件擴展名的完整文件路徑。例如:蘋果腳本擺脫完整路徑

Macintosh HD:Developer:About Xcode.pdf 

是我不想要的。我只是想:

About Xcode 


當存在不止一種.下面的答案被Kassym Dorsel不起作用。

以下答案由LRI不set x to choose file工作:

error "Can’t make quoted form of alias \"Macintosh HD:Applications:Firefox.app:\" into  type Unicode text." number -1700 from quoted form of alias "Macintosh HD:Applications:Firefox.app:" to Unicode text 

回答

1

您可以使用Finder操縱Finder項目的名稱:

choose file with prompt "Pick one" 
set filepath to result 

tell application "Finder" to set {dispName, nameExt, isHidden} to ¬ 
    the {displayed name, name extension, extension hidden} of the filepath 


if isHidden or nameExt is equal to "" then 
    dispName 
else 
    (characters 1 through (-2 - (count of nameExt)) of dispName) as text 
end if 

set baseName to result 
+0

@Lri謝謝你接受延伸;固定。由於使用'choose file'選擇文件,文件將始終存在 - 您需要使用'choose file name'來獲取對可能不存在的文件的引用。 –

0

這將工作:

set a to "Macintosh HD:Developer:About.Xcode.pdf" 
set text item delimiters to ":" 
set temp to last text item of a 
set text item delimiters to "." 
set temp to text items 1 thru -2 of temp as text 

給人=>About.Xcode

+0

當文件名具有多個'.'字符時,將失敗。 –

+0

@Barber你是對的。 – gadgetmo

+0

我最初的解決方案是特定案例,是的。我已經更新了我的解決方案,以允許文件名中的句點。它現在應該適用於一切。 –