Applescript Finder Selection“Stuck”

问题描述:

我一直在使用下面的代码一段时间,通过我想要的各种壁纸尺寸自动向下缩放图像。今天早上,它停止工作,正在给我见过的一些最离奇的行为。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 

问题是由道路“图片事件”造成返回引用打开的图像:

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