我是个新手,想创建一个 Mac OS Automator 应用程序或 AppleScript,当将图像 (PSD、tiff、png、jpeg) 拖到应用程序图标上时,它将接受这些图像,转换为不同的格式并调整它们的大小。我可以在终端中使用下面的代码来执行此操作,但试图弄清楚如何构建 cd 命令并使其在 Automator 中使用 Run Shell Script 操作和类似下面的命令来工作,这样它就不需要通过终端了。理想情况下会询问所需的像素宽度。
for i in *.psb; do sips --setProperty format png --resampleHeightWidthMax 5000 "${i}" --out "${i%psb}png"; done
如何在 Automator 工作流程中实现它?
答案1
将以下 AppleScript 代码在脚本编辑器中保存为应用程序。在 Finder 中双击此新应用程序不会执行任何操作。由于代码中没有on run
处理程序,因此该应用程序将仅处理直接拖到其图标上的一个或多个图像文件。
当将单个或多个图像文件直接拖到其图标上时,您将可以选择新的图像格式和新的像素大小(图像最长边,然后用于按比例缩放图像)。
请确保在“系统偏好设置”中授予新的 AppleScript droplet 控制计算机的权限。
global thisImage, theImage, saveImage, fileExtension, saveToFolder, scaledPixels
use imageEvents : application "Image Events"
use scripting additions
property fileTypes : {BMP, JPEG, JPEG2, PICT, PNG, PSD, TIFF, QuickTime Image}
on open of droppedItems
-- Executed when files are dropped on the script
activate
set fileExtension to item 1 of (choose from list fileTypes ¬
with title "Make A Choice" with prompt ¬
"Choose A New Format For Your Image" OK button name ¬
"OK" cancel button name "Cancel") --as text
setPixels()
repeat with thisFile in droppedItems
tell application "System Events"
set saveToFolder to POSIX path of (container of thisFile) as POSIX file
end tell
tell imageEvents to set thisImage to open thisFile
scale thisImage to size scaledPixels
saveScaledImage()
end repeat
end open
to saveScaledImage()
try
tell imageEvents to save thisImage in saveToFolder as fileExtension
end try
delay 0.5
try
tell imageEvents to close thisImage
end try
end saveScaledImage
to setPixels()
try
activate
set scaledPixels to text returned of (display dialog ¬
"Scale The Longest Side Of Image To How Many Pixels? Enter Numbers Only!" default answer ¬
"3000" buttons {"Cancel", "OK"} ¬
default button 2 cancel button 1 with title ¬
"Scale Your Image") as number
on error errMsg number errNum
if errNum is not -128 then
setPixels()
else
return
end if
end try
end setPixels