2012-02-09 113 views
1

我試圖讓文件名沒有當前文檔文件的擴展名中的OmniGraffle專業5如何從Omnigraffle沒有擴展名得到文件名?

tell application "OmniGraffle Professional 5" 
    set _document to front document 
    set _path to path of _document 

    -- Get filename without extension 
    tell application "Finder" 
     set {_filename, _extension, _ishidden} to the 
      {displayed_name, name_extension, extension_hidden} of the _path 
    end tell 
end tell 

這給了我下面的錯誤:error "Can’t get displayed_name of \"/Users/ca/Downloads/Feb 8.graffle\"." number -1728 from displayed_name of "/Users/ca/Downloads/Feb 8.graffle"

我發現了一些相關的問題和頁面,但我有點失落,真的不明白爲什麼它不起作用。

感謝您的幫助!

回答

2

你需要將其更改爲以下:

tell application "OmniGraffle Professional 5" 
    set _document to front document 
    set _path to path of _document 

    -- Get filename without extension 
    tell application "Finder" 
    set {_filename, _extension, _ishidden} to the ¬ 
       {displayed name, name extension, extension hidden} ¬ 
       of ((the _path as POSIX file) as alias) 
    end tell 
    if (_extension ≠ missing value) then 
     set baseName to text 1 thru -((length of _extension) + 2) of _filename 
    end if 

end tell 

「前文件路徑」返回一個文件,它只是一個簡單的字符串的POSIX路徑。爲了能夠獲得有關某個項目的信息,Finder將需要對該文件的別名引用。當你傳遞一個純字符串時,它會得到一個錯誤,因爲一個純字符串不會有這些屬性。要獲得別名,您需要首先將純路徑強制轉換爲POSIX文件,然後將POSIX文件強制轉換爲別名。

除非您在其他地方定義了這些變量,否則您需要刪除{displayed_name,name_extension,extension_hidden}中的下劃線。當你在與留在這些下劃線的「編譯」的代碼,它看起來像下面的圖片:

enter image description here

因此,AppleScript的是解析displayed_name是一個變量,而不是一個屬性。現在,如果你已經在其他地方定義了這些變量,如屬性中腳本的頂部,那很好。但是,如果沒有,您需要刪除下劃線,因爲Finder項目的屬性名稱中沒有下劃線。當您刪除下劃線時,着色顯示正確(屬性爲紫色,變量爲綠色)。

enter image description here

注意,仍然不會給你的文件名沒有擴展名。爲了得到這一點,你就需要做一些像我使用text n thru m

if (_extension ≠ missing value) then 
    set baseName to text 1 thru -((length of _extension) + 2) of _filename 
end if 
+0

非常感謝。這是一個非常好的答案。謝謝你的解釋。 – charlax 2012-02-09 20:02:57

1

首先,您需要爲您所針對的任何應用程序的屬性使用正確的標籤 - 這些標籤可以在應用程序腳本字典中找到。接下來的問題是,搜索不知道任何關於POSIX路徑,這顯然是什麼的OmniGraffle正在恢復的文件的路徑,所以你需要的路徑強迫到的東西,在搜索不知道,如別名。

tell application "Finder" 
    set {_filename, _extension, _ishidden} to the {displayed name, name extension, extension hidden} of (_path as POSIX file as alias) 
end tell 
+0

「的POSIX文件作爲別名」看上去錯誤的確在添加的行 - 如果有什麼可以強制轉換爲POSIX路徑它爲什麼不能被直接強制別名?我錯過了什麼嗎? – nekomatic 2012-02-09 13:16:21

+1

別名具有POSIX路徑屬性,但實際上沒有一種方便的方法可以從POSIX路徑轉換回來,因爲它僅僅是文本。在這種情況下,我可能不必轉換爲別名,但是之後必須使用其他術語,例如item,因爲POSIX文件是一個僞類,只是工作愚蠢。一個別名是一個更通用的文件說明符類型,所以我只是爲了避免以後出現任何其他問題。 – 2012-02-09 20:01:50