尝试编写一个简单的脚本将图像文件转换为不同的大小,AppleScript在保存文件时不断返回“缺失值”。任何人都知道以下代码中发生了什么?
set f to choose file
set image_file to f as alias
convertFile(image_file, "_512@2x.", 1024)
convertFile(image_file, "_512.", 512)
-- iPad (iOS 7+)
convertFile(image_file, "_76@2x.", 152)
convertFile(image_file, "_76.", 76)
-- iPad (iOS 6)
convertFile(image_file, "_72@2x.", 144)
convertFile(image_file, "_72.", 72)
-- iPhone (iOS 7+)
convertFile(image_file, "_60@3x.", 180)
convertFile(image_file, "_60@2x.", 120)
convertFile(image_file, "_60.", 60)
-- iPhone (iOS 6)
convertFile(image_file, "_57@2x.", 114)
convertFile(image_file, "_57.", 57)
-- iPhone (iOS 7 Spotlight)
convertFile(image_file, "_40@2x.", 80)
convertFile(image_file, "_40.", 40)
-- iPhone (iOS 5,6 Spotlight, iOS 5-7 Settings)
convertFile(image_file, "_29@2x.", 58)
convertFile(image_file, "_29.", 29)
on convertFile(source_file, file_spec, image_size)
set my_path to POSIX path of source_file
set AppleScript's text item delimiters to "/"
set path_parts to text items of my_path
if last item of path_parts is not "" then
set file_name to last item of path_parts
set file_path to items 1 thru -2 of path_parts as string
end if
set AppleScript's text item delimiters to "."
set filename_parts to text items of file_name
set prefix to items 1 thru -2 of filename_parts as string
set suffix to last item of filename_parts
set target_posix_path to file_path & "/" & prefix & file_spec & suffix
set target_posix_file to target_posix_path as POSIX file
tell application "Image Events"
set this_image to open source_file
scale this_image to size image_size
save this_image in target_posix_path
close this_image saving no
end tell
end convertFile
活动&在我的MacBook Air上回复输出(Mac OS X 10.9.5):
tell application "AppleScript Editor"
choose file
--> alias "Macintosh HD:Users:william:Desktop:goro_icon:icon.png"
end tell
tell application "Image Events"
open alias "Macintosh HD:Users:william:Desktop:goro_icon:icon.png"
--> image "icon.png"
scale image "icon.png" to size 1024
save image "icon.png" as PNG in "/Users/william/Desktop/goro_icon/icon_512@2x.png"
--> file "Macintosh HD:Users:william:Desktop:goro_icon:icon_512@2x.png"
close image "icon.png" saving no
open alias "Macintosh HD:Users:william:Desktop:goro_icon:icon.png"
--> image "icon.png"
scale image "icon.png" to size 512
save image "icon.png" as PNG in "/Users/william/Desktop/goro_icon/icon_512.png"
--> missing value
close image "icon.png" saving no
open alias "Macintosh HD:Users:william:Desktop:goro_icon:icon.png"
--> image "icon.png"
scale image "icon.png" to size 152
save image "icon.png" as PNG in "/Users/william/Desktop/goro_icon/icon_76@2x.png"
--> missing value
close image "icon.png" saving no
(omitted the long list of same errors..)
end tell
答案 0 :(得分:1)
最后,在尝试了Paul在上面的评论中提到的内容之后,我找到了与文件本身相关的问题的原因。我用另一个文件尝试了脚本,脚本没有问题。
在编辑器中打开文件并导出具有相同尺寸的新文件(因此文件看起来相同,但大小不同)。新文件有效。因此,问题可能是由一些未发现的图像事件错误引起的。