2011-05-28 64 views
2

我一直在使用下面的代碼一段時間,通過我想要的各種壁紙尺寸自動向下縮放圖像。今天早上,它停止工作,正在給我見過的一些最離奇的行爲。Applescript Finder Selection「Stuck」

如果我重新啓動我的盒子,腳本將在第一次運行時按預期工作。之後,之前的選擇似乎停留在圖像事件內存中。我的意思是這樣的:

我有這樣的目錄結構:

. 
|-- 11 
| `-- Math.png 
|-- 1610 
|-- 169 
| `-- Math.png 
|-- 43 
| `-- Math.png 
|-- Math.jpg 
`-- Math.png 

我跑了第一次的腳本./169/Math.png重啓Finder中選定後得到以下導致的AppleScript編輯:

tell application "Finder" 
     get selection 
       --> {document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk} 
     get document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:Math.png" 
end tell 
tell application "Image Events" 
     open alias "Paul:Users:tim:Downloads:test:169:Math.png" 
       --> image "Math.png" 
     get dimensions of image "Math.png" 
       --> {2560, 1440} 
     scale image "Math.png" to size 1920 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -1708 
     «event ascrgdut» 
       --> error number -1708 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
     offset of "." in "Math.png" 
       --> 5 
end tell 
tell application "Finder" 
     get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:" 
end tell 
tell application "Image Events" 
     save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1920x1080.png" as PNG 
       --> file "Paul:Users:tim:Downloads:test:169:Math_1920x1080.png" 
     scale image "Math.png" to size 1366 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
     offset of "." in "Math.png" 
       --> 5 
end tell 
tell application "Finder" 
     get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:" 
end tell 
tell application "Image Events" 
     save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1366x768.png" as PNG 
       --> file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png" 
end tell 
Result: 
file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png" of application "Image Events" 
tell application "Finder" 
     get selection 
       --> {document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk} 
     get document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:Math.png" 
end tell 
tell application "Image Events" 
     open alias "Paul:Users:tim:Downloads:test:169:Math.png" 
       --> image "Math.png" 
     get dimensions of image "Math.png" 
       --> {2560, 1440} 
     scale image "Math.png" to size 1920 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -1708 
     «event ascrgdut» 
       --> error number -1708 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
     offset of "." in "Math.png" 
       --> 5 
end tell 
tell application "Finder" 
     get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:" 
end tell 
tell application "Image Events" 
     save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1920x1080.png" as PNG 
       --> file "Paul:Users:tim:Downloads:test:169:Math_1920x1080.png" 
     scale image "Math.png" to size 1366 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
     offset of "." in "Math.png" 
       --> 5 
end tell 
tell application "Finder" 
     get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:" 
end tell 
tell application "Image Events" 
     save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1366x768.png" as PNG 
       --> file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png" 
end tell 
Result: 
file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png" of application "Image Events" 
tell application "Finder" 
     get selection 
       --> {document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk} 
     get document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:Math.png" 
end tell 
tell application "Image Events" 
     open alias "Paul:Users:tim:Downloads:test:169:Math.png" 
       --> image "Math.png" 
     get dimensions of image "Math.png" 
       --> {2560, 1440} 
     scale image "Math.png" to size 1920 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -1708 
     «event ascrgdut» 
       --> error number -1708 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
     offset of "." in "Math.png" 
       --> 5 
end tell 
tell application "Finder" 
     get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:" 
end tell 
tell application "Image Events" 
     save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1920x1080.png" as PNG 
       --> file "Paul:Users:tim:Downloads:test:169:Math_1920x1080.png" 
     scale image "Math.png" to size 1366 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
tell application "Finder" 
     get selection 
       --> {document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk} 
     get document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:Math.png" 
end tell 
tell application "Image Events" 
     open alias "Paul:Users:tim:Downloads:test:169:Math.png" 
       --> image "Math.png" 
     get dimensions of image "Math.png" 
       --> {2560, 1440} 
     scale image "Math.png" to size 1920 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -1708 
     «event ascrgdut» 
       --> error number -1708 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
     offset of "." in "Math.png" 
       --> 5 
end tell 
tell application "Finder" 
     get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:" 
end tell 
tell application "Image Events" 
     save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1920x1080.png" as PNG 
       --> file "Paul:Users:tim:Downloads:test:169:Math_1920x1080.png" 
     scale image "Math.png" to size 1366 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
     offset of "." in "Math.png" 
       --> 5 
end tell 
tell application "Finder" 
     get parent of document file "Math.png" of folder "169" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:169:" 
end tell 
tell application "Image Events" 
     save image "Math.png" in "/Users/tim/Downloads/test/169/Math_1366x768.png" as PNG 
       --> file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png" 
end tell 
Result: 
file "Paul:Users:tim:Downloads:test:169:Math_1366x768.png" of application "Image Events" 

我運行./Math.png再次腳本在Finder中選擇:

tell application "Finder" 
     get selection 
       --> {document file "Math.png" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk} 
     get document file "Math.png" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:Math.png" 
end tell 
tell application "Image Events" 
     open alias "Paul:Users:tim:Downloads:test:Math.png" 
       --> image "Math.png" 
     get dimensions of image "Math.png" 
       --> {2560, 1440} 
     scale image "Math.png" to size 1920 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
     offset of "." in "Math.png" 
       --> 5 
end tell 
tell application "Finder" 
     get parent of document file "Math.png" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:" 
end tell 
tell application "Image Events" 
     save image "Math.png" in "/Users/tim/Downloads/test/Math_1920x1080.png" as PNG 
       --> file "Paul:Users:tim:Downloads:test:Math_1920x1080.png" 
     scale image "Math.png" to size 1366 
     get name of image "Math.png" 
       --> "Math.png" 
     get name of image "Math.png" 
       --> "Math.png" 
     offset of "." in "Math.png" 
       --> error number -10004 
end tell 
tell current application 
     offset of "." in "Math.png" 
       --> 5 
end tell 
tell application "Finder" 
     get parent of document file "Math.png" of folder "test" of folder "Downloads" of folder "tim" of folder "Users" of startup disk 
       --> alias "Paul:Users:tim:Downloads:test:" 
end tell 
tell application "Image Events" 
     save image "Math.png" in "/Users/tim/Downloads/test/Math_1366x768.png" as PNG 
       --> file "Paul:Users:tim:Downloads:test:Math_1366x768.png" 
end tell 
Result: 
file "Paul:Users:tim:Downloads:test:Math_1366x768.png" of application "Image Events" 

請注意,先前選擇的./169/Math.png是我撥打open currentWallpaper as (alias)時打開的那個,儘管正確的壁紙顯示在Finder選擇中。換句話說,Finder並不喜歡舊的選擇,但是我循環的壁紙卡住了舊的選擇。也許更奇怪的是,如果我移動舊的壁紙,我選擇了,現在的壁紙參考隨它一起移動!換句話說,即使我廢棄舊選擇,腳本的輸出也會報告它正在打開./.Trashes中的一個。

只要名稱不同,它似乎不會感到困惑。

腳本:

tell application "Finder" to set wallpapers to selection 

set myDimensions to {w169:[{2560, 1440}, {1920, 1080}, {1366, 768}], w1610:[{2560, 1600}, {1920, 1200}, {1440, 900}, {1280, 800}], f43:[{1600, 1200}], s:[{1024, 1024}]} 

tell application "Image Events" 
    repeat with currentWallpaper in wallpapers 
      set theWallpaper to open (currentWallpaper as alias) 
      tell theWallpaper 
        set theWallpaperDimensions to dimensions of theWallpaper 
        set currentAspects to {} 
        if my aspectsEqual(16/10, theWallpaperDimensions) then set currentAspects to w1610 of myDimensions 
        if my aspectsEqual(16/9, theWallpaperDimensions) then set currentAspects to w169 of myDimensions 
        if my aspectsEqual(4/3, theWallpaperDimensions) then set currentAspects to f43 of myDimensions 
        if my aspectsEqual(1, theWallpaperDimensions) then set currentAspects to s of myDimensions 
        repeat with dim in currentAspects 
          -- Scale and Crop to current dimensions 
          if item 1 of theWallpaperDimensions is greater than item 1 of dim then 
            scale to size (get item 1 of dim) 

            -- Set up save target 
            set namePrefix to text items 1 thru ((offset of "." in (get name)) - 1) of (get name) as string 
            tell application "Finder" 
              set _ to parent of currentWallpaper as alias 
              set parentPath to POSIX path of _ 
            end tell 
            set saveTarget to parentPath & namePrefix & "_" & (get item 1 of dim) & "x" & (get item 2 of dim) & ".png" 

            --save to target 
            save in saveTarget as PNG 
          end if 
        end repeat 
      end tell 
    end repeat 
end tell 

on aspectsEqual(baseAspect, wallpaperDimensions) 
    set wallpaperAspect to (item 1 of wallpaperDimensions)/(item 2 of wallpaperDimensions) 
    set aspectDifference to baseAspect - wallpaperAspect 
    if aspectDifference is less than 0 then set aspectDifference to aspectDifference * -1 
    return aspectDifference is less than 1.0E-3 
end aspectsEqual 

回答

1

問題是由道路「圖片事件」造成返回引用打開的圖像:

open alias "Paul:Users:tim:Downloads:test:Math.png" 
     --> image "Math.png" 
open alias "Paul:Users:tim:Downloads:test:169:Math.png" 
     --> image "Math.png" 

打開的圖像會用名字引用。如果您打開具有相同名稱的其他圖像,返回的參考可能偶爾會引用先前打開的同名圖像。

作爲解決方法,在進入處理選定圖像的循環之前添加close every image。此外,您需要在完成後關閉打開的圖像:

tell application "Image Events" 
    close every image 
    repeat with currentWallpaper in wallpapers 
     set theWallpaper to open (currentWallpaper as alias) 
     tell theWallpaper 
     ... 
     end tell 
     close theWallpaper 
    end repeat 
end tell 
+0

這似乎沒有效果,雖然我同意它更乾淨並且有價值的建議。但是,我仍然觀察到「open alias」foo:bar:fooAspectRatio:fooImage「'打開與第一次腳本運行相關的圖像。 – 2011-05-29 11:48:14

相關問題