Applescript Finder 选择“卡住”

发布于 2024-11-10 03:46:40 字数 15149 浏览 3 评论 0原文

我已经使用下面的代码一段时间了,可以通过我想要的各种壁纸尺寸自动神奇地缩小图像。今天早上,它停止工作,并出现了一些我见过的最奇怪的行为。

如果我重新启动我的盒子,脚本第一次运行时将按预期工作。之后,之前的选择似乎被困在图像事件内存中。我的意思是:

我有这样的目录结构:

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

我在重新启动后第一次运行脚本,并在 Finder 中选择 ./169/Math.png,并在 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"

我再次运行脚本在 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"

请注意,之前选择的 ./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

I've been using the code below for awhile to auto-magically scale images down through the various wallpaper sizes that I want. This morning, it ceased working and is giving some of the most bizarre behavior I've ever seen.

If I reboot my box, the script works as expected the first time it runs. After that, the previous selection seems to be stuck in the Image Events memory. What I mean is the following:

I have this directory structure:

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

I run the script for the first time after rebooting with ./169/Math.png selected in the Finder and get the following results in 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"

I run the script again with ./Math.png selected in 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"

Notice that the previously selected ./169/Math.png is the one that gets open when I call open currentWallpaper as (alias), despite the fact that the correct wallpaper shows up in the Finder selection. In other words, it's not like Finder is stuck with the old selection, but that the wallpapers that I'm looping through get stuck on the old ones.

Perhaps even more bizarre, is that if I move the old wallpaper, that I had selected, the currentWallpaper reference moves with it! In other words, even if I trash the old selection, the output from the script reports that it's opening the one in ./.Trashes.

It does not seem to get confused as long as the names are different.

The Script:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我做我的改变 2024-11-17 03:46:40

该问题是由“图像事件”返回对打开的图像的引用的方式引起的:

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"

打开的图像是按名称引用的。如果您打开另一个同名图像,返回的引用有时可能会引用之前打开的同名图像。

作为解决方法,请在进入处理所选图像的循环之前添加关闭每个图像。此外,完成后您需要关闭打开的图像:

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

The problem is caused by way "Image Events" returns references to opened images:

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"

The opened image is referenced by name. If you open another image with the same name, the returned reference may occasionally refer to a previously opened image of the same name.

As a work-around add a close every image before you enter the loop that processes the selected images. Furthermore you need to close the opened image when you are done with it:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文