2

私はしばらくの間、以下のコードを使用して、必要なさまざまな壁紙サイズに合わせて画像を自動的に縮小しました。今朝、それは動作を停止し、私が今まで見た中で最も奇妙な動作のいくつかを示しています.

ボックスを再起動すると、スクリプトは最初の実行時に期待どおりに機能します。その後、前の選択がイメージ イベント メモリに残っているようです。私が意味することは次のとおりです。

私はこのディレクトリ構造を持っています:

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

Finder で ./169/Math.png を選択して再起動した後、初めてスクリプトを実行すると、Applescript Editor で次の結果が得られます。

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"

Finder で ./Math.png を選択してスクリプトを再度実行します。

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"

Finder の選択項目に正しい壁紙が表示されているにもかかわらず、を呼び出すと、以前に選択./169/Math.pngしたものが開かれることに注意してください。open currentWallpaper as (alias)言い換えれば、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
4

1 に答える 1

2

この問題は、「画像イベント」が開いた画像への参照を返す方法が原因で発生します。

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選択した画像を処理するループに入る前にaを追加します。さらに、それが終わったら、開いた画像を閉じる必要があります。

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
于 2011-05-29T09:05:05.953 に答える