2013-10-08 41 views
0

我想按大小對照片進行分類,廣告最容易按照寬度進行分類。我想獲得寬度大於1919px的圖片並將其放入另一個文件夾中。我用Google搜索了幾個小時,但沒有運氣。如何在Applescript中按寬度排序圖像?

我得到這個錯誤:error "Image Events got an error: Can’t make item 1 of dimensions of image \"1mouwi.jpg\" into type specifier." number -1700 from item 1 of dimensions of image "1mouwi.jpg" to specifieritem 1重複循環。有關如何解決這個問題的任何幫助?

我的代碼:

for f in ~/Pictures/DESKTOPS/*; do [[ $(mdls -rn kMDItemPixelWidth "$f") -ge 1920 ]] && mv "$f" ~/Pictures/DESKTOPS_HD/; done

如果mdls沒有顯示一些圖像的大小,儘量使用ImageMagick或sips

set picFolder to alias ":Users:USERNAME:Pictures:DESKTOPS:" 
set hdFolder to alias ":Users:USERNAME:Pictures:DESKTOPS_HD:" 


tell application "System Events" 
    set photos1 to path of files of picFolder 
    set num to count of photos1 
    set photos to items 2 thru num of photos1 
end tell 

set hd to {} 
repeat with imgPath in photos 
    set imgAlias to alias imgPath 
    tell application "Image Events" 
     set img to open imgPath 
     set width to item 1 of dimensions of img 
     if width > 1919.0 then 
      set end of hd to imgAlias 
     end if 
     close img 
    end tell 
end repeat 

tell application "Finder" 
    move hd to hdFolder 
end tell 

回答

2

一個簡單的解決方案是使用「get」命令和括號。一般來說,「get」是可以理解的,所以你通常不必在命令中使用它......但是applescript中的怪癖需要你有時候明確地使用它......特別是當你在一行中有多個命令時。另外,括號確保代碼寬度線的多個操作都按照正確的順序

set width to item 1 of (get dimensions of img) 

執行但是有可以使用所以這裏就是我會寫腳本其他一些優化。

set picFolder to (path to pictures folder as text) & "DESKTOPS:" 
set hdFolder to (path to pictures folder as text) & "DESKTOPS_HD:" 

tell application "System Events" to set photos1 to path of files of folder picFolder 
set photos to items 2 thru end of photos1 

set hd to {} 
repeat with imgPath in photos 
    set imgAlias to alias imgPath 
    tell application "Image Events" 
     set img to open imgAlias 
     set width to item 1 of (get dimensions of img) 
     close img 
    end tell 

    if width > 1919 then 
     set end of hd to imgAlias 
    end if 
end repeat 

tell application "Finder" 
    if hd is not {} then move hd to folder hdFolder 
end tell 
+0

感謝您的優化!我會在稍後嘗試。 – ZuluDeltaNiner

+0

我如何使這個文件夾操作? – ZuluDeltaNiner

0

你可能只是在外殼使用mdls -rn kMDItemPixelWidth

brew install imagemagick; identify -format %w input.png

sips --getProperty pixelWidth input.png | awk 'END{print $NF}'

+0

第一個是applescript,其他的是UNIX,對吧? – ZuluDeltaNiner

0

它的工作原理是如果將尺寸和寬度分成兩個單獨的行。

tell application "Image Events" 
    set img to open imgPath 
    set dim to dimensions of img 
    set width to item 1 of dim 
    [...] 
end