2011-08-25 23 views
1

我有一个简单的Applescript来调整与图像事件的照片。这些照片都是足球运动员,所以他们都以“1.jpg”,“4.jpg”等数字命名。我遇到的问题是当我在不同目录中执行多批玩家时,脚本将用另一个来自另一个团队的具有相同文件名并且之前完成的照片覆盖照片。再次,这些照片全部放置在不同的目录中。最终的结果是,在成功运行两三次之后,重新格式化的球员照片会变得混乱。图像事件的脚本覆盖我的文件与以前格式相同的文件

下面是我在脚本中调用Image Events的内容。

on open some_items 
    repeat with this_item in some_items 
     try 
      rescale_and_save(this_item) 
     end try 
    end repeat 
end open 


to rescale_and_save(this_item) 
    tell application "Image Events" 
     launch 
     set the target_width to 290 
     -- open the image file 
     set this_image to open this_item 

     set typ to this_image's file type 

     copy dimensions of this_image to {current_width, current_height} 
     if current_width is greater than current_height then 
      scale this_image to size target_width 
     else 
      -- figure out new height 
      -- y2 = (y1 * x2)/x1 
      set the new_height to (current_height * target_width)/current_width 
      scale this_image to size new_height 
     end if 

     tell application "Finder" to set new_item to ¬ 
      (container of this_item as string) & "" & (name of this_item) 
     save this_image in new_item as typ 

    end tell 
end rescale_and_save 

回答

2

您似乎在处理多个具有相同名称的项目的图像事件中触发了一个错误。我没有看到你描述的确切行为,但我看到了类似的行为。

我建议你简单地告诉图像事件处理完每个文件夹后退出;这样它就不会感到困惑。 (您不需要launch,要么;使用launch的唯一原因是,如果你想有一个非后台应用程序没有呈现无标题文档窗口中打开。)

顺便说一句,如果你想覆盖现有的图像与缩放版本,所有你需要的是save this_image。如果要打开文档,对其进行修改并保存,则图像事件与任何其他应用程序的行为非常相似。

+0

谢谢,这很好。 –