2015-12-24 39 views
0

我有一個腳本,用您選擇的圖像來更改文件夾的圖標,它工作正常,但我希望能夠讓腳本自動選擇第一張圖像該文件夾並將其指定爲將用於創建圖標的圖像。選擇在Fiinder中選擇的文件夾中的第一個圖像

這是我到目前爲止。它無法選擇圖像,並根據我的嘗試給出各種錯誤消息。任何幫助將不勝感激!

set exePath to (path to home folder as text) & "downloads:SetFileIcon" 

tell application "Finder" 
    set theSelection to selection -- This is a list of selected items, even if only 1 item is selected. 
    set theFile to (item 1 of theSelection) as text -- Get the first item in the list. We make it text so we can convert it to a posix path for  SetFileIcon. 
    set theimage to item 1 of theSelection 
    set imagePath to (path to desktop folder as text) and theimage 
end tell 


do shell script quoted form of POSIX path of exePath & " -image " & quoted form of POSIX path of imagePath & " -file " & quoted form of POSIX path of theFile 

回答

0

第一眼看你的腳本有幾個問題。首先,你永遠不會要求文件夾的內容。您將theFile設置爲item 1 of theSelection,然後您將theimage設置爲item 1 of theSelection。我希望你的意思是把theimage設置爲document files of theFile的第1項。請注意,theFile實際上是選定的文件夾。

二,「和」是一個布爾運算;要連接AppleScript中的文本,請使用「&」。因此,即使theimage包含圖像的名稱,您也需要使用& theimage

但是,由於您正在向Finder詢問信息,因此Finder已知道任何特定文件的完整路徑。所以,一旦你得到一個圖像,你可以直接向Finder詢問路徑。

tell application "Finder" 
    -- This is a list of selected items, even if only 1 item is selected. 
    set theSelections to the selection 
    -- This code assumes that the first selected item is a folder 
    set theFolder to the first item of theSelections 

    -- we want the first document that is also an image 
    set theImages to the document files of theFolder whose kind contains "image" 
    set theImage to item 1 of theImages as text 

    -- and we want it's path 
    set imagePath to POSIX path of theImage 
end tell 
相關問題